一個循環的做法。這個計劃將會涉及到前一個。
對於這個程序,你將創建一個模擬骰子游戲豬。
該遊戲的目標是讓玩家在對手之前獲得100分,其他細節將在任務中討論。
在這種情況下,對手將是電腦。
任務1
這個遊戲需要兩個6面骰子。你需要用隨機數發生器模擬這些骰子。
兩個骰子(隨機數生成器)必須:1-6 之間
農產品值具有140L和340L的種子,分別爲模具和一個模2(用於測試目的) 任務2
現在你已經有了骰子,我們可以重複你將要玩的規則。
規則:
對於每一回合,玩家將輪流滾動兩個骰子。 如果一個骰子沒有出現在骰子上,這些值就會加到玩家的總數上。然後他們可以選擇再次擲出(選擇0)或將轉牌傳給其他玩家(選擇1)。 如果某個骰子出現1,則該玩家在整個回合中獲得的總積分不得增加,併成爲其他玩家的回合(前一回合獲得的積分將依然存在於其總數中)。 如果玩家擲出兩個1s,則玩家的回合結束並且他們的總數被重置爲0. 您需要一個隨機數字生成器來確定計算機(0)或玩家(1)是否會先玩。這也將被用來模擬計算機的選擇,以再次滾動或通過翻轉。這臺發電機的種子將是140L。
假定來自用戶的有效輸入。
輸出應該
以下所有用戶提示應該類似於語句「豬的博弈歡迎」開頭:
輪到你了(目前積分:0) 您推出3和2,分獲得本回合:5 按0再次或1卷啓動電腦的回合 和所有計算機的提示應該是這樣的:
電腦的回合(當前點:0) 電腦推出1和4,沒有點賺取的輪到你 他們宣佈哪個玩家的回合和當前分數。這是由什麼播放器,然後
如果分榮獲推出
然後是數字,顯示贏得了該輪(看到用戶的提示2號線) 如果一個被軋總積分,宣佈獲得無分,接下來的玩家回合(見電腦提示2號線) 如果同時的,利用顯示消息「取其播放器/捲起兩個1 /,點重置和/對手的/轉」
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
「這個程序會比以前更復雜..」,請爲我做作業嗎?看起來你並沒有付出太多努力,甚至試圖自己先解決問題。即你在哪裏開始和失敗? –
現在我可以添加代碼。對不起,以前,它不會讓我。 – Muhammad
我確實試圖首先自己解決問題。現在我能夠發佈我的代碼,可以看到我開始和失敗的位置。 – Muhammad