mardi 15 janvier 2019

Needing help understanding how to implement an array to store dice/remove them based of rolls?

I was given a homework problem that I mostly understand. However, I need to have an array for my 5 dice. I didn't know how to go about setting up the array were I could roll the dice and if they land on a 2 or 5 they get removed from the dice. At the moment the only thing that makes my while loop work is my scoring system. Which I need have the array in there instead saying while I have dice/die keep rolling. Anyways, I think I have done most of the work except for the array that stores the dice. Any help would be greatly appreciated as I am pretty new to programming and would like to understand this for the future.

Here is a brief summary of what I need my program to do:

  1. Make an array of five dice

  2. Roll a the 5 dice. (6 sided).

  3. a. Check for the presence of a 2. b. Check for the presence of a 2 or 5.

  4. If there is a 2 or 5, the sum is 0, and the number of dice available is reduced by the number of 2s or 5s.

  5. If there is no 2 or 5, sum the dice and add it to the total.

  6. While there are dice to roll, keep going until you run out of dice.

I've already written the code most of way that fulfills the requirements of the game. However, I am stuck on the array and can't figure out how to implement it.

import java.util.Scanner;

public class StuckInTheMudd {

    static int rollDice() {

        int die1 = (int) (6 * Math.random()) + 1;
        return die1;
    }

    public static void main(String[] args) {

        int die1;
        int die2;
        int die3;
        int die4;
        int die5;

        int playerPoints = 0;
        int roundPoints = 0;
        String answer;
        Scanner input = new Scanner(System.in);

        System.out.println("Hello, Welcome To Stuck In The Mud");

        while (playerPoints <= 100) {

            System.out.println("Please roll the dice by pressing 'y' and hitting enter");
            System.out.println("o=================o");

            answer = input.next();

            if (answer.equalsIgnoreCase("y")) {

                die1 = rollDice();
                die2 = rollDice();
                die3 = rollDice();
                die4 = rollDice();
                die5 = rollDice();

                System.out.println("Dice 1: " + die1);
                System.out.println("Dice 2: " + die2);
                System.out.println("Dice 3: " + die3);
                System.out.println("Dice 4: " + die4);
                System.out.println("Dice 5: " + die5);

                if (die1 == 2 || die2 == 2 || die3 == 2 || die4 == 2 || die5 == 2) {
                    roundPoints = 0;
                    System.out.println("The Player loses the points for this round.");
                    System.out.println("o=================o");
                    System.out.println("Players points: " + playerPoints);
                    System.out.println("o=================o");
                } else if (die1 == 5 || die2 == 5 || die3 == 5 || die4 == 5 || die5 == 5) {
                    roundPoints = 0;
                    System.out.println("The Player loses the points for this round.");
                    System.out.println("o=================o");
                    System.out.println("Players points: " + playerPoints);
                    System.out.println("o=================o");
                }else {
                    roundPoints = roundPoints + die1 + die2 + die3 + die4 + die5;
                    playerPoints = playerPoints + roundPoints;
                    System.out.println("Current Round Points: " + roundPoints);
                    System.out.println("o=================o");}

            } 

            else {
                System.out.println("Invalid entry, please try again.");
            }

        }
        System.out.println("Game Over and end of loop");
    }
}

Once again I need to have an array that stores my dice. If the dice land on 2s or 5s they are removed from the array. This cycle will continue until I have no more dice left and the game ends. Finally, any help will be greatly appreciated as I would like to understand how to implement this for not only this homework but for future projects as well. Thanks in advance!




Aucun commentaire:

Enregistrer un commentaire