CS 107 (Spring '09)
[Schedule]
[Programs]
[Notes
& Reference] [Examples][Syllabus]
[Lab & TA] [Tests]
[Grades]
Prof. Reed, CS 107, Fall '08
Due Friday 10/31 at 6:00 PM
Write a program in Java to allow two human players to play the game of Yote. Rules of the game are:
This variant on the ancient African game of Yote (YOH-tay) is played on an empty 4x5 square board, where each player has 8 stones initially off the board in their "reserve."
TURN - On each turn, each player may either deploy a stone from their reserve to any empty space on the board, or move a stone already on the board. Moves must be made vertically or horizontally, but not diagonally. Stones move either by sliding to an adjacent empty cell or by jumping over and capturing an adjacent opponent's stone, landing in an empty square immediately beyond. When a Stone captures another stone, the player may then select any other opponent stone on the board (but not in their opponent's reserve) and capture it as well. Captures are not mandatory.
GOAL - The player who captures all of her opponent's stones wins. If either player is unable to move, then the game ends and the player with the most stones remaining wins. If both players are reduced to three or less stones, then the game is a draw.
(The above copy of rules slightly modified from here. See also the Zillions of Games site link.)
For this first part we will only be displaying the board and allowing the human players to place pieces on the board. No moves need to be made, and no error checking is needed in this first part. You must create a graphical representation of this game, that will look something like what is shown at left below, allowing two human players to play against each other. As play progresses, user input is given from the keyboard in the terminal window. Black goes first:
Then after entering 7 to place the piece in square 7, the board looks like:
Then entering 9 for white, would give:
And so on. Note that the 8 white pieces are labelled sequentially, staring with 'A'. The 8 black pieces (actually "darkGray") are labelled sequentially starting with 'J'. As play progresses, each time pieces are placed on the board the respective counters at the top and bottom of the board are updated. Eventually the game might look like:
On each move, the user will enter either the square number where the piece is to be placed, or 'x' to exit the program.

System.out.println("Starting the code to test the Piece class...");
/** Generate an array that can keep track of 4 pieces */
Piece[] thePieces = new Piece[ 4];
// Now allocate memory and initialize values for each Piece
for( int i=0; i<thePieces.length; i++) {
// for this particular piece, call the constructor
// Note that even though created, they are not visible!
thePieces[ i] = new Piece(0,0,60,"blue",false,"A",-1);
}
// now one at a time change values for the Pieces
// change position to 75,75 and change color to red, make it visible
// and set label to "B"
thePieces[ 0].setCircleValues(75,75,60,"red",true,"B");
// store a value into the index field, just to show we can
thePieces[ 0].setBoardPosition( 3);
int theIndexValue = thePieces[ 0].getBoardPosition();
System.out.println("index value for this piece is: " + theIndexValue);
// for this one, move it first, then make it visible
thePieces[ 1].moveTheCircle('r'); // move it to the right
thePieces[ 1].setLabel("C");
thePieces[ 1].setVisibility( true);
// for this one just change location and visibility
// note that label is still default of "A"
thePieces[ 2].setCircleValues(25,75,true);
thePieces[ 2].setColor("green");
// for this last one make it visible and move it down two "squares" worth
thePieces[ 3].setVisibility( true);
thePieces[ 3].setLabel("D");
thePieces[ 3].moveTheCircle('d', 2);
System.out.println("Done testing the Piece class. Exiting...");
The above code should create the output shown below. 
// Create the playing board
theBoard = new Board(); // calls the Board constructor which "paints" the screen
Running the code should now give you:
if ((moveNumber %2) == 10)If moveNumber is odd, then it is the turn of "black", else it is the turn of "white". Your code inside the PlayGame class doIt() method at this point might look something like the following:
// Create the playing board
theBoard = new Board(); // calls the Board constructor which "paints" the screen
moveNumber = 1; // odd moves mean "black", evens mean "white"
while( true) {
// prompt for and get user input
System.out.print("At what position would you like to place your piece? ");
int destination = keyboard.nextInt();
// See if the current move is for "black" or "white", depending on the move number
if( moveNumber%2 == 1) {
// it is odd since the remainder is 1, so it is black's move
// theBoard.placePiece("black", destinationPosition); // make a black piece to the destinationPosition
}
else {
// it is even since the remainder is 0, so it is white's move
// theBoard.placePiece("white", destinationPosition); // make a white piece to the destinationPosition
}
// update the move
moveNumber++;
}
Remember that the arrays of pieces are handled inside the Board class. We interact with them by calling methods in the Board class, such as theBoard.placePiece(...) method that is commented out above. Let's look at the details of what this method should do.
if( theColor.equals("black") && (blackPiecesNumber > 0)) {
// place the appropriate next piece into the requested position and make it visible
int nextPieceIndex = 8 - blackPiecesNumber;
blackPieces[ nextPieceIndex].setCircleValues(x, y, true);
// decrement the count of black pieces and update that display on the playing board
blackPiecesNumber--;
updatePiecesDisplay( "black", blackPiecesNumber);
}
There should be similar code for "white".
At this point you should be able to place pieces on the board, both black and white, giving (at some point in some game) a board that might look like:
turnin -c cs107 -p program3 Yote
where the directory containing your solution is called Yote. Within this directory the java class containing the main( ) method used to start the program must be defined in a file called PlayGame.java
If you wrote *both* program 3 and 4 together, and want to turn them in together, then turn them in to a project called prog3and4 rather than the project name (program3) specified above.
[CS Dept] [UIC] [Prof. Reed]