2名玩家之间的骰子游戏,得分最高50分

时间:2020-07-28 22:04:09

标签: java arrays random

我正在创建一个游戏,其中两个玩家彼此掷骰子。

两个人(玩家A,玩家B)玩骰子游戏。他们每回合掷骰子,更高的数字赢得比赛,获胜者获得5分;如果两个人掷出相同价值的骰子,他们俩都获得3分。如果一名玩家达到50分或更高,游戏将结束。如果两个玩家在同一回合中都达到50,则将变成平局,直到再有一个更高的回合,直到更高价值的玩家赢得比赛为止(如果第11轮仍平局,则继续下一轮比赛。这是我到目前为止的代码,但是我没有得到任何分数达到50结束游戏。

public static void main(String[] args) {
    
    int playerA[] = new int[10];
    int playerB[] = new int[10];
    int playerAScore = 0;
    int playerBScore = 0;
    int round = 1;
    
    for (int i =0; i < playerA.length; i++) {
        System.out.println("Roll the die for round " + round++);
        playerA[i] = (int) ((Math.random() * 6) + 1);
        playerB[i] = (int) ((Math.random() * 6) + 1);
    
        System.out.println("player A has " + playerA[i] + " and player B has " + playerB[i]);
    
        if(playerA[i] == playerB[i]) {
            playerAScore = playerAScore + 3;
            playerBScore = playerBScore + 3;
        }
        else if (playerA[i] > playerB[i]) {
            playerAScore = playerAScore + 5;
        }
        else if (playerB[i] > playerB[i]) {
            playerBScore = playerBScore + 5;
        }
        if(playerAScore >= 50 || playerBScore >= 50) {
            break;
        }
    }
    
    System.out.println("The game is over.");
    
    if(playerAScore >= playerBScore)
        System.out.println("The winner is player A");
    else
        System.out.println("The winner is player B");
    
    System.out.println("How many rounds of game played?");
    System.out.println("Rounds played: " + round);
    
    System.out.println("Total Score per player:");
    System.out.println("Player A score: " + playerAScore);
    System.out.println("Player B score: " + playerBScore);
    
}

}

4 个答案:

答案 0 :(得分:1)

  1. 在得分之间的比较中,您有错别字。
playerB[i] > playerB[i]

这总是错误的,所以一个玩家根本得不到积分。

        else if (playerB[i] > playerB[i]) {
            playerBScore = playerBScore + 5;
        }

片段。显然应该是:

        else if (playerB[i] > playerA[i]) {
            playerBScore = playerBScore + 5;
        }
  1. 第二个错误可能是您只玩playerA.length次。在这种情况下为10,因此只有在赢得所有游戏的情况下玩家才获得50(这是非常低的概率)。

  2. 最后显示的回合数错误。进行的回合数为10,然后再增加一次,并指出回合数为11

答案 1 :(得分:0)

您应该使用for而不是while循环。在您的情况下,其中一位玩家必须每次赢得50分。尝试以下操作:while (playerAScore < 50 && playerBScore < 50)并在循环内递增索引i

答案 2 :(得分:0)

其他人指出了PlayerB > PlayerB的缺陷,但是这个答案是针对如何解决仅10个回合的问题的。而不是限制为10,而是允许函数递归执行,直到达到50分为止。

    public static void main(String[] args) {
        Game game = new Game();

        game.round(50);

        System.out.println("The game is over.");

        if(game.playerAScore >= game.playerBScore)
            System.out.println("The winner is player A");
        else
            System.out.println("The winner is player B");

        System.out.println("How many rounds of game played?");
        System.out.println("Rounds played: " + game.round);

        System.out.println("Total Score per player:");
        System.out.println("Player A score: " + game.playerAScore);
        System.out.println("Player B score: " + game.playerBScore);
    }
    static class Game {

        private int playerAScore = 0;

        private int playerBScore = 0;

        private int round;

        void round(int maximumScore) {
            System.out.println("Roll the die for round " + round);

            int playerARoll = (int) ((Math.random() * 6) + 1);

            int playerBRoll = (int) ((Math.random() * 6) + 1);

            System.out.println("player A has " + playerARoll + " and player B has " + playerBRoll);

            if (playerARoll == playerBRoll) {
                playerAScore += 3;
                playerBScore += 3;
            } else if (playerARoll > playerBRoll) {
                playerAScore += 5;
            } else {
                playerBScore += 5;
            }
            if(playerAScore >= maximumScore || playerBScore >= maximumScore) {
                return;
            }
            round++;
            round(maximumScore);
        }
    }

输出

Roll the die for round 0
player A has 2 and player B has 2
Roll the die for round 1
player A has 2 and player B has 2
Roll the die for round 2
player A has 6 and player B has 6
Roll the die for round 3
player A has 6 and player B has 6
Roll the die for round 4
player A has 6 and player B has 3
Roll the die for round 5
player A has 5 and player B has 2
Roll the die for round 6
player A has 2 and player B has 2
Roll the die for round 7
player A has 3 and player B has 1
Roll the die for round 8
player A has 2 and player B has 4
Roll the die for round 9
player A has 6 and player B has 6
Roll the die for round 10
player A has 1 and player B has 4
Roll the die for round 11
player A has 2 and player B has 3
Roll the die for round 12
player A has 2 and player B has 5
Roll the die for round 13
player A has 6 and player B has 4
Roll the die for round 14
player A has 1 and player B has 2
Roll the die for round 15
player A has 1 and player B has 5
Roll the die for round 16
player A has 3 and player B has 4
The game is over.
The winner is player B
How many rounds of game played?
Rounds played: 16
Total Score per player:
Player A score: 38
Player B score: 53

答案 3 :(得分:0)

您似乎在该行中输入了错字:

else if (playerB[i] > playerA[i]) playerBScore = playerBScore + 5;
}

这是修改后的代码:

public class diceGame{
public static void main(String[] args) {
    int playerA[] = new int[10];
    int playerB[] = new int[10];
    int playerAScore = 0;
    int playerBScore = 0;
    int round = 1;
    
    for (int i =0; i < playerA.length; i++) {
        System.out.println("Roll the die for round " + round++);
        playerA[i] = (int) ((Math.random() * 6) + 1);
        playerB[i] = (int) ((Math.random() * 6) + 1);
    
        System.out.println("player A has " + playerA[i] + " and player B has " + playerB[i]);
    
        if(playerA[i] == playerB[i]) {
            playerAScore = playerAScore + 3;
            playerBScore = playerBScore + 3;
        }
        else if (playerA[i] > playerB[i]) {
            playerAScore = playerAScore + 5;
        }
        else if (playerB[i] > playerA[i]) {
            playerBScore = playerBScore + 5;
        }
        if(playerAScore >= 50 || playerBScore >= 50) {
            break;
        }
    }
    
    System.out.println("The game is over.");
    
    if(playerAScore >= playerBScore)
        System.out.println("The winner is player A");
    else
        System.out.println("The winner is player B");
    
    System.out.println("How many rounds of game played?");
    System.out.println("Rounds played: " + round);
    
    System.out.println("Total Score per player:");
    System.out.println("Player A score: " + playerAScore);
    System.out.println("Player B score: " + playerBScore);
    
}
}