猜猜游戏逻辑

时间:2015-09-08 09:39:09

标签: java

我正在尝试创建一个游戏,其中AI试图猜测用户通过一系列问题思考的数字。

示例:

Is your number greater than 50? (y/n)
n
Is your number greater than 25? (y/n)
y
Is your number greater than 38? (y/n)
y
Is your number greater than 44? (y/n)
n
Is your number greater than 41? (y/n)
y
Is your number greater than 43? (y/n)
y
The number you're thinking of is 44.

我知道您必须根据用户输入的内容设置新的upperLimit和lowerLimit,然后返回新限制的平均值。我的代码效果不好。

如果我想到数字59,我的代码会做什么。

Is your number greater than 50? (y/n)
y
Is your number greater than 75? (y/n)
n
Is your number greater than 62? (y/n)
n
Is your number greater than 56? (y/n)
y
Is your number greater than 59? (y/n)
n
Is your number greater than 57? (y/n)
y
Is your number greater than 58? (y/n)
y
Is your number greater than 58? (y/n)
y
Is your number greater than 58? (y/n)
y
Is your number greater than 58? (y/n)
y
Is your number greater than 58? (y/n)

非常感谢任何建议/提示/帮助!

我的代码:

public void play(int lowerLimit, int upperLimit) {
    instructions(lowerLimit, upperLimit);

    while (lowerLimit != upperLimit) {
        if(isGreaterThan(average(lowerLimit, upperLimit))) {
            lowerLimit = average(lowerLimit, upperLimit);
        } else {
            upperLimit = average(lowerLimit, upperLimit);
        }
    }
    System.out.println("The number you're thinking of is " + lowerLimit);
}

public boolean isGreaterThan(int value) {
    System.out.println("Is your number greater than " + value + "? (y/n)");
    String answer = reader.nextLine();
    return answer.equals("y");
}

public int average(int firstNumber, int secondNumber) {
    return (firstNumber + secondNumber) / 2;
}

3 个答案:

答案 0 :(得分:3)

问题与

有关
return (firstNumber + secondNumber) / 2;

这是使用整数除法,因此当firstNumber = 58secondNumber = 59结果为58时。因此,下限永远不会收敛到上限。

答案 1 :(得分:3)

尝试此lowerLimit = average(lowerLimit,upperLimit)+ 1;

解释非常简单。你一直在问的问题是这个数字是否大于X.如果是,X + 1应该成为下限,因为很明显这个数字不能是X.同样的不能在间隔的另一端应用,因为如果你的数字是X,问题的答案是更大的是否,所以upperLimit应该保持X.

答案 2 :(得分:0)

这是一个非常适合您的计划的解决方案:

<强>代码

public class Program {
    public static void main(String[] args) {
        System.out.println("The number you're thinking of is " + new Program().play(0, 100));
    }

    public void instructions(int lowerLimit, int upperLimit) {
        //xxx
    }

    public int play(int lowerLimit, int upperLimit) {
        instructions(lowerLimit, upperLimit);
        Scanner reader = new Scanner(System.in);
        while (true) {
            switch (upperLimit - lowerLimit) {
                case 0:
                    return lowerLimit;
                case 1:
                    System.out.println("Is your number " + upperLimit + "? (y/n)");
                    boolean upper = "y".equals(reader.nextLine());
                    return upper ? upperLimit : lowerLimit;
                default:
                    int x = (lowerLimit + upperLimit) / 2;
                    System.out.println("Is your number greater than " + x + "? (y/n)");
                    boolean greater = "y".equals(reader.nextLine());
                    if (greater) {
                        lowerLimit = x + 1;
                    } else {
                        upperLimit = x;
                    }
                    break;

            }
        }
    }
}

<强>输出

Is your number greater than 50? (y/n)
n
Is your number greater than 25? (y/n)
y
Is your number greater than 38? (y/n)
y
Is your number greater than 44? (y/n)
n
Is your number greater than 41? (y/n)
y
Is your number greater than 43? (y/n)
n
Is your number 43? (y/n)
n
The number you're thinking of is 42