本文介绍了如何检查用户号码是否等于其他玩家的输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我制作了一款名为Tic Tac Toe的游戏,我想知道如何检查playerOne号码是否等于其他玩家号码



For假设玩家一输入数字5.

玩家2再次输入数字5。所以我想做的是,要求用户再次输入,因为该号码是由其他玩家拍摄的。



这是我的代码。



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.

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

推荐答案

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();

}


这篇关于如何检查用户号码是否等于其他玩家的输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 03:17