Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This is my big Code. I am really sorry for this Big Code but I have to post the entire thing so you can help me out. I tried writing if this doesn't equal this then print tie game but for some reason it is not working propely.

Tie Game Code is:



C#
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;
  }
Posted
Comments
Sergey Alexandrovich Kryukov 18-May-15 18:05pm    
Why helping on the code which is not written in an accurate way? Repeated code, hard-coded immediate constants, all kind of dirt...
Why do you think someone would like to dig in it? You did not even bother to explain the games of the rule. I could guess, but I don't want to...
—SA
Jainam Patel 18-May-15 22:07pm    
Look buddy, I don't care who you are but If you want to help me then help and if you don't then why do you even bother to write all this.
Sergey Alexandrovich Kryukov 18-May-15 23:13pm    
I bother you to help you. You cannot get much help if you don't want to follow my advice. First, you need to write reasonable code, only then you can fix or improve it. Improving the code you have without rewriting it would lead you nowhere. Better listed to a good friendly advice. It's up to you, of course...
—SA
Fredrik Bornander 19-May-15 3:07am    
The code looks like it should work, are you saying it's not printing "Tie Game" if the board is filled up without a winner found?

One thing you could try to change is
Array [0] == Array[otherPlayer]
You shouldn't really be comparing strings like that, use the .equals method instead.
Jainam Patel 19-May-15 21:26pm    
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.


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



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