【问题标题】:Ask user if they wish to continue [closed]询问用户是否希望继续[关闭]
【发布时间】:2016-04-16 19:12:32
【问题描述】:

我编写了一个在命令行上玩井字游戏的程序,但我想添加一个功能,一旦游戏完成,它会询问用户是否想再次玩,如果他们输入 Y,那么它会再次玩如果他们输入 N 它的休息时间,但我不确定从哪里开始 do while 循环以及在哪里关闭它。

import java.util.Scanner;

public class test3
{
static int A1, A2, A3, B1, B2, B3, C1, C2, C3;

static Scanner sc = new Scanner(System.in);

public static void main(String[] args)
{
System.out.println();
System.out.println();

  String prompt = "Welcome to our X and O's game . Please take your first go: ";
    String playerMove = "";
    String cpuMove = "";
    boolean gameIsDecided = false;

   for (int i = 1; i <=9; i++)
    {
        //Gives the human player its identity(1) and sets out what happens if player wins

        playerMove = getMove(prompt);
        updateBoard(playerMove, 1);
        displayBoard();
        if (isGameDecided())
        {
            System.out.println("Congratulations , you have beaten me!!");
            gameIsDecided = true;
            break;
        }

      if (i < 9)
        {
            cpuMove = getCpuMove();
            System.out.println(cpuMove);
            updateBoard(cpuMove, 2);
            displayBoard();
            if (isGameDecided())
            {
                System.out.println("Unlucky , I win. Better luck next time :");
                gameIsDecided = true;
                break;
            }
            prompt = "Take your next go: ";
            i++;
        }
    }
    if (!gameIsDecided)
        System.out.println("So nothing can separate us , the game is a draw !!");
}

       public static String getMove(String prompt)
{
    String turn;
    System.out.print(prompt);
    do
    {
        turn = sc.nextLine();
        if (!isAcceptablePlay(turn))
        {
            System.out.println("Unfortunately this move isn't valid , please try again !!");
        }
    } while (!isAcceptablePlay(turn));
    return turn;
}

     public static boolean isAcceptablePlay(String turn)
{
    if (turn.equalsIgnoreCase("A1") & A1 == 0)
        return true;
    if (turn.equalsIgnoreCase("A2") & A2 == 0)
        return true;
    if (turn.equalsIgnoreCase("A3") & A3 == 0)
        return true;
    if (turn.equalsIgnoreCase("B1") & B1 == 0)
        return true;
    if (turn.equalsIgnoreCase("B2") & B2 == 0)
        return true;
    if (turn.equalsIgnoreCase("B3") & B3 == 0)
        return true;
    if (turn.equalsIgnoreCase("C1") & C1 == 0)
        return true;
    if (turn.equalsIgnoreCase("C2") & C2 == 0)
        return true;
    if (turn.equalsIgnoreCase("C3") & C3 == 0)
        return true;
    return false;
}

   public static void updateBoard(String turn, int player)
{
    if (turn.equalsIgnoreCase("A1"))
        A1 = player;
    if (turn.equalsIgnoreCase("A2"))
        A2 = player;
    if (turn.equalsIgnoreCase("A3"))
        A3 = player;
    if (turn.equalsIgnoreCase("B1"))
        B1 = player;
    if (turn.equalsIgnoreCase("B2"))
        B2 = player;
    if (turn.equalsIgnoreCase("B3"))
        B3 = player;
    if (turn.equalsIgnoreCase("C1"))
        C1 = player;
    if (turn.equalsIgnoreCase("C2"))
        C2 = player;
    if (turn.equalsIgnoreCase("C3"))
        C3 = player;
}


    public static void displayBoard()
{
    String row = "";
    System.out.println();

    row = " " + getSymbol(A1) + " | " + getSymbol(A2) + " | " + getSymbol(A3);
    System.out.println(row);
    System.out.println("-----------");

    row = " " + getSymbol(B1) + " | " + getSymbol(B2) + " | " + getSymbol(B3);
    System.out.println(row);
    System.out.println("-----------");

    row = " " + getSymbol(C1) + " | " + getSymbol(C2) + " | " + getSymbol(C3);
    System.out.println(row);
    System.out.println();
}

