tring [] Array = new String [9]; // Created a new string array which can store up to 9 values. for (int i = 0; i < 9; i++){ Array [i] = " "; } for (int i = 0; i < 9; i++){ // Asking Player 1 for a number between 0-8. The number will then be stored // into the variable called otherPlayer which has an assigned value of O. System.out.println("Player #1: Please enter a number between 0 to 8"); int currentPlayer = in.nextInt(); Array [currentPlayer] = "X"; // This is an imnporntant code. All the Array represetns a specific number. // So, when the playerTwo inputs a number, the number gets stored in otherPlayer. // It will then be then put into an array. System.out.println(" | | "); System.out.println(" "+Array[0]+" | " +Array[1]+" | "+Array[2]+" "); System.out.println(" | | "); System.out.println(" -------------------------------"); System.out.println(" | | "); System.out.println(" "+Array[3]+" | " +Array[4]+" | "+Array[5]+" "); System.out.println(" | | "); System.out.println(" -------------------------------"); System.out.println(" | | "); System.out.println(" "+Array[6]+" | " +Array[7]+" | "+Array[8]+" "); System.out.println(" | | "); // This code basically checks for all the combinations of wining Tic Tac Toe. // For example, the first line will only be true when the user inputs Number 0,1 and 2. // The program will check for every combinations and then will tell the user if playerTwo has won yet. if (Array [0] == Array[currentPlayer] && Array [1] == Array[currentPlayer] && Array [2] == Array[currentPlayer]|| (Array [3] == Array[currentPlayer] && Array[4] == Array[currentPlayer] && Array[5] == Array[currentPlayer] || (Array [6] == Array[currentPlayer] && Array[7] == Array[currentPlayer] && Array[8] == Array[currentPlayer]|| (Array [0] == Array[currentPlayer] && Array[3] == Array[currentPlayer] && Array[6] == Array[currentPlayer] || (Array [1] == Array[currentPlayer] && Array[4] == Array[currentPlayer] && Array[7] == Array[currentPlayer] || (Array [2] == Array[currentPlayer] && Array[5] == Array[currentPlayer] && Array[8] == Array[currentPlayer] || (Array [0] == Array[currentPlayer] && Array[4] == Array[currentPlayer] && Array[8] == Array[currentPlayer] || (Array [2] == Array[currentPlayer] && Array[4] == Array[currentPlayer] && Array[6] == Array[currentPlayer] )))))))) { System.out.println("Player One Won"); return -1; }else if(!Array[0].equals(Array[currentPlayer]) && Array[1].equals(Array[currentPlayer]) && Array[2].equals(Array[currentPlayer])|| (!Array[3].equals(Array[currentPlayer]) && Array[4].equals(Array[currentPlayer]) && Array[5].equals(Array[currentPlayer])|| (!Array[6].equals(Array[currentPlayer]) && Array[7].equals(Array[currentPlayer]) && Array[8].equals(Array[currentPlayer])|| (!Array[0].equals(Array[currentPlayer]) && Array[3].equals(Array[currentPlayer]) && Array[6].equals(Array[currentPlayer])|| (!Array[1].equals(Array[currentPlayer]) && Array[4].equals(Array[currentPlayer]) && Array[7].equals(Array[currentPlayer])|| (!Array[2].equals(Array[currentPlayer]) && Array[5].equals(Array[currentPlayer]) && Array[8].equals(Array[currentPlayer])|| (!Array[0].equals(Array[currentPlayer]) && Array[4].equals(Array[currentPlayer]) && Array[8].equals(Array[currentPlayer])|| (!Array[2].equals(Array[currentPlayer]) && Array[4].equals(Array[currentPlayer]) && Array[6].equals(Array[currentPlayer]) )))))))){ System.out.println("Tie Game"); return -1; } // Asking Player 2 for a number between 0-8. The number will then be stored // into the variable called otherPlayer which has an assigned value of O. System.out.println("Player #2: Please enter a number between 0 to 8"); int otherPlayer = in.nextInt(); Array [otherPlayer] = "O"; // This is the error checking code. So if playerTwo enters // the same number as playerOne, then playerTwo will be asked again. // Then, the number will be stored into variable called different number // which will have the value of O. if (otherPlayer == currentPlayer){ System.out.println("Sorry, The number you have entered is taken by the other player"); System.out.println("Player #2: Please Enter another number between 0-8"); int anotherNumber = in.nextInt(); Array [anotherNumber] = "O"; Array [currentPlayer] = "X"; } // This is an imnporntant code. All the Array represetns a specific number. // So, when the playerTwo inputs a number, the number gets stored in otherPlayer. // It will then be then put into an array. System.out.println(" | | "); System.out.println(" "+Array[0]+" | " +Array[1]+" | "+Array[2]+" "); System.out.println(" | | "); System.out.println(" -------------------------------"); System.out.println(" | | "); System.out.println(" "+Array[3]+" | " +Array[4]+" | "+Array[5]+" "); System.out.println(" | | "); System.out.println(" -------------------------------"); System.out.println(" | | "); System.out.println(" "+Array[6]+" | " +Array[7]+" | "+Array[8]+" "); System.out.println(" | | "); // This code basically checks for all the combinations of wining Tic Tac Toe. // For example, the first line will only be true when the user inputs Number 0,1 and 2. // The program will check for every combinations and then will tell the user if playerTwo has won yet. if (Array [0] == Array[otherPlayer] && Array [1] == Array[otherPlayer] && Array [2] == Array[otherPlayer]|| (Array [3] == Array[otherPlayer] && Array[4] == Array[otherPlayer] && Array[5] == Array[otherPlayer]|| (Array [6] == Array[otherPlayer] && Array[7] == Array[otherPlayer] && Array[8] == Array[otherPlayer]|| (Array [0] == Array[otherPlayer] && Array[3] == Array[otherPlayer] && Array[6] == Array[otherPlayer]|| (Array [1] == Array[otherPlayer] && Array[4] == Array[otherPlayer] && Array[7] == Array[otherPlayer]|| (Array [2] == Array[otherPlayer] && Array[5] == Array[otherPlayer] && Array[8] == Array[otherPlayer]|| (Array [0] == Array[otherPlayer] && Array[4] == Array[otherPlayer] && Array[8] == Array[otherPlayer]|| (Array [2] == Array[otherPlayer] && Array[4] == Array[otherPlayer] && Array[6] == Array[otherPlayer] )))))))) { System.out.println("Player Two Won"); return -1; }else if(!Array[0].equals(Array[otherPlayer]) && Array[1].equals(Array[otherPlayer]) && Array[2].equals(Array[otherPlayer])|| (!Array[3].equals(Array[otherPlayer]) && Array[4].equals(Array[otherPlayer]) && Array[5].equals(Array[otherPlayer])|| (!Array[6].equals(Array[otherPlayer]) && Array[7].equals(Array[otherPlayer]) && Array[8].equals(Array[otherPlayer])|| (!Array[0].equals(Array[otherPlayer]) && Array[3].equals(Array[otherPlayer]) && Array[6].equals(Array[otherPlayer])|| (!Array[1].equals(Array[otherPlayer]) && Array[4].equals(Array[otherPlayer]) && Array[7].equals(Array[otherPlayer])|| (!Array[2].equals(Array[otherPlayer]) && Array[5].equals(Array[otherPlayer]) && Array[8].equals(Array[otherPlayer])|| (!Array[0].equals(Array[otherPlayer]) && Array[4].equals(Array[otherPlayer]) && Array[8].equals(Array[otherPlayer])|| (!Array[2].equals(Array[otherPlayer]) && Array[4].equals(Array[otherPlayer]) && Array[6].equals(Array[otherPlayer]) )))))))){ System.out.println("Tie Game"); return -1; } } return 1; }
Array [0] == Array[otherPlayer]
.equals
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)