Over 10 Million Study Resources Now at Your Fingertips


Download as :
Rating : ⭐⭐⭐⭐⭐
Price : $10.99
Language:EN
Pages: 2

The value player should one the constants red black not

CHAPTER 7. ARRAYS 357

board[toRow][toCol] = board[fromRow][fromCol]; // Move the piece. board[fromRow][fromCol] = EMPTY;

if (toRow == 7 && board[toRow][toCol] == BLACK)

}

int fromRow, fromCol; // Position of piece to be moved. int toRow, toCol; // Square it is to move to.

CheckersMove(int r1, int c1, int r2, int c2) {

It is assumed that

// the move is legal. In a jump, the piece moves two

The CheckersData class has an instance method which finds all the legal moves that are

currently available for a specified player. This method is a function that returns an array

* Return an array containing all the legal CheckersMoves

* for the specified player on the current board.

Start with an empty list of moves
Find any legal jumps and add them to the list
if there are no jumps:
Find any other legal moves and add them to the list if the list is empty:
return null
else:
return the list

Now, what is this “list”? We have to return the legal moves in an array. But since an array has a fixed size, we can’t create the array until we know how many moves there are, and we don’t know that until near the end of the method, after we’ve already made the list! A neat solution is to use an ArrayList instead of an array to hold the moves as we find them. In fact, I use an object defined by the parameterized type ArrayList<CheckersMove> so that the list is restricted to holding objects of type CheckersMove. As we add moves to the list, it will grow just as large as necessary. At the end of the method, we can create the array that we really want and copy the data into it:

How It Works
Login account
Login Your Account
Add to cart
Add to Cart
Payment
Make payment
Document download
Download File
PageId: ELI4E50228
img
Uploaded by :
PbPlLNAN
Page 1 Preview
the value player should one the constants red blac
Sell Your Old Documents & Earn Wallet Balance