/** * PlayGame prompts the user for input and interacts to play the game. * * @author Dale Reed * @version February 20, 2009 */ 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; /** pieceToBeMovedLabel is the label of the piece to be moved */ int pieceToBeMovedLabel = 0; /** userinput, which can be 'x' to exit, 'r' to randomize board, or a number '1'..'8' */ String userInput = ""; /** * 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, 9 Tiles Puzzle\n" + "TA: Englebert Humberdink, T 4-5 \n"+ "Feb 20, 2009"); System.out.println(); // Display game instructions. System.out.println( "Welcome to the 9 tiles puzzle.\n" + "On each turn, select a tile to move (1..8) \n" + "Once the tiles are in ascending numerical order with the blank square at the \n" + "bottom right, then the game ends. Enter x at any point to exit the game. \n" + "Enter 'r' at any point to randomize the board tiles. Have fun!\n"); theBoard = new Board(); // allocate memory and call the Board constructor // main loop while( gameIsNotDone() ) { // prompt for and get user input System.out.print(moveNumber + ". Enter square to move: "); // read in the piece to be moved (or x to exit) userInput = keyboard.nextLine(); // convert to upper case to simplify comparisons later userInput = userInput.toUpperCase(); // check to see if exit ('x') was chosen if( userInput.charAt(0)=='X') { // user wants to exit break; } // check to see if randomize ('r') was chosen if( userInput.charAt(0)=='R') { // user wants to randomize the tiles theBoard.randomizeBoard(); continue; } // Getting here means the user wants to make a move, since neither 'x' nor 'r' // was chosen. Convert the first user input character to an integer for subsequent use. // This is done by character arithmetic. I.e. '2' - '0' equals the int value 2 pieceToBeMovedLabel = userInput.charAt(0) - '0'; // verify that index is within range if( (pieceToBeMovedLabel < 1) || (pieceToBeMovedLabel > 8) ) { // error, piece is out of range. System.out.println("*** Sorry, that piece is out of range. Please retry. *** "); continue; } // Make the move. This code returns 0 if successful, -1 otherwise if( theBoard.moveSquare( pieceToBeMovedLabel) != 0) { // move was invalid continue; // branch back up to retry move } // update the move moveNumber++; }//while (bothPlayersHaveMoreThan3Pieces... // main loop System.out.println("Thanks for playing. Exiting program... "); }//end method doIt() /** * See if game is done. * Game continues if pieces are not in order * * @return true if tiles are in order, false otherwise */ private boolean gameIsNotDone() { // verify piece ordering return true; } }// end class PlayGame()