1、 有游戏开始界面(游戏名称、学生专业、学号、姓名、游戏介绍)。
2、 游戏有两种形式,二人游戏/三人游戏(其中一人为游戏者,剩下都为系统随机)。
3、 输入E/e退出二人游戏/三人游戏,可以重新选择游戏形式,输入Q/q退出整个游戏,否则始终在游戏界面,不能异常退出。
4、 从键盘接收到不合法数据时,能够处理异常,不能退出。
5、 符合游戏规则。
代码实现:
import random
print('***********************************\n'
'*游戏名称: *\n'
'*学生专业: *\n'
'*学号: *\n'
'*姓名: *\n'
'*游戏介绍: *\n'
'***********************************\n')
def liangren(player,computer1):
#比较胜负
if player>=1 and player<=3:
if ((player==1 and computer1==2)
or (player==2 and computer1==3)
or (player==3 and computer1==1)):
print("玩家胜利!")
elif (player==computer1):
print("平局,再来!")
else:
print("电脑胜利,不服气!")
else:
print("玩家输入不合法哦")
def sanren(player,computer1,computer2):
#比较胜负
if player>=1 and player<=3:
if (computer1==computer2 and computer1==player):
print("平局")
elif player == 3 and (computer1==2 or computer2==2):
print("你输了")
elif player == 2 and (computer1==1 or computer2==1):
print("你输了")
elif player == 1 and (computer1==3 or computer2==3):
print("你输了")
else:
print("你赢了")
else:
print("玩家输入不合法哦")
while 1:
#选择模式
moshi = int(input("请选择游戏模式(2,3)"))
if moshi == 2:
while 2:
computer1 = random.randint(1,3)
#从控制台输入所要出的拳 石头1 剪刀2 布3
player = int(input("请输入您要出的拳(石头1 剪刀2 布3):"))
print("玩家出的拳是%s,电脑出的拳是%d" % (player,computer1))
print(liangren(player,computer1))
player = str(input("再来(输入任意键)或重选模式(E/e):"))
if player == '任意按键':
continue
elif player == 'E' or player == 'e':
break
if moshi == 3:
while 3:
computer1 = random.randint(1,3)
computer2 = random.randint(1,3)
#从控制台输入所要出的拳 石头1 剪刀2 布3
player = int(input("请输入您要出的拳(石头1 剪刀2 布3):"))
print("玩家出的拳是%s,电脑1出的拳是%d,电脑2出的拳是%d" % (player,computer1,computer2))
print(sanren(player,computer1,computer2))
player = str(input("再来(输入任意键)或重选模式(E/e):"))
if player == '任意按键':
continue
elif player == 'E' or player == 'e':
break
player = str(input("是否退出游戏?(Q/q或N/n)"))
if player == 'Q' or player == 'q':
break
elif player == 'N' or player == 'n':
continue