/** * PlayGame prompts the user for input and interacts to play the game of Yote. * This version uses Canvas, Square, and Circle classes to create a graphical * display of the output. * * @author Dale Reed * @version October 24, 2008 */ import java.util.Scanner; public class PlayGame { /** keyboard is used to handle user input */ private Scanner keyboard = new Scanner( System.in); /** theBoard is used to create a playing board, which in turn keeps track of the pieces */ private Board theBoard; // the playing board /** moveNumber keeps track of whose move it is, based on odd for "black" and even for "white" */ private int moveNumber = 1; // odd moves mean "black", evens mean "white" /** index of where we are moving from */ private int sourcePosition = -1; /** index of where we are moving to */ private int destinationPosition = -1; /** constant definition of number of columns, used in validating moves */ static final int NUMBER_OF_COLUMNS = 5; /** * main method chains off to method doIt() to avoid static errors. * * @param args command line arguments, which we're not using */ public static void main(String[] args) { PlayGame myInstance = new PlayGame(); myInstance.doIt(); // chain off to begin playing } /** * The "guts" of the program, calling the Board constructor and the loop handling user input. * */ void doIt() { // Display identifying information System.out.println( "Author: Dale Reed \n" + "Program #3 solution\n" + "TA: Englebert Humberdink, T 4-5 \n"+ "Oct 24, 2008"); System.out.println(); // Display game instructions. Modified from instructions taken from // http://homepages.di.fc.ul.pt/~jpn/gv/yote.htm System.out.println( "Welcome to the ancient African game of Yote.\n" + "\n" + "On each turn, each player may either deploy a stone from their reserve \n" + "to any empty space on the board, or move a stone already on the board. Moves must \n" + "be made vertically or horizontally, but not diagonally. Stones move either by \n" + "sliding to an adjacent empty cell or by jumping over and capturing an adjacent \n" + "opponent's stone, landing in an empty square immediately beyond. When a Stone \n" + "captures another stone, the player may then select any other opponent stone on \n" + "the board (but not in their opponent's reserve) and capture it as well. \n" + "\n" + "The player who captures all of her opponent's stones wins. If either player is unable \n" + "to move, then the game ends and the player with the most stones on the board wins. If \n" + "both players are reduced to three or less stones, then the game is a draw. \n" + "\n" + "Possible inputs for each move: \n" + " x to exit program. A message tells who has the most pieces. \n" + " n1 to place a piece at postion n1, where n1 is a number 1..20 \n" + " n1 n2 to move a piece from board position n1 to n2 \n" + "Black goes first. Let's begin...\n" + " \n"); theBoard = new Board(); // allocate memory and call the Board constructor String playerToMove; // will be "black" or "white", depending on the moveNumber // main loop while( true ) { // see whose move it is, based on the moveNumber if( moveNumber%2 == 1) { // odd numbered move, so it is the turn of "black" playerToMove = "black"; } else { // even numbered move playerToMove = "white"; } // prompt for and get user input System.out.print(moveNumber + ". Enter move for " + playerToMove + ": "); String userInput = keyboard.nextLine(); // convert to lower case, in case it is alphabetic userInput = userInput.toLowerCase(); // see if 'x' to exit was entered if( userInput.charAt( 0) == 'x') { break; } // Extract the index of the location where piece should be placed destinationPosition = Integer.parseInt( userInput); theBoard.placePiece( playerToMove, destinationPosition); // update the move number moveNumber++; } //while (true // main loop System.out.println("Thanks for playing. Exiting program... "); }//end method doIt() }// end class TestDrawing()