// Overview: The class represents a session of Checkers between two // players. It contains atttributes to describe the status of the game // and different methods that is used to run the games . This class is // different from CheckersBoard class in the sense that, it captures // the game where the CheckerBoard captures the state of the board at // a given time. import java.io.*; import java.lang.*; import java.util.Scanner; import java.util.InputMismatchException; public class CheckersGame{ // Attributes private boolean gameContinue; private int currentPlayer; // Who's turn is this private int gameFinished; // Is the game finished private CheckersBoard checker; // The current Board private int maxAttempts=3; // How many times the player is allowed to make mistakes in chosing moves private Piece[][] currentBoard; private CheckersMove[] legalMoves; // List of legal moves // Methods // Constructor: Setup the game initially public CheckersGame(){ currentPlayer=1; // Start with RED player gameFinished=0; // The game is not finished gameContinue=true; } public void gameOn(){ /* The method will coordinates the activities during a game session. It includes i. Accept move from the player ii. Sending the moves to the apprpriate object for implementation */ String userInput=""; System.out.println("\n********Welcome to the world of CHECKERS*********\n"); System.out.println("********************************************************\n"); System.out.println("This is a Two-Player game where each of the players have\n"); System.out.println("has 12 pieces to begin with. \n"); System.out.println("Please determine among yourselves who will have the RED \n"); System.out.println("pieces and who'll have the BLACK pieces.\n"); System.out.println("********************************************************\n"); System.out.println("The Game is about to be start ...\n"); System.out.println("Enjoy!!!!!\n"); while(gameContinue){ // Allow players to play multiple games currentPlayer=1; // Start with RED player System.out.println("Start of a new game\n"); checker=new CheckersBoard(); // Create an Checker game while(gameFinished==0){ // Capture a move int movingPiece; int noofMoveAttempts=0; CheckersMove move; System.out.println("*************Checker Board*********************"); checker.displayBoard(); // Display the board to the player before making a make System.out.println("***********************************************"); legalMoves=checker.getLegalMoves(currentPlayer); if (legalMoves == null) {// There is no legal moves for the current player gameOver(currentPlayer); break; } currentBoard=checker.getBoard(); // Get the move from the user move=getMovefromUser(maxAttempts,legalMoves,currentBoard,currentPlayer); if (move==null) { gameOver(currentPlayer); break; } checker.makeMove(move); // Implement the move on the board if (move.isJump()) { // The player wants to make a jump legalMoves = checker.getLegalJumpsFrom(currentPlayer,move.gettoRow(),move.gettoCol()); if (legalMoves != null) { if (currentPlayer == CheckersData.RED) { System.out.println("RED: You must continue jumping."); } else { System.out.println("BLACK: You must continue jumping."); return; } } } // Change the turn to the other player if (currentPlayer==1) { currentPlayer=3; } else if (currentPlayer==3){ currentPlayer=1; } } System.out.println("Do you want to play again? Y/N"); try { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); userInput = in.readLine(); } catch(IOException e){ System.out.println("IOException has been caught"); } if (userInput.equalsIgnoreCase("n")||(userInput.equalsIgnoreCase("no"))) { gameContinue=false; System.out.println("Thanks you for playing the CHECKERS.\n"); System.out.println("Hope you've enjoyed the game.\n"); System.out.println("See you again!!!\n"); } } } //end gameOn() public CheckersMove getMovefromUser(int max, CheckersMove[] OKMoves, Piece[][] cBoard, int player){ /* The method gets a move from the player. It also checks whether the move is legal. It uses a thresold representing the maximum number of mistake a player can make while picking a move option. The method also provide context information to the player. */ String pStr=""; String playerString=Integer.toString(player); int pieceToMove=999; int rowTo=999;int colTo=999; int validMove;int attemptNo=0; CheckersMove move; if (player==1) { playerString="R"; pStr="RED"; } else if (player==3) { playerString="B"; pStr="BLACK"; } do { // Continue until it gets a legal move from the player int fromRow=999; int fromCol=999; // Which piece the user wants to move? The player need not to select the position // of the piece. Only the ID. System.out.println("Turn for the "+pStr+"\n"); do { Scanner in = new Scanner(System.in); System.out.println("Which piece you want to move?\n"); System.out.println("Pick any of the "+playerString+" piece\n"); System.out.println("Select a negative number to exit the current game\n"); try { pieceToMove = in.nextInt(); } catch(InputMismatchException e) { System.out.println("The input you provided is not a number"); System.out.println("Please try again...\n"); continue; } if (pieceToMove>11) { System.out.println("Not a valid piece\n"); System.out.println("Should be within 0 and 11 inclusive"); pieceToMove=999; } else if (pieceToMove<0) { return null; } } while (pieceToMove==999); // Convert userInput to integer // Where the player wants the piece to move? do { Scanner in = new Scanner(System.in); System.out.println("Which row you want to move to?\n"); System.out.println("Select a negative number to exit the current game\n"); try { rowTo = in.nextInt(); } catch (InputMismatchException e) { System.out.print("The input you provided is not a number"); System.out.println("Please try again...\n"); continue; } if (rowTo>7) { System.out.println("Not a valid row number\n"); System.out.println("Should be wihtin 0 and 7 inclusive"); rowTo=999; } else if (rowTo<0) { return null; } } while (rowTo==999); do { Scanner in = new Scanner(System.in); System.out.println("Which column you want to move to?\n"); System.out.println("Select a negative number to exit the current game\n"); try { colTo = in.nextInt(); } catch(InputMismatchException e) { System.out.print("The input you provided is not a number"); System.out.println("Please try again...\n"); continue; } if (colTo>7) { System.out.println("Not a valid column number\n"); System.out.println("Should be wihtin 0 and 7 inclusive"); colTo=999; } else if (colTo<0) { return null; } }while (colTo==999); // Get the position of the piece the player wants to move for (int i=0;i<8;i++) { for (int j=0;j<8;j++) { if ((cBoard[i][j].getpieceID()==pieceToMove) && (cBoard[i][j].getPlayer()==player)) { fromRow=i; fromCol=j; break; } } } // Because of the input checking done previously, we can attempt to create the move move = new CheckersMove(fromRow,fromCol,rowTo,colTo); // Now check the validity of the move validMove=0; for (int i=0;((OKMoves[i]!=null)&&(move!=null));i++) { if ((OKMoves[i].getfromRow()==move.getfromRow())&&(OKMoves[i].getfromCol()==move.getfromCol())&&(OKMoves[i].gettoRow()==move.gettoRow())&&(OKMoves[i].gettoCol()==move.gettoCol())) { // Check if the attributes of the chosen move match System.out.println("That's a valid move"); validMove=1; } } if (validMove==0) { // It's not a valid move System.out.println("That's NOT a valid move\n"); System.out.println("At this point you are restricted to the following moves:\n"); System.out.println("From Row\tFrom Column\tDestination Row\tDestination Column\n"); for (int i=0;OKMoves[i]!=null;i++) { System.out.println(OKMoves[i].getfromRow()+"\t\t"+OKMoves[i].getfromCol()+"\t\t"+OKMoves[i].gettoRow()+"\t\t"+OKMoves[i].gettoCol()+"\n"); } } attemptNo++; if (attemptNo+1==max) { System.out.println("Please select this move carefully.\n"); System.out.println("The game may be awarded to your opponent.\n"); } } while((validMove==0) && (attemptNo