import java.util.*; public class TTT { public static void main(String[] args) { Scanner in=new Scanner(System.in); char[] board = new char[9]; int i, turn=0; char won = 'n'; boolean legal = false; for(i=0;i<9;i++) board[i] = ' '; // start off with all grid entries empty do { do { legal = false; // assume the user's input will be illegal until they get it right // print the board System.out.println(board[0]+"|"+board[1]+"|"+board[2]); System.out.println("-----"); System.out.println(board[3]+"|"+board[4]+"|"+board[5]); System.out.println("-----"); System.out.println(board[6]+"|"+board[7]+"|"+board[8]); // get the user's square (0-8) System.out.print("Enter your square (0-8) "); i=in.nextInt(); // verify that i is within bound (0-8) and board[i] is currently empty, if so, legal becomes true if(i>=0&&i<=8&&board[i]==' ') legal=true; // otherwise legal remains false and the userh as to try again }while(!legal); board[i]='X'; // insert 'X' into this square turn++; // turn is used to determine if there is a draw, add 1 to turn now that the user has placed his latest 'X' // check for a win if((board[0]==board[1]&&board[1]==board[2]&&board[0]!=' ')|| // first row (board[3]==board[4]&&board[4]==board[5]&&board[3]!=' ')|| // second row (board[6]==board[7]&&board[7]==board[8]&&board[6]!=' ')|| // third row (board[0]==board[3]&&board[3]==board[6]&&board[0]!=' ')|| // first column (board[1]==board[4]&&board[4]==board[7]&&board[1]!=' ')|| // second column (board[2]==board[5]&&board[5]==board[8]&&board[2]!=' ')|| // third column (board[0]==board[4]&&board[4]==board[8]&&board[0]!=' ')|| // first diagonal (board[2]==board[4]&&board[4]==board[6]&&board[2]!=' ')) // second diagonal won='X'; // if any of these are true, 'X' has won else if(won=='n'&&turn==9) won='T'; // else if 'X' has not won (won is still 'n') and turn is 9, game ends in a draw else // otherwise its 'O's turn, basically repeat the above { do { // 'O's turn legal=false; // reset legal to false System.out.println(board[0]+"|"+board[1]+"|"+board[2]); // print the board System.out.println("-----"); System.out.println(board[3]+"|"+board[4]+"|"+board[5]); System.out.println("-----"); System.out.println(board[6]+"|"+board[7]+"|"+board[8]); System.out.print("Enter your square (0-8) "); i=in.nextInt(); // get O's square and verify it if(i>=0&&i<=8&&board[i]==' ') legal=true; }while(!legal); board[i]='O'; // insert O turn++; // add 1 to turn for draw checking if((board[0]==board[1]&&board[1]==board[2]&&board[0]!=' ')|| // check if O has won (board[3]==board[4]&&board[4]==board[5]&&board[3]!=' ')|| (board[6]==board[7]&&board[7]==board[8]&&board[6]!=' ')|| (board[0]==board[3]&&board[3]==board[6]&&board[0]!=' ')|| (board[1]==board[4]&&board[4]==board[7]&&board[1]!=' ')|| (board[2]==board[5]&&board[5]==board[8]&&board[2]!=' ')|| (board[0]==board[4]&&board[4]==board[8]&&board[0]!=' ')|| (board[2]==board[4]&&board[4]==board[6]&&board[2]!=' ')) won='O'; // and if so, set won to 'O' if(turn==9) won='T'; // this should never happen here because 'O' will never cause a draw } } while(won=='n'); // repeat the game until either someone has won (won=='X' or won=='O') or there is a draw (won=='T') System.out.println(board[0]+"|"+board[1]+"|"+board[2]); // output final board System.out.println("-----"); System.out.println(board[3]+"|"+board[4]+"|"+board[5]); System.out.println("-----"); System.out.println(board[6]+"|"+board[7]+"|"+board[8]); if(won=='X') System.out.println("X wins!"); // output who won else if(won=='N') System.out.println("O wins!"); else System.out.println("Its a draw!"); } }