猜数字游戏优化(用户创建数字,计算机猜测) [英] Guess the number game optimization (user creates number, computer guesses)

查看:112
本文介绍了猜数字游戏优化(用户创建数字,计算机猜测)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对编程非常陌生,所以大约4或5天前我决定从Python开始。我遇到了一个挑战,要求我创建猜数字游戏。完成后,艰巨的挑战是创建一个猜猜数字游戏,由用户创建数字,然后由计算机(AI)猜猜。

I am very new to programming so I decided to start with Python about 4 or 5 days ago. I came across a challenge that asked for me to create a "Guess the number" game. After completion, the "hard challenge" was to create a guess the number game that the user creates the number and the computer (AI) guesses.

到目前为止,我已经提出了这个方法并且它可以工作,但是可能会更好,我会解释。

So far I have come up with this and it works, but it could be better and I'll explain.

from random import randint

print ("In this program you will enter a number between 1 - 100."
       "\nAfter the computer will try to guess your number!")

number = 0

while number < 1 or number >100:
    number = int(input("\n\nEnter a number for the computer to guess: "))
    if number > 100:
        print ("Number must be lower than or equal to 100!")
    if number < 1:
        print ("Number must be greater than or equal to 1!")

guess = randint(1, 100)

print ("The computer takes a guess...", guess)

while guess != number:
    if guess > number:
        guess -= 1
        guess = randint(1, guess)
    else:
        guess += 1
        guess = randint(guess, 100)
    print ("The computer takes a guess...", guess)

print ("The computer guessed", guess, "and it was correct!")

这是我上次运行时发生的事情:

This is what happened on my last run:

输入一个数字要猜测的计算机:78

Enter a number for the computer to guess: 78

计算机要猜测... 74

The computer takes a guess... 74

计算机要猜测... 89

The computer takes a guess... 89

计算机进行猜测... 55

The computer takes a guess... 55

计算机进行猜测.. 。78

The computer takes a guess... 78

计算机猜到了78,它是正确的!

The computer guessed 78 and it was correct!

请注意,它可以工作,但是当计算机猜到74,然后猜到更高的数字到89。这个数字太高了,因此计算机猜到了一个较小的数字,但是选择的数字是55。有没有办法让我的计算机猜到一个小于89的数字,但高于74?

Notice that it works, however when the computer guessed 74, it then guessed a higher number to 89. The number is too high so the computer guesses a lower number, however the number chosen was 55. Is there a way that I can have the computer guess a number that is lower than 89, but higher than 74? Would this require additional variables or more complex if, elif, else statements?

谢谢Ryan Haining

我使用了您的回复中的代码,并对其进行了一些更改,因此猜测总是随机的。如果您看到此消息,请告诉我这是否是最好的方法。

I used the code from your reply and altered it slightly so the guess is always random. If you see this, let me know if this is the best way to do so.

from random import randint

def computer_guess(num):
    low = 1
    high = 100
    # This will make the computer's first guess random
    guess = randint(1,100)
    while guess != num:
        print("The computer takes a guess...", guess)
        if guess > num:
            high = guess
        elif guess < num:
            low = guess + 1
        # having the next guess be after the elif statement
        # will allow for the random guess to take place
        # instead of the first guess being 50 each time
        # or whatever the outcome of your low+high division
        guess = (low+high)//2    

    print("The computer guessed", guess, "and it was correct!")


def main():
    num = int(input("Enter a number: "))
    if num < 1 or num > 100:
        print("Must be in range [1, 100]")
    else:
        computer_guess(num)

if __name__ == '__main__':
    main()


推荐答案

您正在寻找什么经典的二进制搜索算法

what you are looking for is the classic binary search algorithm

def computer_guess(num):
    low = 1
    high = 100
    guess = 50
    while guess != num:
        guess = (low+high)//2
        print("The computer takes a guess...", guess)
        if guess > num:
            high = guess
        elif guess < num:
            low = guess + 1

    print("The computer guessed", guess, "and it was correct!")


def main():
    num = int(input("Enter a number: "))
    if num < 1 or num > 100:
        print("Must be in range [1, 100]")
    else:
        computer_guess(num)

if __name__ == '__main__':
    main()

该算法通过选择以(在您的情况下,low = 1和high = 100)。然后检查它们之间的中点。

The algorithm works by selecting a low and high limit to start with (in your case low=1 and high=100). It then checks the midpoint between them.

如果中点小于数字,则中点将成为新的下界。如果中点更高,它将成为新的上限。完成此操作后,将在上限和下限之间生成一个新的中点。

If the midpoint is less than number, the midpoint becomes the new lower bound. If the midpoint is higher, it becomes the new upper bound. After doing this a new midpoint is generated between the upper and lower bound.

为了说明一个示例,假设您要查找82。

To illustrate an example let's say you're looking for 82.

这里是一个示例运行

Enter a number: 82
The computer takes a guess... 50
The computer takes a guess... 75
The computer takes a guess... 88
The computer takes a guess... 82
The computer guessed 82 and it was correct!

那么每一步都在做什么?

So what's happening here in each step?


  1. low = 1 high = 100 => 猜测= 50 50< 82所以 low = 51

  2. low = 51 最高= 100 => 猜测= 75 75< 82所以 low = 76

  3. low = 76 最高= 100 => 猜测= 88 88> 82所以最高= 88

  4. 低= 76 高= 88 => 猜测= 82 82 == 82,我们就完成了。

  1. low = 1, high = 100 => guess = 50 50 < 82 so low = 51
  2. low = 51, high = 100 => guess = 75 75 < 82 so low = 76
  3. low = 76, high = 100 => guess = 88 88 > 82 so high = 88
  4. low = 76, high = 88 => guess = 82 82 == 82 and we're done.

请注意,时间复杂度其中是 O(lg(N))

Note that the time complexity of this is O(lg(N))

这篇关于猜数字游戏优化(用户创建数字,计算机猜测)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