Click here to Skip to main content
15,442,578 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.


C#
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);                          //Waits 1.5 Seconds
    }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) {                       //Waits 1.5 Seconds
      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) {                           //Waits 1.5 Seconds
      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) {                           //Waits 1.5 Seconds
      System.out.println("Error");
    }

    System.out.println("So, Would You Like to play Tic Tac Toe?");       // The user will have two options.
    System.out.println("Press 1 for yes and 2 for No");                 //  If the user wants to play, Press 1
    int answer = in.nextInt();                                         // If the user doesn't want to play then press 2
    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");  // This is the part of the code, when the user
    System.out.println("******** THE END **********");            // decides not want to play Tic Tac Toe.

    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) {                       //Waits 1 Seconds
      System.out.println("Error");
    }

    System.out.println("Loading.....");

    try {
      Thread.sleep(1000);
    }catch (Exception e) {                       //Waits 1 Seconds
      System.out.println("Error");
    }

    System.out.println("Loading.....");

    try {
      Thread.sleep(1000);
    }catch (Exception e) {                       //Waits 1 Seconds
      System.out.println("Error");
    }

    System.out.println("Loading.....");

    try {
      Thread.sleep(2000);
    }catch (Exception e) {                       //Waits 1 Seconds
      System.out.println("Error");
    }




    String [] 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();

      if (currentPlayer >8 || currentPlayer <0){
        System.out.println("Please enter a valid number");
        return 2;
      }

      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("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;
      }


      // 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;

      } 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){ // Main Method

    int finish = 1;

    while(finish != -1){

      if(finish == 1){
        finish = Introduction();

      }if (finish == 2){
        finish = playTime();
      }if (finish == 3){
        finish = notPlaying();
      }
    }
  }
}
Posted

1 solution

Your 'error checking code' doesn't work:
- You only compare the input to the number last chosen by the previous player; it would be possible to enter a number that was already chosen earlier.
- What if you enter an invalid (already taken) number twice?

Ideally you should:
- Have a method where you put Thread.sleep(..) into with the try-catch-block and call this method in those places instead so you don't repeat the same code numerous times.
- Only have the code for the move of any player in playTime() and call it alternatingly for player X and player O.
- Use two nested loops for checking all horizontals and verticals for 3-in-a-row plus checking the two diagonals either also with loops or (because there are only two) with "static code"; instead of checking all with static code.

How to determine a tie?
You have a tie if, after turn 9 is done, there's no winner; that is, if your check if the current player has won, returns false.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 19-May-15 22:11pm     CRLF
Good points, a 5. The code is hopeless enough; in such cases, I usually advise to write it from scratch. Writing new version of code from scratch is highly undervalued idea... —SA
Sascha Lefèvre 19-May-15 22:47pm    
Thank you, Sergey. I agree on the value of rewriting code - but it can result in an empty fridge if you overdo it ;-)
Sergey Alexandrovich Kryukov 19-May-15 23:13pm     CRLF
Empty fridge? Then someone can earn for living doing something else, the sooner the better. Often, not code is better. —SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900