【发布时间】:2017-11-11 07:32:44
【问题描述】:
简介
再一次练习循环。这个程序会比上一个程序更复杂一些。
对于这个程序,您将创建一个骰子游戏 Pig 的模拟。
游戏的目标是让玩家在对手之前获得100分,更多细节将在任务中讨论。
在这种情况下,对手将是计算机。
任务 1
这个游戏需要两个 6 面骰子。您需要使用随机数生成器来模拟这些骰子。
两个骰子(随机数生成器)必须:
产生 1-6 之间的值 有一个140L和340L的种子,分别用于一模和二模(用于测试目的) 任务 2
现在你已经有了你的骰子,我们可以回顾一下你将要玩的规则。
规则:
每回合,玩家将轮流掷两个骰子。 如果两个骰子上都没有出现 1,则将这些值添加到玩家的总数中。然后他们可以选择再次滚动(选择 0)或将回合传给其他玩家(选择 1)。 如果其中一个骰子出现 1,则玩家在整个回合中不会获得任何积分,并且轮到另一位玩家滚动(之前回合获得的积分仍将存在于他们的总数中)。 如果玩家掷出两个 1,则玩家的回合结束,他们的总数重置为 0。 您将需要一个随机数生成器来确定是计算机(0)还是玩家(1)先走。这也将用于模拟计算机选择再次滚动或通过翻身。这台发电机的种子是 140L。
假设来自用户的有效输入。
输出应以“欢迎猪游戏”语句开头
以下所有用户提示应类似于:
轮到你了(当前点数:0) 你掷出 3 和 2,本回合获得的点数:5 按 0 再次滚动或按 1 开始轮到计算机 所有计算机提示应如下所示:
轮到计算机了(当前点数:0) 电脑掷出 1 和 4,没有得分,轮到你了 他们宣布轮到哪位玩家和当前分数。
然后是由哪个玩家随后滚动的数字
如果获得积分,则显示该回合获得的总积分(参见用户提示的第 2 行) 如果掷出一个,宣布没有得分并轮到下一位玩家(见电脑提示第 2 行) 如果两者都是一个,则使用显示消息“/whichever player/rolled two one, point reset and /opponent's/ turn”
import java.util.Scanner;
import java.util.Random;
public class GameOfPigs {
public static void main(String[] args) {
Random die1 = new Random(140L);
Random die2 = new Random(340L);
Random compDecision = new Random(140L);
Scanner scnr = new Scanner(System.in);
int computerTotal = 0;
int playerTotal = 0;
boolean playerTurn = true;
// Decides who goes first...
if ((compDecision.nextInt(2)) == 0) {
playersTurn = false;
}
System.out.println("Welcome of the Game of Pigs");
// Main game loop
while (computerTotal < 100 && playerTotal < 100) {
System.out.println();
int currentPlayerPoints = 0;
// Player's loop
while (playersTurn) {
System.out.println("Your turn (current points: " + playerTotal + ")");
int roll1 = die1.nextInt(6) + 1;
int roll2 = die2.nextInt(6) + 1;
// First Rule...Not the same as the example in class!!!
// Adjust accordingly!!!! Multiple ways to do this!!!!
if (roll1 == 1 && roll2 == 1) {
playerTotal = 0;
playersTurn = false;
break;
}
// Second Rule
else if (roll1 == 1 || roll2 == 1) {
playerTotal = playerTotal;
playersTurn = false;
break;
}
// Third Rule
else {
playerTotal += currentPlayerPoints;
int choice = scnr.nextInt();
if (choice == 1) {
playerTotal += currentPlayerPoints;
playersTurn = false;
break;
}
}
}
if (playerTotal >= 100) {
break;
}
//
int currentCompPoints = 0;
// Computer's loop
while (!playersTurn) {
System.out.println("Computer's turn (current points: " + computerTotal + ")");
int roll1 = die1.nextInt(6)+1;
int roll2 = die2.nextInt(6)+1;
if (roll1 == 1 && roll2 == 1) {
computerTotal = 0;
}
else if (roll1 == 1 || roll2 == 1) {
computerTotal = computerTotal;
}
else {
int choice = compDecision.nextInt(2);
computerTotal += currentPlayerPoints;
}
}
}
}
if (playerTotal > computerTotal) {
System.out.println("Congratulations! You won!");
}
else {
System.out.println("Too Bad, the computer won.");
}
}
}
GameOfPigs.java:86: error: illegal start of type
if (playerTotal > computerTotal) {
^
GameOfPigs.java:86: error: <identifier> expected
if (playerTotal > computerTotal) {
^
GameOfPigs.java:86: error: ';' expected
if (playerTotal > computerTotal) {
^
GameOfPigs.java:86: error: illegal start of type
if (playerTotal > computerTotal) {
^
GameOfPigs.java:86: error: <identifier> expected
if (playerTotal > computerTotal) {
^
GameOfPigs.java:86: error: ';' expected
if (playerTotal > computerTotal) {
^
GameOfPigs.java:87: error: illegal start of type
System.out.println("Congratulations! You won!");
^
GameOfPigs.java:87: error: ';' expected
System.out.println("Congratulations! You won!");
^
GameOfPigs.java:87: error: invalid method declaration; return type required
System.out.println("Congratulations! You won!");
^
GameOfPigs.java:87: error: illegal start of type
System.out.println("Congratulations! You won!");
^
GameOfPigs.java:89: error: class, interface, or enum expected
else {
^
GameOfPigs.java:91: error: class, interface, or enum expected
}
^
12 errors
谢谢!但现在它说:
GameOfPigs.java:18: error: cannot find symbol
playersTurn = false;
^
symbol: variable playersTurn
location: class GameOfPigs
GameOfPigs.java:29: error: cannot find symbol
while (playersTurn) {
^
symbol: variable playersTurn
location: class GameOfPigs
GameOfPigs.java:29: error: illegal start of type
while (playersTurn) {
^
GameOfPigs.java:38: error: cannot find symbol
playersTurn = false;
^
symbol: variable playersTurn
location: class GameOfPigs
GameOfPigs.java:45: error: cannot find symbol
playersTurn = false;
^
symbol: variable playersTurn
location: class GameOfPigs
GameOfPigs.java:55: error: cannot find symbol
playersTurn = false;
^
symbol: variable playersTurn
location: class GameOfPigs
GameOfPigs.java:68: error: cannot find symbol
while (!playersTurn) {
^
symbol: variable playersTurn
location: class GameOfPigs
7 errors
【问题讨论】:
-
"这个程序会比前面的多一点..",请帮我做作业好吗?似乎您甚至没有花太多精力尝试自己先解决问题。 IE。你从哪里开始又失败了?
-
现在我可以添加代码了。对不起,以前,它不会让我。
-
我确实尝试过自己先解决问题。现在我可以发布我的代码,你可以看到我从哪里开始和失败了。
-
好的,这样好多了。请耐心等待我看看。
-
谢谢。 :) 我之前无法添加代码的原因是格式问题。我尝试先在没有代码的情况下发布问题,看看它是否会通过并且有效,但我仍在想办法在其中添加代码。它使用 CTRL-K 工作。请记住,这是我第一次使用 StackOverflow。顺便说一句,我在这个实验室上花了好几个小时。
标签: java