// Tic Tac Toe solution. // Date: 27 Feb 2001 // Author: Rimon Barr import java.io.*; class TicTacToe { // constants for player pieces and numbers private static final char PL1C='X', PL2C='O', EMPTY=' '; private static final int PL1N=1, PL2N=2; // board variables (row, col) private static char sq11,sq12,sq13, sq21,sq22,sq23, sq31,sq32,sq33; // input stream private static BufferedReader in= new BufferedReader(new InputStreamReader(System.in)); // display board public static void printBoard() { System.out.println("+---+---+---+"); System.out.println("| "+sq11+" | "+sq12+" | "+sq13+" |"); System.out.println("+---+---+---+"); System.out.println("| "+sq21+" | "+sq22+" | "+sq23+" |"); System.out.println("+---+---+---+"); System.out.println("| "+sq31+" | "+sq32+" | "+sq33+" |"); System.out.println("+---+---+---+"); } // reset the board public static void clearBoard() { sq11=EMPTY; sq12=EMPTY; sq13=EMPTY; sq21=EMPTY; sq22=EMPTY; sq23=EMPTY; sq31=EMPTY; sq32=EMPTY; sq33=EMPTY; } // determine whether position within board limits public static boolean isValidPos(String s) { return s!=null && s.length()==2 && s.charAt(0)>='1' && s.charAt(0)<='3' && s.charAt(1)>='1' && s.charAt(1)<='3'; } // convert string position representation to integer public static int convertStringPosToNumber(String sPos) { return 10*(sPos.charAt(0)-'0') + sPos.charAt(1)-'0'; } // return piece at board position public static char getBoard(int pos) { switch (pos) { case 11: return sq11; case 12: return sq12; case 13: return sq13; case 21: return sq21; case 22: return sq22; case 23: return sq23; case 31: return sq31; case 32: return sq32; case 33: return sq33; default: return EMPTY; } } // set a square on the board to given piece public static void setBoard(int pos, char piece) { switch (pos) { case 11: sq11=piece; break; case 12: sq12=piece; break; case 13: sq13=piece; break; case 21: sq21=piece; break; case 22: sq22=piece; break; case 23: sq23=piece; break; case 31: sq31=piece; break; case 32: sq32=piece; break; case 33: sq33=piece; break; } } // determine whether the board is filled public static boolean isBoardFull() { return sq11!=EMPTY && sq12!=EMPTY && sq13!=EMPTY && sq21!=EMPTY && sq22!=EMPTY && sq23!=EMPTY && sq31!=EMPTY && sq32!=EMPTY && sq33!=EMPTY; } // check if a given player has won public static boolean hasPlayerWon(char player) { // columns if (sq11==player && sq21==player && sq31==player) return true; if (sq12==player && sq22==player && sq32==player) return true; if (sq13==player && sq23==player && sq33==player) return true; // rows if (sq11==player && sq12==player && sq13==player) return true; if (sq21==player && sq22==player && sq23==player) return true; if (sq31==player && sq32==player && sq33==player) return true; // diagonals if (sq11==player && sq22==player && sq33==player) return true; if (sq31==player && sq22==player && sq13==player) return true; // exhausted all winning possibilities return false; } // determine if the game is over public static boolean isGameOver() { return isBoardFull() || hasPlayerWon(PL1C) || hasPlayerWon(PL2C); } // play one game public static boolean playGame() { System.out.println("New Game."); // variables for input and turn counter String input=""; int turnCounter=0; // reset board clearBoard(); // play game until its over or user asks to quit while (!isGameOver() && !input.equals("99")) { // display board printBoard(); // set pln and plc according to turn int pln; char plc; if (turnCounter%2==0) { plc=PL1C; pln=PL1N; } else { plc=PL2C; pln=PL2N; } // ask for input System.out.print("Player "+pln+" turn: "); try { input=in.readLine(); } catch (IOException e) { input="99"; } // check that position is valid if (!isValidPos(input)) { System.out.println("Invalid position."); continue; } int pos=convertStringPosToNumber(input); // check that position is empty if (getBoard(pos)!=EMPTY) { System.out.println("Position already taken. Please try again."); continue; } // put piece on board setBoard(pos, plc); // increment turn counter turnCounter++; } // determine how the game ended printBoard(); if (hasPlayerWon(PL1C)) System.out.println("Player "+PL1N+" won."); else if (hasPlayerWon(PL2C)) System.out.println("Player "+PL2N+" won."); else if (isBoardFull()) System.out.println("Draw."); else { System.out.println("Game not completed."); return false; // do not play another game } return true; // play another game } // program entry point public static void main(String[] args) { // introduction System.out.println("Welcome to Tic Tac Toe."); System.out.println("Please enter positions as 'rc' (r - row, c - col), or 99 to exit"); // play loop while(playGame()); // say goodbye System.out.println("Thanks for playing. Come again."); } }