当前位置: 首页 > 知识库问答 >
问题:

Java骰子游戏分配困难

范浩荡
2023-03-14

我是Java的初学者,所以我不太懂。我在大学的课程中学习Java,我正在为作业做骰子游戏。

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

共有1个答案

公冶谦
2023-03-14

我将展开我的评论,并使用您已经存在的代码来帮助您开始使用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()将让玩家滚动,直到不再有双打(您可能必须检查中间的获胜条件,但我现在在回合结束时保留了这个条件)。

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编写掷骰子游戏的使用技巧和注意事项,需要的朋友参考一下 废话不多说了,直接奔主题。 **多线程&&观察者模式 题目要求:《掷骰子》窗体小游戏,在该游戏中,玩家初始拥有1000的金钱,每次输入押大还是押小,以及下注金额,随机3个骰子的点数,如果3个骰子的总点数小于等于9,则开小,否则开大,然后判断玩家是否押对,如果未押对则扣除下注金额,如果押对则奖

  • 有人能在这里给我指个正确的方向吗?我的游戏工作完美,但我想添加一些实际的互动/目标。谢谢

  • 我对C#和一般编码都是新手。为了提高我的技能,我试图创建一个基本的游戏,两个玩家掷骰子,并记录他们的得分。玩家达到20分即获胜。每个玩家轮流掷一个骰子,把他们的第一个骰子加到他们的第二个骰子上,以此类推,直到其中一个达到20。如果玩家掷出一个六,他们可以再次掷骰子。 任何帮助都将不胜感激。

  • 代码的目的是让两个玩家掷一对骰子。第一个掷出20分的玩家赢得比赛。我很难弄清楚如何正确地跟踪滚动的总和;它只给我当前回合的总和,然后当每个玩家滚动10次时游戏就结束了。 我如何正确地计算每个玩家游戏的总和,然后当其中一个玩家的总和等于20时停止循环?

  • 程序在最后未能计算出正确的和。我做错了什么?如有任何帮助,不胜感激。谢了。

  • rollDice( )应使用兰德( )随机生成介于1-6之间的数字 返回兰德( )生成的数字 3)实现一个功能,功能原型为Int ;Playgame( Void ;);  根据用户的赢或输的次数给用户一个适当的消息 返回值为EXIT_SUCCESS 这里是我现在拥有的,但它告诉我有错误。有谁能帮我完成这个任务吗? ^ X.C:10:1:错误:程序中杂散“\240” X.C:12:1:错误:程序中杂