一、Pico Fermi Bagels猜数字游戏简介
Pico Fermi Bagels(简称PFB)是一款经典的逻辑推理游戏,玩家需要根据给出的线索来猜测一个由不重复数字组成的秘密数字。游戏规则如下:
1、游戏目标:玩家需要猜出一个由特定数量(通常是3个)的不重复数字组成的秘密数字。
2、线索解释:
Pico:表示玩家猜测的数字中有一个数字是正确的,但位置不正确。
Fermi:表示玩家猜测的数字中有一个数字是正确的,并且位置也正确。
Bagels:表示玩家猜测的数字中没有任何一个数字是正确的。
3、游戏流程:
游戏开始时,系统会生成一个随机的秘密数字。
玩家在每次猜测后,系统会根据猜测的数字和秘密数字给出相应的线索。
玩家根据这些线索逐步推理出正确的数字。
玩家有一定数量的猜测次数(通常是10次),如果在次数内猜中数字,则玩家获胜;否则,游戏结束并揭示秘密数字。
4、游戏示例:
假设秘密数字是“248”,玩家第一次猜测“843”。
系统给出的线索是“Fermi Pico”,表示数字“4”位置正确,数字“8”存在但位置不正确。
Pico Fermi Bagels游戏不仅考验玩家的逻辑推理能力,还能提高玩家的注意力和记忆力。通过逐步缩小可能的数字范围,玩家可以最终猜出正确的数字。
二、python代码展示与解析
1.引入库并设置参数
代码如下:
import random
NUM_DIGITS = 3 # 数字个数
MAX_TRIES = 10 # 最大猜测次数
可以根据需要,自行修改猜测的数字个数与猜测的最大次数。
2.主函数
代码如下:
def main():
print('''Welcome to Pico Fermi Bagels!
I am thinking of a {}-digit number. Try to guess what it is.
Here are some clues:
when I say: That means:
Pico one digit is correct but in the wrong position.
Fermi one digit is correct and in the right position.
Bagels no digit is correct.
For example, if the number was 248 and your guess was 843, the clues would be Fermi Pico.
that would mean 2 is correct and in the right position, 4 is correct but in the wrong position,
and 8 is correct but not present.'''.format(NUM_DIGITS))
while True: # 循环猜测
secret_num = generate_secret_num() # 生成一个随机的数字
print('I have thought of a number. You have {} tries to guess it.'.format(MAX_TRIES))
numGuesses = 1 # 猜测次数
while numGuesses <= MAX_TRIES: # 猜测次数小于最大猜测次数
guess = '' # 初始化 guess 变量
while len(guess)!= NUM_DIGITS or not guess.isdecimal(): # 输入的数字不合法
print('Guess #{}:'.format(numGuesses))
guess = input('>')
clues = get_clues(guess, secret_num) # 得到猜测的线索
print(clues) # 输出线索
numGuesses += 1 # 猜测次数加1
if guess == secret_num: # 猜测正确
print('Congratulations, you guessed my number in {} tries!'.format(numGuesses-1))
break
if numGuesses > MAX_TRIES: # 猜测次数达到最大猜测次数
print('Sorry, you ran out of tries. The number was {}.'.format(secret_num))
#询问是否继续游戏
play_again = input('Do you want to play again? (yes or no) ')
if play_again.lower() == 'no':
break
print('Let\'s play again!')
这段代码主要是打印游戏介绍和规则:
通过generate_secret_num()函数生成一个随机的秘密数字。
提示用户猜测数字,并给出最大猜测次数为10。
循环接受用户输入,直到输入合法。
根据用户输入的猜测数字,生成线索clues并输出。
如果猜测正确,提示用户并结束当前循环。
如果猜测次数达到最大值仍未猜中,提示用户并结束当前循环。
询问用户是否继续游戏,如果选择不继续,则退出循环。
3.定义函数
def generate_secret_num():
"""生成一个随机的数字"""
digits = list(range(10)) # 数字列表
random.shuffle(digits) # 随机打乱数字列表
secret_num=''
for i in range(NUM_DIGITS):
secret_num += str(digits[i]) # 数字列表转换为字符串
return secret_num
这段代码定义了一个名为 generate_secret_num 的函数,用于生成一个随机的秘密数字。通过创建一个包含0到9的数字列表,随机打乱这个数字列表。从打乱后的列表中取出前 NUM_DIGITS 个数字,并将它们组合成一个字符串。返回这个字符串作为生成的秘密数字。
def get_clues(guess, secret_num):
"""得到猜测的线索"""
clues = ''
for i in range(len(guess)):
if guess[i] == secret_num[i]:
clues += 'Fermi '
elif guess[i] in secret_num:
clues += 'Pico '
if clues == '':
clues = 'Bagels'
return clues
这段代码定义了一个名为 get_clues 的函数,用于根据玩家的猜测和秘密数字生成相应的线索。首先通过初始化一个空字符串 clues,用于存储线索。再遍历玩家猜测的每一位数字,
根据以下规则生成线索:如果某位数字与秘密数字的对应位相同,添加 Fermi。
如果某位数字在秘密数字中但位置不对,添加 Pico。
如果没有任何数字匹配,则将 Bagels 赋值给 clues。
最后返回生成的线索字符串。
4.完整代码
"""
Pico Fermi Bagels猜数字游戏
一个逻辑脱离游戏,你必须根据线索猜数字。
"""
import random
NUM_DIGITS = 3 # 数字个数
MAX_TRIES = 10 # 最大猜测次数
def main():
print('''Welcome to Pico Fermi Bagels!
I am thinking of a {}-digit number. Try to guess what it is.
Here are some clues:
when I say: That means:
Pico one digit is correct but in the wrong position.
Fermi one digit is correct and in the right position.
Bagels no digit is correct.
For example, if the number was 248 and your guess was 843, the clues would be Fermi Pico.
that would mean 2 is correct and in the right position, 4 is correct but in the wrong position,
and 8 is correct but not present.'''.format(NUM_DIGITS))
while True: # 循环猜测
secret_num = generate_secret_num() # 生成一个随机的数字
print('I have thought of a number. You have {} tries to guess it.'.format(MAX_TRIES))
numGuesses = 1 # 猜测次数
while numGuesses <= MAX_TRIES: # 猜测次数小于最大猜测次数
guess = '' # 初始化 guess 变量
while len(guess)!= NUM_DIGITS or not guess.isdecimal(): # 输入的数字不合法
print('Guess #{}:'.format(numGuesses))
guess = input('>')
clues = get_clues(guess, secret_num) # 得到猜测的线索
print(clues) # 输出线索
numGuesses += 1 # 猜测次数加1
if guess == secret_num: # 猜测正确
print('Congratulations, you guessed my number in {} tries!'.format(numGuesses-1))
break
if numGuesses > MAX_TRIES: # 猜测次数达到最大猜测次数
print('Sorry, you ran out of tries. The number was {}.'.format(secret_num))
#询问是否继续游戏
play_again = input('Do you want to play again? (yes or no) ')
if play_again.lower() == 'no':
break
print('Let\'s play again!')
def generate_secret_num():
"""生成一个随机的数字"""
digits = list(range(10)) # 数字列表
random.shuffle(digits) # 随机打乱数字列表
secret_num=''
for i in range(NUM_DIGITS):
secret_num += str(digits[i]) # 数字列表转换为字符串
return secret_num
def get_clues(guess, secret_num):
"""得到猜测的线索"""
clues = ''
for i in range(len(guess)):
if guess[i] == secret_num[i]:
clues += 'Fermi '
elif guess[i] in secret_num:
clues += 'Pico '
if clues == '':
clues = 'Bagels'
return clues
if __name__ == '__main__':
main()
5.代码测试
Welcome to Pico Fermi Bagels!
I am thinking of a 3-digit number. Try to guess what it is.
Here are some clues:
when I say: That means:
Pico one digit is correct but in the wrong position.
Fermi one digit is correct and in the right position.
Bagels no digit is correct.
For example, if the number was 248 and your guess was 843, the clues would be Fermi Pico.
that would mean 2 is correct and in the right position, 4 is correct but in the wrong position,
and 8 is correct but not present.
I have thought of a number. You have 10 tries to guess it.
Guess #1:
>456
Fermi
Guess #2:
>555
Bagels
Guess #3:
>444
Fermi Pico Pico
Guess #4:
>476
Fermi
Guess #5:
>474
Fermi Pico
Guess #6:
>447
Fermi Pico
Guess #7:
>449
Fermi Pico Fermi
Guess #8:
>489
Fermi Fermi
Guess #9:
>429
Fermi Fermi Fermi
Congratulations, you guessed my number in 9 tries!
Do you want to play again? (yes or no)
哈哈哈,也是用9次完成了。
6.html展示
html代码链接: Pico Fermi Bagels猜数字游戏.html
html链接: Pico Fermi Bagels猜数字游戏
展示结果如图所示:
三、总结
由于最近在学相关python的东西,心血来潮,突发奇想写篇博客。由于这是我第一次用csdn写博客,便用Pico Fermi Bagels猜数字游戏这个小程序做个开头,作为博客小白浅浅尝试,还是学会了很多东西的,比如这个小程序的整体设计思路与逻辑,还有csdn撰写博客的技巧,GitHub上如何使用pages生成url网页链接的功能等。