/******************************* FILE HEADER ********************************** Program: Tic Tac Toe, version 1 File: Rules.java Main File: TicTacToe1.java Author: James D. Skrentny copyright 2000, all rights reserved Course: CS 302, Fall 2000, Lectures 1 & 2 Compiler: CodeWarrior IDE 4.0 (JDK 1.2) Platform: Windows NT 4.0 **************************** 80 columns wide *********************************/ /** * The Rules class is used to verify that players are playing by the * rules for the game Tic Tac Toe. * * Bugs: none known **/ /* * The following concepts are illustrated: * - public class methods (see checkMove and checkWin) * - private class method (see win) * - sequenced if statements (see checkMove and checkWin) * - simple conditions (see checkMove and checkWin) * - conditions using messages (see checkMove and checkWin) * - complex conditions (see checkWin) */ public class Rules { /** * Checks if a specified move is valid for the specified board. If the move * isn't valid, an error message is displayed and the program is terminated. * * @param move the specified move to be checked * @param board the specified board on which the move would be made **/ public static void checkMove (Move move, Board board) { // check if row and column are valid int row = move.getRow(); int col = move.getCol(); boolean invalid = false; if (row < 1 || row > 3) { System.out.println("Invalid row: " + row); invalid = true; } if (col < 1 || col > 3) { System.out.println("Invalid column: " + col); invalid = true; } // terminate the program if invalid row or column if (invalid) { System.out.println("BYE!"); System.exit(1); } // check if cell is taken and terminate the program if so else if (!board.cellFree(move)) { System.out.println("Invalid Move, cell taken. BYE!"); System.exit(1); } } /** * Checks if a specified move results in a win for the specified mark * on the board. The row, column, and possibly a diagonal are checked for * three of the player's marks "in a row". * If there is a winner, the mark of the winner is displayed and the program * is terminated. * * @param move the specified move where the checking is to be based * @param board the board to be checked for a win * @param mark the mark of the player that is to be checked for a win **/ public static void checkWin (Board board, Move move, Mark mark) { int row = move.getRow(); int col = move.getCol(); // check if win in a row or column, and if so call win() if (board.checkRow(row, mark)) win(board, mark); if (board.checkCol(col, mark)) win(board, mark); // check if win along a diagonal, and if so call win() if (row == 1 && col == 1 || row == 2 && col == 2 || row == 3 && col == 3) if (board.checkDiag(1, mark)) win(board, mark); if (row == 1 && col == 3 || row == 2 && col == 2 || row == 3 && col == 1) if (board.checkDiag(2, mark)) win(board, mark); } /** * Displays the board with the win and terminates the program. * * @param board the board with a win * @param mark the winning player's mark **/ private static void win (Board board, Mark mark) { System.out.println("\n"); board.display(); System.out.println(mark + " wins! Game over. BYE!"); System.exit(0); } }