如何在我的简单java骰子游戏中添加第三个玩家?

时间:2014-02-19 12:29:42

标签: java

我希望有人能指出我正确的方向。下面的代码是我的双人骰子游戏程序。我需要添加第三个玩家,但不知道如何。到目前为止,我们已经介绍了if语句,开关和循环 - 所以我不允许使用其他任何东西,因为我们还没有覆盖它。我已经查看了这些问题,但我找不到任何可以回答我的问题的内容,有人可以帮忙吗?

import java.util.Scanner;

class dice {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String fP, sP;
        int dice = 0, dfPTot = 0, dsPTot = 0, round = 0, fPScore = 0, sPScore = 0, fPScoreR = 0,
                sPScoreR = 0;

        System.out.println(
        "Welcome to the online interactive dice game.\n\n\t
        * To complete the 5 rounds you will need two players and three dice
        !");
        System.out.println("\nPlayer 1, please state your name: ");
        fP = input.next();
        System.out.println("Welcome " + fP + "\n\nPlayer 2, please state your name: ");
        sP = input.next();
        System.out.println("Welcome " + sP + "\n\nLet's begin!");

        for (int count = 1; count <= 5; count++) {
            System.out.print(fP + " please throw your three dices and then input 
                                            your total dice score
            : ");
                        dfPTot = input.nextInt();
            System.out.print(sP + " please throw your three dices and then input
                        your total dice score
            : ");
                        dsPTot = input.nextInt();
            round = dfPTot + dsPTot;
            System.out.print(" The round total is: " + round + " \n");

            if (dfPTot > dsPTot) {
                fPScore = fPScore + round;
                fPScoreR += fPScore;
                sPScore = sPScore + 0;
                sPScoreR += sPScore;
            } else {
                sPScore = sPScore + round;
                sPScoreR += sPScore;
                fPScore = fPScore + 0;
                fPScoreR += fPScore;
            }
        }

        dfPTot = fPScore = round = 0;
        fPScore = fPScore + fPScoreR;
        sPScore = sPScore + sPScoreR;

        if (fPScore > sPScore) {
            System.out.println(fP + " is the Dice Master scoring: " + fPScore + " points");
        } else {
            System.out.println(sP + " is the Dice Master scoring: " + sPScore + " points");
        }
    }
}

2 个答案:

答案 0 :(得分:1)

(假设你不能使用数组,对象等。)

你希望永远不会像往常那样设计它。无论如何,几乎所有与他们名字中的sP(第二个玩家)和fP(第一个玩家)的变量都需要有第三个,可以受到影响的tP(第三个玩家)。

然后你可以开始复制粘贴并更改最终if-else语句中的逻辑以适应第三个玩家。

(告诉你这样做真的很痛,只是承诺永远不会设计这样的东西):))

答案 1 :(得分:1)

你可以使用Arraylist并循环浏览它以确保每个玩家都有机会。使用Arraylist的一个优点是您可以动态更改大小。所以你的实现应该适用于n个玩家。

此片段允许您为玩家初始化arraylist。您必须导入java.utils包。

ArrayList<String> players = new ArrayList<String>();

添加玩家;先问一下有多少玩家。通过输入读取,或使用常量。然后使用下一个添加n个玩家。 (max_players =玩家数量)

for(int i = 1; i <= max_players; i++) {
    System.out.println("\nPlayer " + i + ", please state your name: ");
    String name =input.next();
    // add to array
    players.Add(name);
}

完成此操作后,您将获得一系列玩家。为游戏本身再做一次。提示:使用两个循环。一轮循环和一个内循环,每个玩家都有机会进行一轮比赛。

如果要访问该阵列,可以使用this documentation

中提供的方法 例如

; players.size()给出阵列中当前的玩家数量。或players.get(0)给出数组中第一个玩家的名字,等等......

请查看文档以了解更多可能性。尝试实现这样的控制结构,最后你有一个动态代码。我不会提出一个完整的解决方案,先自己尝试一下。自我教育是理解这一概念的好方法。

如果您以后遇到问题,请随时search it on stackoverflow或询问您是否找不到解决未来问题的方法。

祝你好运,编码愉快。

PS:实际上,使用对象(OOP)会更容易。只需使用Playername字段创建课程score即可。然后将其用作数组的类型。