Java骰子游戏分配的困难
我是Java的初学者,所以我不太了解Java。我正在大学学习Java,我正在做一个骰子游戏来完成作业 以下是作业说明: “编写一个模拟骰子游戏的程序。在这个游戏中,玩家轮流掷两个骰子。在每一个回合中,他们记录两个骰子的总和,并将其加到总数中。如果一个玩家掷一个双倍骰子(两个骰子的值相同),那么玩家将再次掷骰子。第一个达到总数75的玩家将获胜 对于到20的游戏,输出应如下所示(注:无需用户输入): 我的问题是,我不知道如何在没有用户输入的情况下切换到下一个玩家的回合,我也不知道如何正确跟踪玩家的总数,因为它会重置,并且不会达到75。请帮助Java骰子游戏分配的困难,java,Java,我是Java的初学者,所以我不太了解Java。我正在大学学习Java,我正在做一个骰子游戏来完成作业 以下是作业说明: “编写一个模拟骰子游戏的程序。在这个游戏中,玩家轮流掷两个骰子。在每一个回合中,他们记录两个骰子的总和,并将其加到总数中。如果一个玩家掷一个双倍骰子(两个骰子的值相同),那么玩家将再次掷骰子。第一个达到总数75的玩家将获胜 对于到20的游戏,输出应如下所示(注:无需用户输入): 我的问题是,我不知道如何在没有用户输入的情况下切换到下一个玩家的回合,我也不知道如何正确跟踪玩家的总
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);
}
}
import java.util.*;
公共班机
{
公共静态void main(字符串[]args)
{
扫描仪扫描=新扫描仪(System.in);
随机生成器=新随机();
int-1;
int-2;
整数和;
布尔值endTurn=false;
布尔终局=假;
int totalsum1=0;
int totalsum2=0;
//玩家1
做
{
dice1=生成器。下一个(6)+1;
dice2=生成器。下一个(6)+1;
总和=骰子1+骰子2;
totalsum1+=总和;
System.out.println(“玩家1掷一个“+骰子1+”和一个“+骰子2”);
System.out.println(“玩家1现在有”+和);
如果(骰子1!=骰子2)
{
//endTurn=true;
总sum1<75;
}否则
{
System.out.println(“玩家1再次开始玩”);
dice1=生成器。下一个(6)+1;
dice2=生成器。下一个(6)+1;
System.out.println(“玩家1掷一个“+骰子1+”和一个“+骰子2”);
System.out.println(“玩家1现在有”+(totalsum1+dice1+dice2));
}
如果(totalsum1>=75&!(totalsum2>=75))
{
System.out.println(“玩家1以“+totalsum1”的总数获胜);
//终局=正确;
打破
}
//玩家
dice1=生成器。下一个(6)+1;
dice2=生成器。下一个(6)+1;
总和=骰子1+骰子2;
totalsum2+=总和;
System.out.println(“玩家2掷一个“+骰子1+”和一个“+骰子2”);
System.out.println(“玩家2现在有”+sum);
如果(骰子1!=骰子2)
{
endTurn=true;
总sum2<75;
}否则
{
System.out.println(“玩家2再次开始玩”);
dice1=生成器。下一个(6)+1;
dice2=生成器。下一个(6)+1;
System.out.println(“玩家2掷一个“+骰子1+”和一个“+骰子2”);
System.out.println(“玩家2现在有”+(totalsum2+dice1+dice2));
}
如果(totalsum2>=75&!(totalsum1>=75))
{
System.out.println(“玩家2以“+totalsum2”的总数获胜);
终局=正确;
打破
}
}而(totalsum1<75&&totalsum2<75);
}
}
我将展开我的评论,并使用您已有的代码帮助您开始使用播放器
类:
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()
如果允许播放器再次滚动并executeTurn(),则将返回true
将让玩家滚动直到不再有双打(你可能需要检查中间的获胜条件,但我在回合结束时暂时没有检查)
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多个玩家,通过使用扫描仪来改变<代码> WiSqk你将传递给玩家,但是我将留给你作为一个练习。)
你好,欢迎。代码需要一个完整的重构,所以它有点超出站点的范围。考虑一下:要么是玩家的回合,要么是玩家的TW。o、 因此,您可以使用布尔值(例如,true=player1,false=player2)跟踪这一点。根据这一点,您可以决定轮到谁(对于字符串和变量,如totalsum1
和totalsum2
).根据Federico的评论,我建议您将某个玩家的代码放入一个单独的类中,该类可以命名为player
。然后您只需创建player
的两个实例,并给它们一个不同的名称,例如player player1=新玩家(“玩家1”);player player2=新玩家(“玩家2”)然后基本上是这样的:if(currentPlayer==player1){currentPlayer=player2;}else{currentPlayer=player1;}
在循环开始时,执行当前玩家的回合,并在回合结束时检查他是否赢了。while(totalsum1<75);
将无限循环。还要注意,while(totalsum1<75);
之类的事情是危险的,会破坏您的应用程序。假设totalsum1
小于75,那么循环将执行空体indefin
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");