[英]How can i make the computer player in my game stop after it has earned 20 points in one turn?
我一直在嘗試用java制作一個叫做Pig的游戲,其中玩家擲出2個骰子。 玩家根據自己的擲骰數獲得積分,例如,如果玩家擲5和4,他現在有9分,但是如果玩家擲1,則不會獲得積分,並且控制權切換到計算機,並且如果玩家擲蛇眼( 1和1),玩家失去了所有積分,控制權轉移到了計算機上。 我有游戲正常運行,但在其輪流獲得20分后,我需要停止計算機。 哪個玩家擊中100個首勝。
這是我的代碼:
//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");
}
}
}
}
}
}
嘗試添加:
if (turn >= 20){
control = true;
turn = 0;
}
后:
while (control==false && y<100)
{
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.