public class TicTacToe { //=================================================================================== //This function holds the main game loop for Tic Tac Toe. //=================================================================================== public static void main(String[] args) { String v1 = " "; String v2 = " "; String v3 = " "; String v4 = " "; String v5 = " "; String v6 = " "; String v7 = " "; String v8 = " "; String v9 = " "; String turn = "X"; displayBoard(v1, v2, v3, v4, v5, v6, v7, v8, v9); while(true) { int choice = humanPlayerTurn(); if (choice == 1) { v1 = turn; } else if (choice == 2) { v2 = turn; } else if (choice == 3) { v3 = turn; } else if (choice == 4) { v4 = turn; } else if (choice == 5) { v5 = turn; } else if (choice == 6) { v6 = turn; } else if (choice == 7) { v7 = turn; } else if (choice == 8) { v8 = turn; } else if (choice == 9) { v9 = turn; } displayBoard(v1, v2, v3, v4, v5, v6, v7, v8, v9); turn = swapTurn(turn); } //end of while loop } //=================================================================================== //This function gets the 9 board values and displays them and the board to screen. //=================================================================================== public static void displayBoard(String s1, String s2, String s3, String s4, String s5, String s6, String s7, String s8, String s9) { System.out.println(" * * "); System.out.println(" " + s1 + " * " + s2 + " * " + s3 + " "); System.out.println(" * * "); System.out.println("***********"); System.out.println(" * * "); System.out.println(" " + s4 + " * " + s5 + " * " + s6 + " "); System.out.println(" * * "); System.out.println("***********"); System.out.println(" * * "); System.out.println(" " + s7 + " * " + s8 + " * " + s9 + " "); System.out.println(" * * "); } //=================================================================================== //This function asks the human (via screen) to choose a square and returns that value. //No checking whether the value is legal or if that square is taken. //=================================================================================== public static int humanPlayerTurn() { Scanner scr = new Scanner(System.in); System.out.println("Where do you want to go?"); int myChoice = scr.nextInt(); return myChoice; } //=================================================================================== //This function simply returns the opposite of the currentTurn. So if it is "X", this //function returns "O". If it is "O", this function returns "X". //=================================================================================== public static String swapTurn(String currentTurn) { String nextTurn; if (currentTurn.equals("X")) { nextTurn = "O"; } else //if (currentTurn.equals("O")) { nextTurn = "X"; } return nextTurn; } //=================================================================================== }