  public static String getSymbol(int square)
{
    if (square == 1)
        return "X";
    if (square == 2)
        return "O";
    return " ";
}
//This controls the Computer move . By mixing them up we make the computer more competitive
public static String getCpuMove()
{
    if (B2 == 0)
        return  "B2";

    if (A3 == 0)
        return  "A3";

    if (C2 == 0)
        return  "C2";

    if (B1 == 0)
        return  "B1";

    if (B3 == 0)
        return  "B3";

    if (C1 == 0)
        return  "C1";

    if (A1 == 0)
        return "A1";

    if (C3 == 0)
        return  "C3";
    if (A2 == 0)
        return "A2";
    return "";
}

//This contains all the possible winning combinations .
public static boolean isGameDecided()
{
    if (is3inARow(A1, A2, A3))
        return true;
    if (is3inARow(B1, B2, B3))
        return true;
    if (is3inARow(C1, C2, C3))
        return true;
    if (is3inARow(A1, B1, C1))
        return true;
    if (is3inARow(A2, B2, C2))
        return true;
    if (is3inARow(A3, B3, C3))
        return true;
    if (is3inARow(A1, B2, C3))
        return true;
    if (is3inARow(A3, B2, C1))
        return true;
    return false;
}

public static boolean is3inARow(int a, int b, int c)
{
    return ((a == b) & (a == c) & (a != 0));
}

}

