0
votes

I am trying to write a program in which a player rolls a dice and if they roll a six they enter in to a game but they don't roll a six they remain outside the game until they get a six. The aim of the game is for player to reach 100 to win .I have attempted to write a program that allows each player to roll the dice and when they get six roll again. After those in game have rolled the dice those out side get a chance to roll the dice. However something about my code is seriously wrong in that when the player gets a six they can roll again but after that they fall out of the loop. Any help would be very much appreciated !!

 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
Holy spaghetti code. This is near undecipherable. - Kon
come on, everybody started like this... - Leo
@Leo Actually, I started with BASICA. And I didn't know what a loop was :| - user2864740

1 Answers

0
votes

First, I would work on reformatting your code properly so that it is easier to read and debug.

After you do that, you should be able to easily see that you have extra open scope brackets ({) where they should not be. I counted two from a quick glance but I will leave it as an exercise for you to find them after you reformat your code so it is easier to read.

Hint: there is one in the first function and one in the second function.