python猜数字游戏_自定义难度_记录玩家历史数据

  • Post author:
  • Post category:python




本文任务

本文将使用

Python

开发一个

猜数小游戏

。在游戏中,程序每一轮会根据

玩家所选难度

随机生成一个的数字,玩家输入猜测的数字,程序告诉玩家猜大了还是猜小了。在一定次数内猜对,则本轮玩家获胜,并加分;否则本轮玩家失败,显示答案。

每一轮开始时,程序会要求玩家输入玩家名,并存放在

玩家数据字典

。玩家输入“

Y

”,开始这一轮游戏。玩家输入“

N

”,结束这一轮游戏。在每一轮游戏开始前,输入“

H

”可以查看该玩家的输入

历史数据

。在每一轮游戏中,存放玩家数据(

得分score,每轮答案answer,历史猜测guess

)到

玩家数据字典

中。



代码雏形

from random import *
difficulty = {'A' : 7,
			'B' : 8,
			'C' : 9,
			'D' : 10,}

user = input('Please input your name:')
print(f'Hello, smart {user}.\n\nThis is a game about guessing figure!\n')
difficulty_state = input(f'Please select the difficulty of the game!\n(A.easy B.medium C.difficult D.nightmare):')
game_state = input('Are you ready?(Y/N):')

while game_state == 'Y':
	times = difficulty.get(difficulty_state,7)
	answer = randint(0,2 ** difficulty.get(difficulty_state,7))
	print(f'The answer is between 0 to {2 ** difficulty.get(difficulty_state,7)},and you have {times} opportunities!')
	
	while times > 0:
		user_answer = input('Please guess:\n')
		user_answer = int(user_answer)
		if user_answer == answer:
			break
		elif user_answer > answer:
			print('The number that you guess is bigger than answer!\n')
			times -= 1
		else:
			print('The number that you guess is smaller than answer!\n')
			times -= 1
		pass
	pass

	if times > 0:
		print('YOU WIN !\n')
		break
	else:
		print('YOU FAILED T-T\n')
		print(f'The answer is {answer}')
		break
	pass
pass



部分分析

1、

困难字典


困难字典是存放困难程度所对应数字的字典。本次设计困难字典对应数字为可猜测次数,而随机数取值范围与猜测次数有关,因此随机数取值范围和猜测次数均与困难字典对应数字有关。

其中,随机数取值下限为0,取值上限 = 2 ^ 猜测次数

"""存放困难程度的字典"""
difficulty = {'A' : 7,
			'B' : 8,
			'C' : 9,
			'D' : 10,}
self.times = difficulty.get(self.difficulty_state,7)
self.low = 0
self.high = 2 ** difficulty.get(self.difficulty_state,7)

2、随机数问题,因此需要调用random库,并采用randint函数

from random import *

答案

随机数选取

self.answer = randint(0,2 ** difficulty.get(self.difficulty_state,7))

3、

玩家数据字典


初始化玩家数据字典:

Users = {}


玩家数据字典结构

Users = {user1:{'score':score1,
				'answer':[answer],
				'guess':[guess1_1,guess1_2,……],},
		 user2:{'score':score2,
				'answer':[answer2],
				'guess':[guess2_1,guess2_2,……],},
			·
			·
			·
							}

对玩家数据字典的操作:

Users[self.user]['score'] += 5
Users[self.user]['answer'].append(self.answer)
Users[self.user]['guess'].append(self.user_answer) 



代码全貌

from random import *
"""存放困难程度的字典"""
difficulty = {'A' : 7,
			'B' : 8,
			'C' : 9,
			'D' : 10,}
"""存放玩家数据的字典"""
Users = {}

class GFGame():
	"""GFGame的类"""

	def __init__(self,user,difficulty_state,game_state):
		"""initial for GFGame"""
		self.user = user
		self.difficulty_state = difficulty_state 
		self.game_state = game_state
		#根据困难状态标志位在困难字典中寻找,并确定猜测次数和所在范围的答案。若不存在,默认简单模式。
		self.times = difficulty.get(self.difficulty_state,7)
		self.answer = randint(0,2 ** difficulty.get(self.difficulty_state,7))
		#读取玩家历史数据。若无该玩家数据,新建玩家数据。
		if Users.get(user,0) == 0:
			self.score = 0
			info = {self.user:{'score':self.score,
								'answer':[self.answer],
								'guess':[],},}
			Users.update(info) 
		else:
			Users[self.user]['answer'].append(self.answer)
		pass
	pass

	def Guess(self):
		"""Guess for the answer of GFGame"""
		self.user_answer = input('Please guess:\n')
		#将玩家输入的字符数字转换为整型
		self.user_answer = int(self.user_answer)
		#记录玩家猜测的数据
		Users[self.user]['guess'].append(self.user_answer) 
	pass

	def Prompt(self):
		"""Prompt about answer for user"""
		if self.user_answer == self.answer:
			self.times -= 0
		elif self.user_answer > self.answer:
			print('The number that you guess is bigger than answer!\n')
			self.times -= 1
		else:
			print('The number that you guess is smaller than answer!\n')
			self.times -= 1
		pass
	pass

	def Judge(self):
		"""Judge about user'answer """
		if self.times > 0:
			print('YOU WIN !\n')
			#答对:+5分
			Users[self.user]['score'] += 5
		else:
			print('YOU FAILED T-T\n')
			#答错:给出答案数字
			print(f'The answer is {self.answer}')
		pass
	pass

	
if __name__ == '__main__':

	while(1):
		#输入用户名
		user = input('Please input your name:')
		print(f'Hello, smart {user}.\n\nThis is a game about guessing figure!\n')
		#给出游戏状态game_state,Y表示开始游戏,N表示结束游戏,H表示查询当前玩家的历史数据
		game_state = input('Are you ready?(Y/N/H):')
		#开始游戏
		if game_state == 'Y':
			difficulty_state = input(f'\nPlease select the difficulty of the game!\n(A.easy B.medium C.difficult D.nightmare):')
			user_1 = GFGame(user,difficulty_state,game_state)
			print(f'The answer is between 0 to {2 ** difficulty.get(user_1.difficulty_state,7)},and you have {user_1.times} opportunities!')
			while(user_1.times > 0):
				user_1.Guess()
				user_1.Prompt()
				if user_1.user_answer == user_1.answer:
					break
			user_1.Judge()
		#结束游戏
		elif game_state == 'N':
			continue
		#查看玩家数据
		else:
			if Users.get(user,0) == 0:
				print(f'There is no information about {user}')
			else:
				print(f"The history of {user} is :\n")
				print(f"score:{Users[user]['score']}\n")
				print(f"answer:{Users[user]['answer']}\n")
				print(f"guess:{Users[user]['guess']}\n")
			pass
		pass
	pass



运行结果

在这里插入图片描述



版权声明:本文为m0_56555154原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。