Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So I have making a game called Tic Tac Toe and I want to know how can I check if the playerOne number is equal to the other players number

For example, Say player one inputs number 5.
Player 2 inputs number 5 again. So i want to do is, ask the user to input again because the number is taken by the other player.

this is my 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 replacee 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 1;
  }


  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 **********");            // doesn't 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("Your game will begin in");

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

    System.out.println("3");

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

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

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


    String [] Array = new String [9];     // Created a new string array


    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();
      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 [0] == Array[currentPlayer] && Array[3] == Array[currentPlayer] && Array[6] == 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] ||
             (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 [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]))))))))) {


        System.out.println("Player One Won");

        return -1;
      }



      System.out.println("Player #2: Please enter a number between 0 to 8");
      int otherPlayer = in.nextInt();
      Array [otherPlayer] = "O";



      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 (currentPlayer == otherPlayer){
        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";

      }

      if (Array [0] == Array[otherPlayer] && Array [1] == Array[otherPlayer] && Array [2] == Array[otherPlayer]||
          (Array [0] == Array[otherPlayer] && Array[3] == Array[otherPlayer] && Array[6] == 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]||
             (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 [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])) ))))))) {


        System.out.println("Player Two Won");

        return -1;

      }

    }
    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
Comments
Richard MacCutchan 17-May-15 3:10am    
Just check the cell where you store the Os and Xs. If it is free then the number is valid, if it is already used then ask the user to try again.

1 solution

The main problem is the design of your class. You should not make all methods static. To design a class make an outline listing the variables and methods you'll need. Wherever you might repeat code e.g print the game grid make a separate private method.
Such as:

Java
public class TicTacToe
{
private boolean player1turn;
private int score;
private char[9] grid;

// methods
public void Intro();
public void StartNewGame();
public int GetPlayerChoice();
private void ResetGrid();
private void UpdateGrid();
private void ShowGrid();
private int CheckForWinner();

}
 
Share this answer
 

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