0
我一直在試圖在Java中製作一款名爲「豬」的遊戲,其中一名玩家擲出2個骰子。玩家根據他們擲出的東西賺取點數,例如如果玩家擲出5和4,他現在擁有9分,但是如果玩家擲出1,他們就不會獲得積分並且控制切換到計算機,並且如果玩家擲出蛇眼( 1和1),玩家將所有點和控制開關切換到計算機。我有遊戲的工作,但我需要停止計算機後,它獲得20分。無論哪位球員首先贏得100場勝利。如何讓我的遊戲中的電腦玩家在一回閤中獲得20分後停止?
這裏是我的代碼:
//this program runs 4.4
import java.util.Scanner;
public class run {
public static void main(String[] args)
{Scanner scan = new Scanner (System.in);
dice thing = new dice();
dice thing1 = new dice();
boolean control=true;
int x = 0,points=0,a,b,y=0,c,d,turn=0,save=0,no=0;
while(x<100 && y<100)
{
while (control == true && x<100)
{
a=thing.roll();
b=thing1.roll();
if (a == 1 && b==1)
{
control=false;
System.out.println("your first die rolled a " + thing.getroll() + " second die rolled a " + thing1.getroll()+"computer now has control");
x=0;
}
else if(a == 1 || b==1)
{
control = false;
System.out.println("your first die rolled a " + thing.getroll() + " second die rolled a " + thing1.getroll() + " now you have " + x +" points "+"computer now has control");
}
else
{
x +=(a+b);
System.out.println("your first die rolled a " + thing.getroll() + " your second die rolled a " + thing1.getroll() + " now you have " + x +" points");
System.out.println(" would you like to end your turn put in 1 for yes 0 for no");
points = scan.nextInt();
if(points==1)
{
control= false;
}
if (x>=100)
{
System.out.println("you win");
}
}
}
while (control==false && y<100)
{
c=thing.roll();
d=thing1.roll();
if (c == 1 && d==1)
{
control=true;
System.out.println("The computers first die rolled a " + thing.getroll() + " The computers second die rolled a " + thing1.getroll()+"you now have control");
turn = y;
y=0;
}
else if(c == 1 || d==1)
{
control = true;
System.out.println("The computers first die rolled a " + thing.getroll() + " The computers second die rolled a " + thing1.getroll()+ " now the computer has " + y +" points "+"you now have control");
turn = y;
}
else
{
y +=(c+d);
System.out.println("The computers first die rolled a " + thing.getroll() + " The computers second die rolled a " + thing1.getroll() + " now the computer has " + y +" points");
turn = y;
if (y>=100)
{
System.out.println("you lose");
}
}
}
}
}
}
你怎麼會在轉彎獲得20分?你最多可以獲得12(6 + 6)?或者我們處理的是六邊以上的骰子? –
@MarcB在現實生活中[豬](https://en.wikipedia.org/wiki/Pig_%28dice_game%29),至少,你的回合不會在一輪之後結束。當你擲出1(本回閤中你棄掉分數)或者當你選擇持有時(你保留本回合的分數),你的回合結束。我沒有看過代碼,看看這裏是否也是如此。 – dcsohl