You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java中computer方法在bonegame执行后无法运行的问题排查

Issue with CPU Method in Turn-Based Game

I'm developing a turn-based game where players compete against a CPU. The player portion is complete, but the computer CPU method fails to run as expected. According to the design, this method should start immediately after the player's turn (when the bonegame method finishes executing). The expected flow is: after bonegame completes one player turn, switch to the CPU turn, alternating until either side reaches 20 points.

Relevant Code

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;
  }
}

内容的提问来源于stack exchange,提问作者Sponge curry

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 10:32:07