【问题讨论】:

    标签: java arrays string methods do-while


    【解决方案1】:

    看来您可以自己编写代码,所以我不打算给您代码,而只是提供一些指导。

    首先不要把大部分游戏写在main方法里,放到它自己的方法里。也许将该方法称为“播放”并从主要方法中调用它。然后当 play() 完成时,在 main 方法的正下方添加一个 win() 调用。当游戏调用 win() 时,如果用户想再次玩游戏,则打印到控制台。然后添加:

    if (input.equals("Y")) {
        play();
    }
    

    您不需要 else,否则程序会中断,而这正是您想要的。

    您可以使用 do while 循环,但在我看来这是不必要的并且更复杂。

    【讨论】:

    • 我认为信息量恰到好处。
    • 这实际上是我在这里提出的另一个线程,我认识到使用一个 java 文件不是正确的方法,但我正在努力将其划分为单独的类我可以从主 java 文件调用
    • @Arran 好吧,您可以使用单独的类文件,但即使只使用单独的方法也可以。事实上,如果你把它放在单独的类文件中很麻烦,方法就是要走的路。拥有长类文件并不可怕,尤其是当它是一个非常简单的过程时。但是,如果您想从主类中的另一个类调用方法,则该方法必须是static。你可以这样打电话:ClassName.someMethod().
    【解决方案2】:

    在这种情况下你只能做一个大的编辑

    int main(...)
    { 
        char exit = 'N';
        do {
            //your code here        
        exit = System.in.read();
        } while(exit != 'Y');
    }
    

    【讨论】:

      【解决方案3】:
      import java.util.Scanner;
      
      public class test3
      {
      static int A1, A2, A3, B1, B2, B3, C1, C2, C3;
      
      static Scanner sc = new Scanner(System.in);
      
      public static void main(String[] args)
      {
          new test3().starttPlaying();
      
      }
      
      
          String prompt = "Welcome to our X and O's game . Please take your first go: ";
          String playerMove = "";
          String cpuMove = "";
          boolean gameIsDecided = false;
      
      
          public void starttPlaying(){
          System.out.println();
          System.out.println();
          boolean playAgain = true;
      
              while (playAgain) {            
      
      
         for (int i = 1; i <=9; i++)
          {
              //Gives the human player its identity(1) and sets out what happens if player wins
      
              playerMove = getMove(prompt);
              updateBoard(playerMove, 1);
              displayBoard();
              if (isGameDecided())
              {
                  System.out.println("Congratulations , you have beaten me!!");
                  gameIsDecided = true;
                  break;
              }
      
            if (i < 9)
              {
                  cpuMove = getCpuMove();
                  System.out.println(cpuMove);
                  updateBoard(cpuMove, 2);
                  displayBoard();
                  if (isGameDecided())
                  {
                      System.out.println("Unlucky , I win. Better luck next time :");
                      gameIsDecided = true;
                      break;
                  }
                  prompt = "Take your next go: ";
                  i++;
              }
          }
         if (!gameIsDecided)
            System.out.println("So nothing can separate us , the game is a draw !!");
      
              System.out.println("do you want to play Again Y : N"); 
             Scanner scanner = new Scanner(System.in);
             String desicion = scanner.nextLine();
             playAgain = desicion.equalsIgnoreCase("y") ? true : false;
          }
          }
      
      
      //    
      
      
      
             public static String getMove(String prompt)
      {
          String turn;
          System.out.print(prompt);
          do
          {
              turn = sc.nextLine();
              if (!isAcceptablePlay(turn))
              {
                  System.out.println("Unfortunately this move isn't valid , please try again !!");
              }
          } while (!isAcceptablePlay(turn));
          return turn;
      }
      
           public static boolean isAcceptablePlay(String turn)
      {
          if (turn.equalsIgnoreCase("A1") & A1 == 0)
              return true;
          if (turn.equalsIgnoreCase("A2") & A2 == 0)
              return true;
          if (turn.equalsIgnoreCase("A3") & A3 == 0)
              return true;
          if (turn.equalsIgnoreCase("B1") & B1 == 0)
              return true;
          if (turn.equalsIgnoreCase("B2") & B2 == 0)
              return true;
          if (turn.equalsIgnoreCase("B3") & B3 == 0)
              return true;
          if (turn.equalsIgnoreCase("C1") & C1 == 0)
              return true;
          if (turn.equalsIgnoreCase("C2") & C2 == 0)
              return true;
          if (turn.equalsIgnoreCase("C3") & C3 == 0)
              return true;
          return false;
      }
      
         public static void updateBoard(String turn, int player)
      {
          if (turn.equalsIgnoreCase("A1"))
              A1 = player;
          if (turn.equalsIgnoreCase("A2"))
              A2 = player;
          if (turn.equalsIgnoreCase("A3"))
              A3 = player;
          if (turn.equalsIgnoreCase("B1"))
              B1 = player;
          if (turn.equalsIgnoreCase("B2"))
              B2 = player;
          if (turn.equalsIgnoreCase("B3"))
              B3 = player;
          if (turn.equalsIgnoreCase("C1"))
              C1 = player;
          if (turn.equalsIgnoreCase("C2"))
              C2 = player;
          if (turn.equalsIgnoreCase("C3"))
              C3 = player;
      }
      
      
          public static void displayBoard()
      {
          String row = "";
          System.out.println();
      
          row = " " + getSymbol(A1) + " | " + getSymbol(A2) + " | " + getSymbol(A3);
          System.out.println(row);
          System.out.println("-----------");
      
          row = " " + getSymbol(B1) + " | " + getSymbol(B2) + " | " + getSymbol(B3);
          System.out.println(row);
          System.out.println("-----------");
      
          row = " " + getSymbol(C1) + " | " + getSymbol(C2) + " | " + getSymbol(C3);
          System.out.println(row);
          System.out.println();
      }
      
        public static String getSymbol(int square)
      {
          if (square == 1)
              return "X";
          if (square == 2)
              return "O";
          return " ";
      }
      //This controls the Computer move . By mixing them up we make the computer more competitive
      public static String getCpuMove()
      {
          if (B2 == 0)
              return  "B2";
      
          if (A3 == 0)
              return  "A3";
      
          if (C2 == 0)
              return  "C2";
      
          if (B1 == 0)
              return  "B1";
      
          if (B3 == 0)
              return  "B3";
      
          if (C1 == 0)
              return  "C1";
      
          if (A1 == 0)
              return "A1";
      
          if (C3 == 0)
              return  "C3";
          if (A2 == 0)
              return "A2";
          return "";
      }
      
      //This contains all the possible winning combinations .
      public static boolean isGameDecided()
      {
          if (is3inARow(A1, A2, A3))
              return true;
          if (is3inARow(B1, B2, B3))
              return true;
          if (is3inARow(C1, C2, C3))
              return true;
          if (is3inARow(A1, B1, C1))
              return true;
          if (is3inARow(A2, B2, C2))
              return true;
          if (is3inARow(A3, B3, C3))
              return true;
          if (is3inARow(A1, B2, C3))
              return true;
          if (is3inARow(A3, B2, C1))
              return true;
          return false;
      }
      
      public static boolean is3inARow(int a, int b, int c)
      {
          return ((a == b) & (a == c) & (a != 0));
      }
      
      }
      

      我认为这就是你想要做的,试试我的代码,如果你愿意,可以做更多的改变

      【讨论】:

      • 这是我遇到的问题,我试过了,但完成后它不会再次重置板,所以游戏变得无法玩
      • 告诉我怎么玩这个游戏:D
      • 9个方框,轮流选择一个方框,X代表用户,O代表计算机。您必须连续匹配 3 次才能获胜
      • 您需要清除static int A1, A2, A3, B1, B2, B3, C1, C2, C3; 游戏中的棋盘已更新,一旦您按“是”并重新开始游戏,棋盘未清除,我的代码没有任何问题,因为您再次询问如何玩游戏,,清除棋盘
      • 用你的代码,游戏重新开始,只有位置仍然被填满。我将如何清除他们的位置
      猜你喜欢
      • 1970-01-01
      • 2012-05-26
      • 1970-01-01
      • 2013-04-18
      • 1970-01-01
      • 2023-03-10
      • 2018-02-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多