我正在尝试创建一个玩家与 CPU 的游戏。我已经完成了播放器部分。但是名为 'computer 的 Cpu 方法将不会运行。理论上,“计算机”方法应该在玩家回合结束后运行。
import java.util.Scanner;
import java.util.Random;
class Main {
public static void main(String[] args) {
// acknowledgements
System.out.print("welcome to the Bone game --- ");
System.out.println("Created by the indigenous Blackfoot tribe");
System.out.println("=======================================================================");
int playerScore = bonegame(10);
System.out.println("Player score: " + playerScore);
int computerScore = computer(10);
System.out.println("Computer score: " + computerScore);
if (playerScore > computerScore) {
System.out.println("Player wins!");
} else if (computerScore > playerScore) {
System.out.println("Computer wins!");
} else {
System.out.println("It's a tie!");
}
bonegame(10);
score1();
}
//bone game method
public static int bonegame(int round){
// variables
Random random = new Random();
//int cir = 3,dia = 4,tri = 2,lin = 1,che = 1,blanc = 0;
int cir = random.nextInt(2);
int dia = random.nextInt(2);
int tri = random.nextInt(2);
int lin = random.nextInt(2);
int che = random.nextInt(2);
//int comp = computer(cpu);
String c = "circle";
String bl = "blank";
String d = "diamond";
String t = "triangle";
String l = "line";
String check = "checkered";
round = 10;
int reroll = 0;
int score = 0;
while (round >= 0 || score <= 20){
//System.out.println(reroll);
System.out.println("round: "+ round);
//System.out.println("your score: " + score);
// System.out.println("computer score: "+ cpu);
System.out.println("--------------------------");
if(cir ==1){
score += 3;
System.out.println(c);
}
if(dia ==1){
score += 4;
System.out.println(d);
}
if(tri ==1){
score += 2;
System.out.println(t);
}
if(lin ==1){
score += 1;
System.out.println(l);
}
if(che ==1){
score += 1;
System.out.println(check);
}
//blanks
if(cir ==0){
System.out.println(bl);
}
if(dia ==0){
System.out.println(bl);
}
if(tri ==0){
System.out.println(bl);
}
if(lin ==0){
System.out.println(bl);
}
if(che ==0){
System.out.println(bl);
}
if (reroll == 3) {
System.out.println("You have used all your rerolls.");
break;
}
if (score >= 20){
System.out.println("You win:"+ score);
break;
}
System.out.print("current score: ");
System.out.println(score);
System.out.println("-----------------");
//accept y or n
Scanner play = new Scanner(System.in);
System.out.println("To reroll type y ---- to continue type n");
String move = play.nextLine();
if (move.equals("y")){
reroll ++;
score = 0;
cir = random.nextInt(2);
dia = random.nextInt(2);
tri = random.nextInt(2);
lin = random.nextInt(2);
che = random.nextInt(2);
round = 10;
}
else {
round --;
}
}
return score;
}
//score
public static int score1() {
int scored = bonegame(10);
System.out.println("Player score: " + scored);
System.out.println(" ");
return scored;
}
// Computer turn
public static int computer(int cpu){
Random random1 = new Random();
int cir = random1.nextInt(2);
int dia = random1.nextInt(2);
int tri = random1.nextInt(2);
int lin = random1.nextInt(2);
int che = random1.nextInt(2);
int ron = bonegame(10);
String c = "circle";
String bl = "blank";
String d = "diamond";
String t = "triangle";
String l = "line";
String check = "checkered";
while (ron >= 0 || cpu <= 20){
System.out.println("--------------------------");
if(cir ==1){
cpu += 3;
System.out.println(c);
}
if(dia ==1){
cpu += 4;
System.out.println(d);
}
if(tri ==1){
cpu += 2;
System.out.println(t);
}
if(lin ==1){
cpu += 1;
System.out.println(l);
}
if(che ==1){
cpu += 1;
System.out.println(check);
}
//blanks
if(cir ==0){
System.out.println(bl);
}
if(dia ==0){
System.out.println(bl);
}
if(tri ==0){
System.out.println(bl);
}
if(lin ==0){
System.out.println(bl);
}
if(che ==0){
System.out.println(bl);
}
if (cpu >= 20){
System.out.println("You lost:"+ cpu);
break;
}
System.out.print("current CPU score: ");
System.out.println(cpu);
System.out.println("-----------------");
}
return cpu;
}
}
在'bonegame'方法允许用户一轮之后,下一轮将是CPU的,直到一个达到20分。