CS 107 (Spring '09)
[Schedule]
[Programs]
[Notes
& Reference] [Examples][Syllabus]
[Lab & TA] [Tests]
[Grades]
Prof. Reed, CS 107, Fall '08
Due Friday 11/14 at 1:00 PM
Continue to work on your graphical Yote game-playing program from last time, that will allow two human players to play the game. In this second part you will need to implement making moves, validating moves, and checking for a win or a draw. Rules of the game are the same as in the previous version. Note that to simplify things, no double-jumps will be allowed. If you prefer, you can start with my solution to part 1 of Yote.
On each move, the user will enter one of the following types of input:
x to exit
16 (for example) to place a piece on square 16
17 16 (for example) to move the piece from position 17 to position 16
Only valid moves are allowed. If invalid input is given, an error message should be given and the user prompted to try again. Running the game should look like the following:
As play progresses, pieces might be (unskillfully) placed as follows:
Note how error messages are given for invalid moves, prompting the user to retry. Assume that black makes a jumping move:
After a jump, an additional piece is taken off the board. so here another white piece will be removed.
Play continues back and forth, filling up the board with pieces.
Let's say black now wants to take the white 'C' piece currently in position 10. On this move black can place a piece into the corner at position 5:
White could place a new piece at 15, or could alternatively move piece 'C' from position 10 to position 15, as shown below.
Error checking must be done to ensure that only valid moves are made, as shown below for white's next move:
Once all pieces have been placed, attempts to make that sort of move must result in an error message and a retry:
The program should detect once a player loses, or there is a draw (when both players have 3 or less pieces)
String inputFirstPart;
String inputSecondPart;
int sourcePosition; // will store the index of where we're moving from
int destinationPosition; // will store the index of where we're moving to
// find the space, if there is one, in the user input
int indexOfSpace = userInput.indexOf(' '); // stores -1 if ' ' not found
if( indexOfSpace < 0) {
// there should only be one number on the input line, so parse it to an integer
destinationPosition = Integer.parseInt( userInput);
}
else {
// Since there is a space in the input, there should be more than one number.
// This means we are moving a piece, either to an adjacent position or to make a jump.
// Break up input string into two parts
String inputFirstPart = userInput.substring(0,indexOfSpace);
String inputSecondPart = userInput.substring(indexOfSpace, userInput.length());
// Trim off any excess leading or trailing space that might confuse the conversion to int
inputFirstPart = inputFirstPart.trim();
inputSecondPart = inputSecondPart.trim();
// Now parse the two numbers from the two parts
sourcePosition = Integer.parseInt( inputFirstPart);
destinationPosition = Integer.parseInt( inputSecondPart);
//... other code ...
}
I recommend that throughout this program you read user input as a whole line (keyboard.nextLine()), and then look at the different parts of it. It gets very confusing if you read a whole line sometimes but then later read only part of it (e.g. keyboard.nextInt()), leaving the carriage-return character still sitting on the input line. Your program ends up "skipping" your input the next time around your main loop.// ensure sourcePosition is the same color as the playerToMoveFor reference remember the BlueJ class drawing for this project, that shows which classes use which other classes:
if( ! theBoard.colorOfPieceAt( sourcePosition).equals( playerToMove) ) {
System.out.println("*** "+playerToMove+" you can only move your own piece. Please retry...");
return false; // go back to prompt for another move
}

turnin -c cs107 -p program4 Yote2
where the directory containing your solution is called Yote2. Within this directory the java class containing the main( ) method used to start the program must be defined in a file called PlayGame.java[CS Dept] [UIC] [Prof. Reed]