Python实现抓狐狸小游戏
要求:实现抓狐狸游戏。假设墙上有5个洞(编号分别为0、1、2、3、4),其中一个洞里有狐狸,人类玩家输入洞口编号,如果都里有狐狸就抓到了;如果洞里没有狐狸就第二天抓。
但在第二天人类玩家来抓之前,狐狸会调到隔壁的洞里。
思路如下:
1.随机狐狸位置在洞中
2.判断用户输入洞的位置是否是狐狸所在的位置
1.如果是则抓到了
2.否则 输出没抓到 并且移动狐狸
代码如下:
'''
2020年11月2日11:05:09
5. 实现抓狐狸游戏。假设墙上有5个洞(编号分别为0、1、2、3、4),其中一个洞里有狐狸,人类玩家输入洞口编号,如果都里有狐狸就抓到了;如果洞里没有狐狸就第二天抓。
但在第二天人类玩家来抓之前,狐狸会调到隔壁的洞里。
made ChinaV
'''
import random
day = 1 # 抓狐狸的天数
holeList = [0, 0, 0, 0, 0] # 狐狸在那个洞里
holeNum = random.randint(0, 4) # 随机生成狐狸的位置
holeList[holeNum] = 1 # 狐狸在那个洞 那个洞就为1 不在为0
# print(holeList)
# 狐狸移动
def foxMove(holeList):
move = random.randint(0, 1)
if move == 1 and holeList[4] != 1: # 向右移动
old = holeList.index(1)
holeList[old] = 0
holeList[old + 1] = 1
else: # 向左移动
old = holeList.index(1)
holeList[old] = 0
holeList[old - 1] = 1
#print(holeList)
# 游戏体
while True:
while True:
try:
playerNum = int(input("请输入你洞的编号(1,2,3,4,5):")) - 1
if 0 <= playerNum <= 4:
break
except Exception:
print("输入错误,请重新输入!")
if holeList[playerNum] == 1:
print("恭喜你在第{}天的{}号洞抓到了狐狸!".format(day, playerNum + 1))
break
else:
print(playerNum + 1, "号洞没有狐狸")
day += 1
foxMove(holeList)