Well Mine Works. It tells who the winner is. But the problem is when we play tic tac toe there will always be a tie. So I am trying to figure it out how I can check if all the 9 turns are used then print tie game.
To make it more clear let me give you an example.
Say you're playerOne and i am player Two.
We are playing Tic Tac Toe.
U enter a number 0. Then I enter 1. Then u enter 2. Then I enter 3 and so on.....
After we've filed all the boxes of tic tac toe and there is no winner then it is a tie game.
So my question to you is: What do I have to do in order to check if all the boxes are full and there is no winner so we can have a Tie Game. '
If you need mine code. Here is the official and full code.
import java.util.Scanner;
public class TicTacToeOffical2{
public static Scanner in = new Scanner(System.in);
public static int Introduction(){
System.out.println("Hey");
System.out.println("Today, you will play the game of Tic Tac Toe");
System.out.println("So, let me go over few rules");
System.out.println("Rule #1: This is how the Tic Tac Toe board will look like. ");
try {
Thread.sleep(1500);
}catch (Exception e) {
System.out.println("Error");
}
System.out.println(" | | ");
System.out.println(" 0 | 1 | 2 ");
System.out.println(" | | ");
System.out.println(" -------------------------------");
System.out.println(" | | ");
System.out.println(" 3 | 4 | 5 ");
System.out.println(" | | ");
System.out.println(" -------------------------------");
System.out.println(" | | ");
System.out.println(" 6 | 7 | 8 ");
System.out.println(" | | ");
try {
Thread.sleep(1500);
}catch (Exception e) {
System.out.println("Error");
}
System.out.println("Rule #2: Choose a number to place your variable X or O");
System.out.println("Lets say that you choose to be X and you choose the number 4");
System.out.println("So now, the number 4 will be replaced with X");
System.out.println(" | | ");
System.out.println(" 0 | 1 | 2 ");
System.out.println(" | | ");
System.out.println(" -------------------------------");
System.out.println(" | | ");
System.out.println(" 3 | X | 5 ");
System.out.println(" | | ");
System.out.println(" -------------------------------");
System.out.println(" | | ");
System.out.println(" 6 | 7 | 8 ");
System.out.println(" | | ");
try {
Thread.sleep(2000);
}catch (Exception e) {
System.out.println("Error");
}
System.out.println("Rule #3: To win the game, you have to get 3 X or O in a row ");
System.out.println(" | | ");
System.out.println(" 0 | X | 2 ");
System.out.println(" | | ");
System.out.println(" -------------------------------");
System.out.println(" | | ");
System.out.println(" 3 | X | 5 ");
System.out.println(" | | ");
System.out.println(" -------------------------------");
System.out.println(" | | ");
System.out.println(" 6 | X | 8 ");
System.out.println(" | | ");
try {
Thread.sleep(1500);
}catch (Exception e) {
System.out.println("Error");
}
System.out.println("So, Would You Like to play Tic Tac Toe?");
System.out.println("Press 1 for yes and 2 for No");
int answer = in.nextInt();
if (answer == 1){
return 2;
}else if (answer == 2){
return 3;
}
return 2;
}
private static int notPlaying(){
System.out.println("So you decided not to play Tic Tac Toe");
System.out.println("******** THE END **********");
return -1;
}
private static int playTime(){
System.out.println("So, Are you ready to play Tic Tac Toe?");
System.out.println("Player #1 will be X");
System.out.println("Player #2 will be O");
System.out.println("Please wait while your Game is loading........");
try {
Thread.sleep(1000);
}catch (Exception e) {
System.out.println("Error");
}
System.out.println("Loading.....");
try {
Thread.sleep(1000);
}catch (Exception e) {
System.out.println("Error");
}
System.out.println("Loading.....");
try {
Thread.sleep(1000);
}catch (Exception e) {
System.out.println("Error");
}
System.out.println("Loading.....");
try {
Thread.sleep(2000);
}catch (Exception e) {
System.out.println("Error");
}
String [] Array = new String [9];
for (int i = 0; i < 9; i++){
Array [i] = " ";
}
for (int i = 0; i < 9; i++){
System.out.println("Player #1: Please enter a number between 0 to 8");
int currentPlayer = in.nextInt();
if (currentPlayer >8 || currentPlayer <0){
System.out.println("Please enter a valid number");
return 2;
}
Array [currentPlayer] = "X";
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(" | | ");
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("X") && Array[1].equals("X") && Array[2].equals("X")||
(!Array[3].equals("X") && Array[4].equals("X") && Array[5].equals("X")||
(!Array[6].equals("X") && Array[7].equals("X") && Array[8].equals("X")||
(!Array[0].equals("X") && Array[3].equals("X") && Array[6].equals("X")||
(!Array[1].equals("X") && Array[7].equals("X") && Array[7].equals("X")||
(!Array[2].equals("X") && Array[5].equals("X") && Array[8].equals("X")||
(!Array[0].equals("X") && Array[4].equals("X") && Array[8].equals("X")||
(!Array[2].equals("X") && Array[4].equals("X") && Array[6].equals("X")
)))))))){
System.out.println("Tie Game");
return -1;
}
System.out.println("Player #2: Please enter a number between 0 to 8");
int otherPlayer = in.nextInt();
Array [otherPlayer] = "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";
}
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(" | | ");
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;
} if (!Array[0].equals("O") && Array[1].equals("O") && Array[2].equals("O")||
(!Array[3].equals("O") && Array[4].equals("O") && Array[5].equals("O")||
(!Array[6].equals("O") && Array[7].equals("O") && Array[8].equals("O")||
(!Array[0].equals("O") && Array[3].equals("O") && Array[6].equals("O")||
(!Array[1].equals("O") && Array[7].equals("O") && Array[7].equals("O")||
(!Array[2].equals("O") && Array[5].equals("O") && Array[8].equals("O")||
(!Array[0].equals("O") && Array[4].equals("O") && Array[8].equals("O")||
(!Array[2].equals("O") && Array[4].equals("O") && Array[6].equals("O")
)))))))){
System.out.println("Tie Game");
System.out.println("To Play Again, Please Press 1");
int answer = in.nextInt();
if (answer == 1) {
return 2;
}else{
return 3;
}
}
}
return 1;
}
public static void main(String[] args){
int finish = 1;
while(finish != -1){
if(finish == 1){
finish = Introduction();
}if (finish == 2){
finish = playTime();
}if (finish == 3){
finish = notPlaying();
}
}
}
}