//////////////////////////////////////////////////////////////////////////////// // Author: Julie Petrusa (991339). // // Due date: June 18, 2001. // // This program implements the game 'Duper Tac Toe' (written in Java). // // In this game, 1 point is awarded for each 3 in a row, 2 points for each 4 // // in a row and 3 points for each 5 in a row. // // The person with the most points wins the game. // // Rule: the first move cannot occupy the center square. // // Known bugs: none // //////////////////////////////////////////////////////////////////////////////// import java.io.*; // needed in order to do I/O class Board { //--------------------------------------------------------------------------- // fields // -Representation of Game Board: 2D char array (5 X 5) // -Representation of Playing Pieces: '#' = empty square // 'X' = player 1 // 'O' = player 2 private char[][] boardArray; // board is represented by 2D char array final int size = 5; // size of board is (5 X 5) //--------------------------------------------------------------------------- // constructors public Board(char[][] Pboard) { boardArray = new char[size][size]; // create the board for (int i=0; i 4 || row < 0 || col > 4 || col < 0) { System.out.println(""); System.out.print("That is not a legal move because "); System.out.println("the row and column indexes are from 0 to 4"); System.out.println("so please try again!"); System.out.println(""); return false; } if (row == 2 && col == 2 && counterPB == 0) { System.out.println(""); System.out.print("That is not a legal move because "); System.out.print("the first move cannot occupy the Center "); System.out.println("Square so please try again!"); System.out.println(""); return false; } if (boardArrayP[row][col] == '#') return true; else { System.out.println(""); System.out.print("That is not a legal move because "); System.out.println("that square is already occupied"); System.out.println("so please try again!"); System.out.println(""); return false; } } //--------------------------------------------------------------------------- // method move // -adds an 'X' or 'O' to the board public void move(int row, int col) { char XorO; if (counterPB % 2 == 0) XorO = 'X'; else XorO = 'O'; boardArrayP[row][col] = XorO; // add 'X' or 'O' counterPB++; // increment counter } //--------------------------------------------------------------------------- // method win // -determines if the game is over // -returns true if the board is full, false otherwise // -displays a message if the game is over public boolean win() { if (counterPB >= 25) { System.out.println(""); System.out.println(""); System.out.println("GAME OVER!!!"); return true; } else return false; } //--------------------------------------------------------------------------- // method winner // -displays the user's and the computer's total points // -displays the winner public void winner() { Board newBoard = new Board(boardArrayP); int points1 = newBoard.totalPoints('X'); // total points for user System.out.println("Player 1 has " + points1 + " points"); int points2 = newBoard.totalPoints('O'); // total points for comp System.out.println("Player 2 has " + points2 + " points"); if (points1 - points2 > 0) // find winner System.out.println("Player 1 wins!"); else System.out.println("Player 2 wins!"); } //--------------------------------------------------------------------------- // method toString // -displays the board public String toString() { String display = ""; display = " 01234" + "\n"; for (int i=0; i Play Duper Tac Toe"); System.out.println(" 2 --> Quit"); System.out.print("Answer: "); // prompt user for answer System.out.flush(); int answer = getInt(); // read in the user's answer switch (answer) // do what the user wants { case 1: PlayBoard PBoard = new PlayBoard(); // initialize while (!PBoard.win()) // while game not over, { System.out.println(""); System.out.println(""); System.out.println("Player 1 --> 'X'"); System.out.println("Player 2 --> 'O'"); System.out.println(""); System.out.println(""); System.out.println(PBoard.toString()); // display System.out.println(""); int row = 0; int col = 0; do { System.out.println("Your move:"); System.out.print("Row: "); // prompt user System.out.flush(); row = getInt(); // read in user's move System.out.print("Col: "); // prompt user System.out.flush(); col = getInt(); // read in user's move } while (!PBoard.legalMove(row, col)); PBoard.move(row, col); // update } System.out.println(""); System.out.println(""); System.out.println(PBoard.toString()); // display System.out.println(""); PBoard.winner(); // display total points and winner break; case 2: value = false; // to end do-while loop break; default: // invalid answer, display error message System.out.println(""); System.out.println("Invalid answer!"); break; } // end switch } while (value != false); System.in.read(); // to view console } // end try catch (IOException e) { System.out.println("IOException"); } // end catch } //--------------------------------------------------------------------------- /* method getString -reads in a string -precondition: -the user has typed in a valid string -postcondition: -if valid string, string is read in -if not valid string, throws IOException -return: -static */ public static String getString() throws IOException { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String s = br.readLine(); return s; } //--------------------------------------------------------------------------- /* method getInt -reads in an integer -precondition: -the user has typed in a valid integer -postcondition: -if valid integer, integer is read in -if not valid integer, throws IOException -return: -static */ public static int getInt() throws IOException { String s = getString(); return Integer.parseInt(s); } //--------------------------------------------------------------------------- } // end class DuperTacToe2