在java中退出或继续游戏

时间:2013-11-14 06:56:39

标签: java

afer程序输出获胜者,我询问用户是否想再次播放或退出但由于某种原因扫描仪不读取此输入(继续或不继续)。我甚至在while循环之外提示继续,但仍然没有运气: “| XXX |  | O |  | Ò|

玩家X是赢家! 你想再玩一次吗?输入Y表示是,或输入N表示否! 建立成功(总时间:26秒) “

这是代码:

public class TicTacToeRunner
{
   public static void main(String[] args)
   {
      int row;
      int column;
      Scanner in = new Scanner(System.in);
      TicTacToe game = new TicTacToe();
      String player = "X";
      boolean done = false; 
      int moveCounter = 0;
      String answer;


       int noMovement = -2;
       int toQuit = -1;
       int movesToCheckWins = 5;
       int movesToCheckTies = 7;



      while (!done) 
      {

         do
         {
            row = column = noMovement;

            System.out.print("\n" + game);
            System.out.print("Please make a move " + (moveCounter + 1) + "(" + player + ")\nRow for " + player.toUpperCase() + " (or -1 to exit): ");

            if (in.hasNextInt()) //check if input is an integer
             {
               row = in.nextInt();
             }

             if (row == toQuit) //if entered -1 quit the game
             {
                 done = true;
                 System.out.println("Player " +player.toUpperCase() + " ended the game !");
                 System.exit(0); //game termination
             }
             else
             {
                System.out.print("Column for " + player.toUpperCase() + ": ");
                if(in.hasNextInt()) //check if input is an integer
                 {
                  column = in.nextInt();
                 }
             }
         }while(!game.checkForValidMove(row, column)); //end of do-while loop if checkForValidMove is false

        moveCounter++;
        game.set(row, column, player);

        if (moveCounter >= movesToCheckWins) //check wins after 5 moves
        {
            if (game.checkForWin(row, column, player)) //if a winner
            {
                done = true;
                System.out.println("\n" + game);
                System.out.println("Player " + player.toUpperCase() + " is a winner!");

            }
        }

        if (moveCounter >= movesToCheckTies) //Check for ties after 7 moves
        {
            if (game.checkForEarlyTie()) //check for early tie
            {
                done = true;
                System.out.println("\n" + game);
                System.out.println("Tie Game after " + moveCounter + " moves!");

            }
        }


     // Switching players 

     if (player.equals("X"))
     {
           player = "O";
     }
        else
     {
           player = "X";
     }


      }


                System.out.println("Do you want to play again? Enter Y for yes or N for no!");

                answer = in.nextLine();
                answer = answer.toLowerCase(); //change input to lowercase for bullet proofing
                if(answer.equals("y"))
                {
                done = false;
                player = "X";
                moveCounter = 0;
                game.resetTheGame();

                }

              else
                 {
                 System.exit(0); //program termination
               }


  }

}

1 个答案:

答案 0 :(得分:0)

使用执行循环。你的工作很简单..

import java.util.Scanner;

public class TicTacToeRunner {
    public static void main(String[] args) {
        int row;
        int column;
        Scanner in = new Scanner(System.in);
        TicTacToe game = new TicTacToe();
        String player = "X";
        boolean done = false;
        String choice = "";
        int moveCounter = 0;
        String answer;

        int noMovement = -2;
        int toQuit = -1;
        int movesToCheckWins = 5;
        int movesToCheckTies = 7;
        do{
        while (!done) {
            do {
                row = column = noMovement;

                System.out.print("\n" + game);
                System.out.print("Please make a move " + (moveCounter + 1) + "(" + player + ")\nRow for " + player.toUpperCase() + " (or -1 to exit): ");

                if (in.hasNextInt()) // check if input is an integer
                {
                    row = in.nextInt();
                }

                if (row == toQuit) // if entered -1 quit the game
                {
                    done = true;
                    System.out.println("Player " + player.toUpperCase() + " ended the game !");
                    System.exit(0); // game termination
                } else {
                    System.out.print("Column for " + player.toUpperCase() + ": ");
                    if (in.hasNextInt()) // check if input is an integer
                    {
                        column = in.nextInt();
                    }
                }
            } while (!game.checkForValidMove(row, column)); // end of do-while
                                                            // loop if
                                                            // checkForValidMove
                                                            // is false

            moveCounter++;
            game.set(row, column, player);

            if (moveCounter >= movesToCheckWins) // check wins after 5 moves
            {
                if (game.checkForWin(row, column, player)) // if a winner
                {
                    done = true;
                    System.out.println("\n" + game);
                    System.out.println("Player " + player.toUpperCase() + " is a winner!");

                }
            }

            if (moveCounter >= movesToCheckTies) // Check for ties after 7 moves
            {
                if (game.checkForEarlyTie()) // check for early tie
                {
                    done = true;
                    System.out.println("\n" + game);
                    System.out.println("Tie Game after " + moveCounter + " moves!");

                }
            }

            // Switching players

            if (player.equals("X")) {
                player = "O";
            } else {
                player = "X";
            }

        }

        System.out.println("Do you want to play again? Enter Y for yes or N for no!");
        choice = in.nextLine();
        }while(choice.equals("y")||choice.equals("Y"));

    }

}