Java骰子游戏循环问题

时间:2014-03-12 01:24:56

标签: java

我正在尝试编写一个玩家掷骰子的程序,如果他们掷出六个他们进入游戏,但他们不会滚动六个他们仍然在游戏之外,直到他们得到六个。 游戏的目的是让玩家达到100才能获胜。我试图编写一个程序,允许每个玩家掷骰子,当他们再次获得六次掷骰时。在游戏中的人已经掷出骰子后,那些外面有机会掷骰子。然而,关于我的代码的一些事情是严重错误的,因为当玩家获得六分时他们可以再次滚动,但之后它们就会脱离循环。 任何帮助将非常感谢!!

 static Scanner keyboard = new Scanner(System.in);
    static int numberOfPlayers = 0;
    static int selectOption;
    static boolean started = false;
    static boolean finished = false;
    static boolean gameFinished = false;
    static int backMenu ;
    String winner = "";
    static int diceOption;
    static int position = 0;
    static int score = 0;
    static int location = 0;


    public static void gameOptions() {

        System.out.println("**************************************************************");
        System.out.println("\t\t\t\t\t\tWelcome to snakes and ladders!!!");
        System.out.println("**************************************************************");

        do {

            System.out.println("Please select an option: \n" +
                    "1. Game instuctions " +
                    "\n2. Select number of players");
            selectOption = keyboard.nextInt();
        } while (selectOption != 1 && selectOption != 2);

        Player[] playerArray;
        {
            if (selectOption == 1 ) {
                System.out.println("Rules of snakes and ladders" +
                        "\n1. To enter the game you have to roll a six. " +
                           "\n2.The first player to get to square 100 wins!");
                          System.out.println("To go back to menu press 3");
                         selectOption=keyboard.nextInt();



            } else if (selectOption == 2) {

                System.out.println("Enter number of players");
                numberOfPlayers = keyboard.nextInt();

                //clearing the buffer
                keyboard.nextLine();
            }

            playerArray = new Player[numberOfPlayers];
            for (int count = 0; count < playerArray.length; count++) {
                playerArray[count] = new Player();

                System.out.println("\t Please enter the firstname of the player ");
                playerArray[count].setUserName(keyboard.next());
                //System.out.println( playerArray[count].getUserName() + " you are now ready to play snakes and ladders!!!!!!!!");

            }



        playGame(playerArray, gameFinished );
    }

    public static void playGame(Player[] pPlayerArray, Boolean gameFinished)
    // to enter the game each player has to roll a six
    // When they have rolled a six they move to position one where they wait until their next turn at rolling the dice,
    {

        //print gameboard



        while(!gameFinished )

        {
            for (int loop = 0; loop < numberOfPlayers; loop++) {
                  do{
                System.out.println(pPlayerArray[loop].getUserName() + "  turn to roll the dice  ");
                keyboard.nextLine();
                //dice instance
                pPlayerArray[loop].setScore(Dice2.rolledDice());
                }
                if ((pPlayerArray[loop].score == 6) && (pPlayerArray[loop].started==false)){


                    pPlayerArray[loop].setScore(Dice2.rolledDice());
                    pPlayerArray[loop].setPosition(0);
                    System.out.println(pPlayerArray[loop].getUserName() + "  you rolled a " + pPlayerArray[loop].getScore() +
                            " your new position is " + pPlayerArray[loop].getPosition());

              while (pPlayerArray[loop].score == 6){

               pPlayerArray[loop].setScore(Dice2.rolledDice());
                System.out.println("Please roll again");
                System.out.println(pPlayerArray[loop].getUserName() + " you rolled a  " + pPlayerArray[loop].getScore() +
                        " your new position is" + pPlayerArray[loop].getPosition());
                }
                //System.out.println(pPlayerArray[loop].getUserName() + "Press the number 8 roll the dice ");
                //diceOption = keyboard.nextInt();
                //position = position + score;
                }
                else if (pPlayerArray[loop].started == true)  {


                pPlayerArray[loop].setScore(Dice2.rolledDice());

                pPlayerArray[loop].getPosition();



            }
                else
                {

                    System.out.println(pPlayerArray[loop].getUserName() + "  You have rolled  " + pPlayerArray[loop].getScore() + "\n");
                    System.out.println("Sorry you have to roll a six to enter the game" + "\n");


                    //System.out.println("Next players turn to roll the dice");
                }
            }while (pPlayerArray[loop].finished==false);
            }
        }
    }











    public static void main(String[] args) {

        gameOptions();
    }//main
}//class

1 个答案:

答案 0 :(得分:0)

首先,我会正确地重新格式化您的代码,以便更容易阅读和调试。

在您这样做之后,您应该能够轻松地看到您有不应该有的额外开放范围括号({)。我快速浏览了两下,但是在重新格式化代码之后我会把它作为练习让你找到它们,这样就更容易阅读。

提示:第一个函数中有一个,第二个函数中有一个。