我正在编写一个应该玩HiLo游戏的程序。 HiLo是一款游戏,其中会生成一个随机数,玩家必须猜测它。如果他们猜错了,他们会被告知答案是高于还是低于他们的猜测,然后允许他们再次猜测。如果需要,我的程序应该能够限制猜测的数量,设置可以从中选择答案的值的范围(因此也可以猜测的范围)。我还需要选择一个人是否会玩游戏,或者是否让计算机玩,如果是的话,以什么技能水平(差,中,好)进行选择。

我已经写了3类:GameManager,HiLo和Player。

GameManager类允许玩多个游戏并跟踪获胜次数。

public class GameManager {

private HiLo game;
private int wins;
private int totalRounds;
private int player;

/**
 * Constructor for objects of class GameManager
 */
public GameManager()
{
    player = 2;
}

public void playGames(int rounds)
{
    int i = 0;
    while(i < rounds) {
        game = new HiLo(player);
        game.play();
        wins += game.getHasWon();
        i++;
    }
    totalRounds += rounds;
}

public void changePlayer(int playerType)
{
    player = playerType;
}
} //This should be indented better, but it won't format properly, apologies


HiLo课程基本上是设置并玩HiLo的个人游戏。

public class HiLo {

private int guessLimit;
private int lowerLimit;
private int upperLimit;
private int guesses;
private int answer;
private boolean hasWon;
private Player player;
private String result;
/**
 * Constructor for objects of class HiLo
 */
public HiLo(int playerType) //Set default values
{
    guessLimit = 10;
    lowerLimit = 0;
    upperLimit = 50;
    hasWon = false;
    player = new Player(playerType);
}
public HiLo(int guessLimit, int lowerLimit, int upperLimit, int playerType) //Overload
{
    this.guessLimit = guessLimit;
    this.lowerLimit = lowerLimit;
    this.upperLimit = upperLimit;
    hasWon = false;
    player = new Player(playerType);
}
//Returns lower limit
public int getLowerLimit()
{
    return lowerLimit;
}
//Returns upper limit
public int getUpperLimit()
{
    return upperLimit;
}
//Returns 1 if hasWon is true and 0 if hasWon is false
public int getHasWon()
{
    if(hasWon) {
        return 1;
    } else {
        return 0;
    }
}
//Returns the number of guesses so far
public int getGuesses()
{
    return guesses;
}
//Play a game of HiLo
public void play()
{
    answer = randInt(lowerLimit, upperLimit);
    guesses = 0;
    while(guesses < guessLimit) {
        int guess = player.guess(); //This gets a guess from the player
        result = isCorrect(guess); //Was the guess correct?
        if(result.equals("Win")) {
            break; //If the player has won, break from the while loop
        }
        guesses++;
    }

}
//Checks if the guess was correct
public String isCorrect(int guess)
{
    if(guess == answer) {
        hasWon = true;
        return "Win";
    } else if(guess < answer) {
        lowerLimit = guess;
        return "Higher";
    } else {
        upperLimit = guess;
        return "Lower";
    }

}

public int randInt(int min, int max)
{
    Random rand = new Random();
    int randomNum = rand.nextInt((max - min) + 1) + min;
    return randomNum;
}
}


玩家类别确定哪个玩家正在玩(0 =人类,1 = badComp,2 = medComp,3 = goodComp)以及他们将用来猜测的策略。

import java.util.Random;
import java.util.Scanner;

public class Player {

private int playerType;
private HiLo game;
private int range;
private int lowestLimit; //This is the starting value of lowerLimit

/**
 * Constructor for objects of class Player
 */
public Player(int playerType)
{
   if(playerType < 0 || playerType > 3) {
       System.out.println("ERROR. Please pick a valid playerType");
   }
   this.playerType = playerType;
   range = 0;
   range = setRange();
   lowestLimit = game.getLowerLimit();
}

public int guess()
{
    if(playerType == 0) {
        return humanPlayer();
    } else if(playerType == 1) {
        return badComputerPlayer();
    } else if(playerType == 2) {
        return mediumComputerPlayer();
    } else {
        return goodComputerPlayer();
    }
}
//Guess from a good computer player
public int goodComputerPlayer()
{
   return (game.getUpperLimit() - game.getLowerLimit())/2;
}
//Guess from a medium computer player
public int mediumComputerPlayer()
{
    return randInt(game.getLowerLimit(), game.getUpperLimit());
}
//Guess from a bad computer player
public int badComputerPlayer()
{
    return randInt(lowestLimit, range);
}

public int humanPlayer()
{
    Scanner keyboard = new Scanner(System.in);
    System.out.println("enter an integer");
    int myint = keyboard.nextInt();
    return myint;
}
//Generates a random number in the range maz-min
public int randInt(int min, int max)
{
    Random rand = new Random();
    int randomNum = rand.nextInt((max - min) + 1) + min;
    return randomNum;
}
//Set the range of values to guess from for bad player
public int setRange()
{
    int r = game.getUpperLimit() - game.getLowerLimit();
    return r;
}
}


当我从HiLo类创建对象并输入所有参数时,出现NullPointException:

java.lang.NullPointerException
at Player.setRange(Player.java:75)
at Player.<init>(Player.java:26)
at HiLo.<init>(HiLo.java:35)


我是编程和Java的新手,这是我的第一个NullPointException错误。我已经阅读了其他一些文章,但我无法弄清楚。

我也不认为播放器类也不完全按照我的要求做。从概念上讲,Player类的每个新实例都与创建该实例的HiLo游戏实例相关,因为HiLo游戏一次只能有一个玩家。我希望Player类的新实例引用创建它的HiLo类的实例:

lowestLimit = game.getLowerLimit();


我正在尝试将lowerLimit =设置为创建此Player实例的HiLo游戏实例的下限,但是我认为这只是引用从HiLo类创建的对象游戏,而不是创建此实例的实例。播放器实例。抱歉,如果这令人困惑,我正在尽力解释:)

我也在Player类中的许多其他方法中执行此操作。我已经读过一些有关使用反射来潜在地解决此问题的知识,但我真的不确定我是否理解。

任何帮助和/或想法将不胜感激,

谢谢。

编辑:只是以为我会让你知道我正在使用BlueJ环境。

最佳答案

setRange()方法取决于game不是null。因为setRange()是在Player的构造函数中调用的,所以由于NullPointerException实例变量尚未初始化,因此它将始终抛出game

解决此问题的一种方法是使Player的构造函数接受HiLo参数,并在调用game之前将setRange()设置为该值:

public Player(int playerType, HiLo game) {
    this.game = game;
    // ...
}


现在,您需要更新player = new Player(playerType);语句以使用正确的参数。因为该语句在HiLo的实例方法中,所以我们可以简单地将this作为第二个参数传递:

player = new Player(playerType, this);

10-08 07:05