/* The game of Tic Tac Toe using a 2-D array. Users will enter their choice as a 1-9 which must be * translated to a 0-2,0-2 coordinate value. Input will be data verified to make sure that the * grid location is within bounds and not currently used. This is a human-vs-human game. To make * it a human-vs-computer game, we have to change things up so that O is generated by computer logic. */ import java.util.Scanner; public class TicTacToe { public static void main(String[] args) { char[][] grid = new char[3][3]; // create the 3x3 grid and fill each location with ' ' for(int i=0;i<3;i++) for(int j=0;j<3;j++) grid[i][j] = ' '; // initialize tic tac toe grid to all blanks char winner = playGame(grid); // play the game and get the winner ('X', 'O' or ' ' for draw) display(grid); // display the final grid if(winner=='X') System.out.println("Congratulations, you win!"); // output the winner else if(winner=='O') System.out.println("Sorry, but I win!"); else System.out.println("How predictable, its a draw"); } // This method is the main engine for the game. It will repeat until either someone has won the game or // count is 9 (a draw) alternating 'X' and 'O' turns public static char playGame(char[][] g) { Scanner in = new Scanner(System.in); int count = 0; // count number of turns, if it becomes 9 its a draw char winner = ' '; // no one has won yet int choice; // user input choice of grid number to insert into (1-9) char turn = 'X'; // X goes first, alternate X and O while(winner!='X'&&winner!='O'&&count<9) // while no one has won and not yet a draw { display(g); // display the tic tac toe grid choice = getChoice(g, turn, in); // get user's choice, data verified g[(choice)/3][(choice)%3] = turn; // fill in the user's choice with the appropriate letter ('X' or 'O') winner = checkForWinner(g); // now see if anyone has won yet, checkForWinner returns 'X', 'O' or ' ' count++; // we have now done 1 more turn, count is used to determine draws if(turn=='X') turn = 'O'; // change the player from 'X' to 'O' or 'O' to 'X' else turn = 'X'; } return winner; } public static char checkForWinner(char[][] g) // there are 8 possible winners, we check to see if the character in any row, column or diagonal is the same and not ' ' { // if the character is ' ' then its not a winner because any ' ' entries are empty squares if(g[0][0]==g[0][1]&&g[0][1]==g[0][2]&&g[0][0]!=' ') return g[0][0]; // upon a match, return the character found in one of those squares else if(g[1][0]==g[1][1]&&g[1][1]==g[1][2]&&g[1][0]!=' ') return g[1][0]; else if(g[2][0]==g[2][1]&&g[2][1]==g[2][2]&&g[2][0]!=' ') return g[2][0]; else if(g[0][0]==g[1][0]&&g[1][0]==g[2][0]&&g[0][0]!=' ') return g[0][0]; else if(g[0][1]==g[1][1]&&g[1][1]==g[2][1]&&g[0][1]!=' ') return g[0][1]; else if(g[0][2]==g[1][2]&&g[1][2]==g[2][2]&&g[0][2]!=' ') return g[0][2]; else if(g[0][0]==g[1][1]&&g[1][1]==g[2][2]&&g[0][0]!=' ') return g[0][0]; else if(g[0][2]==g[1][1]&&g[1][1]==g[2][0]&&g[0][2]!=' ') return g[0][2]; else return ' '; // noone has won yet } public static void display(char[][] g) // display the 3x3 grid in a somewhat formatted way { System.out.println(" "+g[0][0]+" | "+g[0][1]+" | "+g[0][2]); System.out.println("-----------"); System.out.println(" "+g[1][0]+" | "+g[1][1]+" | "+g[1][2]); System.out.println("-----------"); System.out.println(" "+g[2][0]+" | "+g[2][1]+" | "+g[2][2]); } public static int getChoice(char[][]g, char t, Scanner in) // get the user's choice (1-9), data verify that the choice is legal, and return the value-1 so that { // we can adjust it to be "carved up" into [0-2][0-2] for array access int input; do { System.out.print("Its " + t + "'s turn, enter your grid coordinate from 1-9 "); input = in.nextInt(); }while(input<1||input>9||g[(input-1)/3][(input-1)%3]!=' '); // illegal if out of bounds (<1, >9) or the grid location is already filled (not blank) return input-1; // adjust the choice to be on a 0-8 scale instead of 1-9 for easier computation } }