/* A class for playing the Dots and Boxes game.*/ public class DotsAndBoxes { /* The game board. */ private EditGrid playingBoard; /* Booleans for controlling game play loops. */ private boolean turnOver; private boolean gameOver; /* For keeping track of the players' scores. */ private int scoreOfA; private int scoreOfB; /* * For keeping track of whose turn it is. This will take value 'A' * for player A's turn and 'B' for player B's turn. */ private char playerToMove; /* To keep track of number of boxes completed for determining end of game. */ private int numBoxesCompleted = 0; /* * The main method creates a game of the dimensions passed by the * command-line arguments. */ public static void main(String[] args) { DotsAndBoxes theGame = new DotsAndBoxes( Integer.parseInt(args[0]), Integer.parseInt(args[1])); theGame.playGame(); } /* * The constructor creates a game board of the given size, starts * with A's turn to move, and initializes the instance variables. */ public DotsAndBoxes(int numRows, int numCols) { playingBoard = new EditGrid(numRows, numCols); gameOver = false; playerToMove = 'A'; scoreOfA = 0; scoreOfB = 0; } /* * This method runs a loop for the two players to take turns * until the game is over. */ public void playGame() { System.out.println(); while (!gameOver) { turnOver = false; while (!turnOver) { printScores(); playingBoard.printGridContents(); printPrompt(); playingBoard.processCommand(); if (!completedSquare(playingBoard.getLastEdgeCoords())) { turnOver = true; togglePlayers(); } else { if (completedBoard()) { turnOver = true; gameOver = true; printScores(); playingBoard.printGridContents(); printResult(); } } } } } /* Prints out the scores of the two players. */ public void printScores() { System.out.println("Scores A: " + scoreOfA + " B: " + scoreOfB); System.out.println(); } /* Prints out a command prompt. */ public void printPrompt() { System.out.println(); System.out.print(playerToMove + "'s "); playingBoard.promptUser(); } /* Switches whose turn to move it is. */ public void togglePlayers() { switch(playerToMove) { case 'A': playerToMove = 'B'; break; case 'B': playerToMove = 'A'; break; } } /* Checks whether the given move has completed a square. */ public boolean completedSquare(int[] coords) { switch(playingBoard.getGridContents(coords[0], coords[1])) { case '_': { boolean above = completedSquareAbove(coords); boolean below = completedSquareBelow(coords); return (above || below); } case '|': { boolean toLeft = completedSquareToLeft(coords); boolean toRight = completedSquareToRight(coords); return (toLeft || toRight); } default: return false; // Necessary for the paranoid java compiler } } /* Checks whether there is a square above the given position. */ public boolean completedSquareAbove(int[] coords) { int row = coords[0]; int col = coords[1]; if ((playingBoard.getGridContents(row - 1, col - 1) == '|') && (playingBoard.getGridContents(row - 2, col) == '_') && (playingBoard.getGridContents(row - 1, col + 1) == '|')) { numBoxesCompleted++; incrementScore(playerToMove); playingBoard.setGridContents(row - 1, col, playerToMove); return true; } else { return false; } } /* Checks whether there is a square below the given position. */ public boolean completedSquareBelow(int[] coords) { int row = coords[0]; int col = coords[1]; if (row > playingBoard.numRows()*2 - 2) { return false; } if ((playingBoard.getGridContents(row + 1, col - 1) == '|') && (playingBoard.getGridContents(row + 2, col) == '_') && (playingBoard.getGridContents(row + 1, col + 1) == '|')) { numBoxesCompleted++; incrementScore(playerToMove); playingBoard.setGridContents(row + 1, col, playerToMove); return true; } return false; } /* Checks whether there is a square to the left of the given position. */ public boolean completedSquareToLeft(int[] coords) { int row = coords[0]; int col = coords[1]; if ((playingBoard.getGridContents(row - 1, col - 1) == '_') && (playingBoard.getGridContents(row, col - 2) == '|') && (playingBoard.getGridContents(row + 1, col - 1) == '_')) { numBoxesCompleted++; incrementScore(playerToMove); playingBoard.setGridContents(row, col - 1, playerToMove); return true; } else { return false; } } /* Checks whether there is a square to the right of the given position. */ public boolean completedSquareToRight(int[] coords) { int row = coords[0]; int col = coords[1]; if (col > playingBoard.numCols()*2 - 1) { return false; } if ((playingBoard.getGridContents(row - 1, col + 1) == '_') && (playingBoard.getGridContents(row, col + 2) == '|') && (playingBoard.getGridContents(row + 1, col + 1) == '_')) { numBoxesCompleted++; incrementScore(playerToMove); playingBoard.setGridContents(row, col + 1, playerToMove); return true; } else { return false; } } /* Prints out the result of the game. */ public void printResult() { System.out.println(); if (scoreOfA < scoreOfB) { System.out.println("B wins."); } else if (scoreOfA == scoreOfB) { System.out.println("A and B are tied."); } else { System.out.println("A wins."); } } /* Checks whether every box in the board has been completed. */ public boolean completedBoard() { return (numBoxesCompleted == (playingBoard.numRows() - 1)*(playingBoard.numCols() - 1)); } /* Increases the score of the given player. */ private void incrementScore(char playerToMove) { switch(playerToMove) { case 'A': scoreOfA++; break; case 'B': scoreOfB++; break; } } }