参考文章:https://www.jianshu.com/p/42c4bc3e16c7
#三局两胜(赢1平2也为胜)
#定义赢一次加一分,输一次减一分,平局不加分也不减分
#如果分值大于0为赢,等于0为平,小于0为输
import random
choice_lists = ['石头', '剪刀', '布']
play_num = 0 #统计游戏次数
win_num = 0 #统计赢的次数
lose_num = 0 #统计输的次数
score = 0 #统计分数
# 列出电脑赢的三种情况
win_lists = [['石头', '剪刀'], ['剪刀', '布'], ['布', '石头']]
print('\033[32;1m游戏开始,三局两胜,Reding Go......\033[0m')
print('1.石头 2.剪刀 3.布')
#############################################################################
#定义函数,等待用户是否继续游戏
def UserSelect():
print('-' * 40)
print('\n是否继续? 继续请输入1,退出请输入0')
select = int(input('输入0/1: '))
if select == 1:
global play_num,score,win_num,lose_num
play_num = 0 # 游戏次数清零
score = 0 # 分数清零
win_num = 0
lose_num = 0
else:
print('\033[34;1m游戏结束,欢迎下次再来!!!\033[0m')
exit()
#########################主程序#################################
while True:
computer = random.choice(choice_lists) #电脑出的拳
num = int(input('输入[1-3]: '))
if num not in [1, 2, 3]:
print('\033[31;1m输入错误,请重新输入!\033[0m')
continue
people = choice_lists[num - 1] #用户出的拳
play_num += 1
print('Computer is %s,People is %s' %(computer,people))
if people == computer:
print('\033[36;1m这把平局\033[0m')
elif [computer,people] in win_lists:
print('\033[31;1m这把输了\033[0m')
lose_num += 1
else:
print('\033[92;1m这把赢了\033[0m')
win_num += 1
if play_num == 2 and win_num == 2: #两局全赢和两句全输的情况
print('\033[92;1m真棒,两把就赢了!!!\033[0m')
UserSelect()
if play_num == 2 and lose_num == -2:
print('\033[31;1m运气不佳啊,都不用玩第三把就输了!!!\033[0m')
UserSelect()
if play_num == 3: #判断最终结果
score = win_num - lose_num #计算分数
print('本轮赢%s次,平%s次,输%s次' % (win_num, (3 - win_num - lose_num),lose_num,))
if score > 0:
print('\033[94;1m最终结果:你赢了\033[0m')
elif score == 0:
print('\033[94;1m最终结果:平局\033[0m')
else:
print('\033[94;1m最终结果:你输了\033[0m')
UserSelect()