java - Java 骰子游戏分配的困难

标签 java

我是 Java 的初学者,所以了解不多。我正在大学类(class)中学习 Java,并且正在为一项作业开发骰子游戏。

以下是作业说明:

"Write a program that simulates a game of dice. In this game, players take alternate turns rolling two dice. On each turn, they record the sum of the two dice and add this to their total. If a player rolls a doublet (both dice have the same value), then the player gets to roll again. The first player to reach a total of 75 will win.

For games to 20, the output should be as follow (note: no user input is required):

Player 1 rolls a 3 and a 3
Player 1 now has 6 Player 1 gets to roll again Player 1 rolls a 5 and a 1
Player 1 now has 12
Player 2 rolls a 5 and a 1
Player 2 now has 6
Player 1 rolls a 5 and a 6
Player 1 now has 23
Player 1 wins with a total of 23"

我的问题是我无法弄清楚如何在没有用户输入的情况下切换到下一个玩家的回合,而且我也无法弄清楚如何正确跟踪玩家的总和,因为它会重置并且无法达到75.请帮忙!

import java.util.*;

public class Main
{

    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        Random generator = new Random();

        int dice1;
        int dice2;
        int sum;
        boolean endTurn = false;
        boolean endGame = false;
        int totalsum1 = 0;
        int totalsum2 = 0;

        //PLAYER 1

        do
            {

            dice1 = generator.nextInt(6) + 1;
            dice2 = generator.nextInt(6) + 1;
            sum = dice1 + dice2;
            totalsum1 += sum;

            System.out.println("Player 1 rolls a " + dice1 + " and a " + dice2);
            System.out.println("Player 1 now has " + sum);

            if (dice1 != dice2)
            {

                //endTurn = true;
                while (totalsum1 < 75) ;

            } else
                {

                System.out.println("Player 1 gets to roll again");

                dice1 = generator.nextInt(6) + 1;
                dice2 = generator.nextInt(6) + 1;

                System.out.println("Player 1 rolls a " + dice1 + " and a " + dice2);
                System.out.println("Player 1 now has " + (totalsum1 + dice1 + dice2));

                }

            if (totalsum1 >= 75 && !(totalsum2 >= 75))
            {
                System.out.println("Player 1 wins with a total of " + totalsum1);
                //endGame = true;
                break;

            }

            //PLAYER

            dice1 = generator.nextInt(6) + 1;
            dice2 = generator.nextInt(6) + 1;
            sum = dice1 + dice2;
            totalsum2 += sum;

            System.out.println("Player 2 rolls a " + dice1 + " and a " + dice2);
            System.out.println("Player 2 now has " + sum);

            if (dice1 != dice2)
            {

                endTurn = true;
                while (totalsum2 < 75) ;

            } else
                {

                        System.out.println("Player 2 gets to roll again");

                        dice1 = generator.nextInt(6) + 1;
                        dice2 = generator.nextInt(6) + 1;

                        System.out.println("Player 2 rolls a " + dice1 + " and a " + dice2);
                        System.out.println("Player 2 now has " + (totalsum2 + dice1 + dice2));

                }

                    if (totalsum2 >= 75 && !(totalsum1 >= 75))
                    {
                        System.out.println("Player 2 wins with a total of " + totalsum2);
                        endGame = true;
                        break;

                    }
                } while (totalsum1 < 75 && totalsum2 < 75);
        }
    }

最佳答案

我将扩展我的评论并使用您现有的代码来帮助您开始使用 Player 类:

class Player {
  private static final Random generator = new Random();

  private static final int WIN_SCORE = 75;

  private final String name;
  private int totalSum;
  private final int winScore;

  public Player( String name, int winScore ) {
    super();
    this.name = name;
    this.winScore = winScore;
  }

  public boolean executeTurn() {

    boolean rollAgain = roll();

    while( rollAgain ) {
      System.out.println( name + " gets to roll again" );
      rollAgain = roll();
    }

    //returns true if the player won
    return totalSum >= winScore;
  }

  private boolean roll() {
    int dice1 = generator.nextInt( 6 ) + 1;
    int dice2 = generator.nextInt( 6 ) + 1;
    totalSum += dice1 + dice2;

    System.out.println( name + " rolls a " + dice1 + " and a " + dice2 );
    System.out.println( name + " now has " + totalSum );

    //return true if he may roll again
    return dice1 == dice2;
  }

  public String getName() {
    return name;
  }
}

我做了一些更改,最值得注意的是方法 roll()executeTurn()。它们使用您已经存在的代码,但稍微分开以简化重新滚动。如果允许玩家再次滚动,roll() 将返回 true,并且 executeTurn() 将让玩家滚动,直到不再有 double 为止(你可能需要检查两者之间的获胜条件,但我暂时将其留在回合结束时)。

executeTurn() 现在将返回 true 表示玩家获胜,因此您所要做的就是循环并交替玩家,直到有人获胜。

示例:

int winScore = 75;
Player p1 = new Player("Player 1", winScore );
Player p2 = new Player("Player 2", winScore );
Player current = null;

//might be dangerous if due to a bug no player would ever win but we'll leave it simple for now
//we just loop forever and break the loop from the inside whenever a player won
while ( true ) {
  //p1 has had his turn so now it's p2's turn
  if( current == p1 ) {
    current = p2;
  } else {
    current = p1;
  }

  boolean won = current.executeTurn();

  //the player won so end the loop
  if( won ) {
    break;
  }
}

//since we didn't change the current player after he's won we just can get his name
System.out.println(current.getName() + " won");

正如您所看到的,这从玩家 1 开始,然后在两者之间交替。他们轮流进行,直到有人获胜,然后我们结束循环并宣布获胜者。

这可以扩展到超过 2 个玩家,并使用扫描仪更改您传递给玩家的 winScore,但我将把它留给您作为练习。 :)

关于java - Java 骰子游戏分配的困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58507539/

相关文章:

java - 如何在 Java GUI 中添加一行?

java - 从 AsyncTask 的 onPostExecute() 将自定义 View 添加到布局后未恢复自定义 View

java - 可以将日期转换为长但长太大?

java - 从命令行运行 Java 程序

Java 数组环绕

Java Session失效和超时不起作用

java - 如何在 boxLayout 的两个按钮之间添加空格?

java - Sax 解析器字符数组到整数?

java - 使用 OPEN SAML 的服务提供商实现 - JAVA

java - 将一维字符串导入二维int数组