dimanche 29 novembre 2020

How do I check for the smallest array value every time?

So there is a 2d array. In every row of the first column, there are 4 pieces. Every space on the 2d array is a stack of pieces, whether it be 0 or more. Every piece in the first column has to be moved to the second column, but they can only be moved one at a time, and they have to fill up the smallest stack in that second column first. For example, if every row in column 2 has a stack size of 1, then you can place a piece anywhere. But if there is one stack size of 2 while every other stack size is 1, then you can place it anywhere other than that stack of size 2. I am having a hard time implementing this. So far, it allows me to move one piece to the second column without stacking, then it no longer lets me move any pieces to the second column. I assume this is because of the smallestStackSize value not updating. I have also tried using a greatestStackSize value to see if that would help, and I am having similar issues. This method is being called every time someone clicks on a piece to move it. Could really use some help!

private int smallestStackSize_ = 0;
private int greatestStackSize_ = 1;
/**
     * Allows the players to place their pieces on the grid
     */
    public boolean setupPiece () {
        
        // board_.getHeight returns the number of rows on the board
        int[] numPieces = new int[board_.getHeight()];

        // initialize numPieces to contain the number of pieces in each row of
        // column 1. pieceHolderGrid_[][] is the array that holds all of the 
        // stacks of pieces. 
        for ( int i = 0 ; i < numPieces.length ; i++ ) {
            numPieces[i] = pieceHolderGrid_[i][1].getSize(); //all rows of column 1
        }
        
        // update smallestStackSize and greatestStackSize_
        smallestStackSize_ = numPieces[0];
        for ( int i = 1 ; i < numPieces.length ; i++ ) {
            if (numPieces[i] > greatestStackSize_) {
                greatestStackSize_ = numPieces[i];
            }
            if (numPieces[i] < smallestStackSize_) {
                smallestStackSize_ = numPieces[i];
            }
        }
        
        // check if move is to the smallest stack size
        if ( numPieces[1] == smallestStackSize_ ) {
            return true;
        } else {
            return false;
        }

    }

Aucun commentaire:

Enregistrer un commentaire