如何实现一个数组来存储 dice/remove 它们基于卷?

How to implement an array to store dice/remove them based of rolls?

我被布置了一道我基本理解的作业题。但是,我需要为我的 5 个骰子创建一个数组。我不知道如何设置阵列,我可以掷骰子,如果它们落在 2 或 5 上,它们就会从骰子中移除。目前,唯一让我的 while 循环起作用的是我的评分系统。我需要把数组放在那里,而不是说当我有 dice/die 继续滚动时。不管怎样,我想我已经完成了除了存储骰子的数组之外的大部分工作。我对编程还很陌生,想为将来了解这一点。

这里是我需要我的程序做什么的简要总结:

  1. 排列五个骰子

  2. 掷 5 个骰子。 (6面).

  3. 一个。检查是否存在 2.
    b.检查是否存在 2 或 5。

  4. 如果出现2或5,则和为0,可用骰子数减去2或5的个数

  5. 如果没有 2 或 5,则将骰子加起来。

  6. 虽然还有骰子可以掷,但继续前进,直到 运行 没骰子。

我已经编写了大部分满足游戏要求的代码。然而,我卡在了数组上,不知道如何实现它。

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");
    }
}

我再次需要一个数组来存储我的骰子。如果骰子落在 2s 或 5s 上,它们将从阵列中移除。这个循环将一直持续到我没有更多的骰子并且游戏结束。

您可以只使用位掩码而不是数组:

int bitmask = 0b11111; // 5 bits = 5 dice

要读取有多少个骰子,只需使用 Integer.bitCount():

int dicesLeft = Integer.bitCount(bitmask);

要移除骰子,您可以使用位移:

bitmask >>= n;

其中 n 是您要移除的骰子数量

我会为骰子项目创建一个 int 数组,但我不会从中删除项目(如果你想这样做,那么 List 是一个更好的选择)

int[] dices = new int[5];

我会使用一个简单的常量值来将特定的骰子标记为已移除,-1 对此有好处。

private final static int NO_DICE = -1;


public static void main(String[] args) {
    int[] dices = new int[5];

    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")) {
            for (int i = 0; i < dices.length; i++) {
                if (dices[i] == NO_DICE) {
                    continue;
                }
                dices[i] = rollDice();
                System.out.println("Dice " + i + ": " + dices[i]);
            }

然后在检查 2 或 5 时

for (int i = 0; i < dices.length; i++) {
    if (dices[i] == NO_DICE) {
        continue;
    }
    if (dices[i] == 2 || dices[i] == 5) {
         dices[i] = NO_DICE;
         roundPoints = 0;
         //...
     }
}

不幸的是,您不能从数组本身中删除,因为大小在创建时是固定的(因此,如果您可以使用某种 List - 这会更容易)。因此,您将不得不创建一个没有您不想要的元素的新数组。

也许循环遍历现有数组,并在新数组中为每个数组设置值,除非它是二或五。这是一个简单的例子:

static int[] removeElementFromArray(int[] arr) {
    int[] newArr = new int[arr.length - 1];
    int temp = 0;
    for (int i = 0; i < arr.length; i++) {
        if (arr[i] != 5 && arr[i] != 2) {
            newArr[temp] = arr[i];
            temp++;
        }
    }
    return newArr;
}

这里有一个 online 的例子,但是显然是存储返回的数组而不是打印它