Python 猜拳游戏 石头剪刀布

猜拳游戏

设置总局数,设置局数,获胜要求得分必须比其高。计算机随机出拳,石头剪刀布游戏,相同则进行下一轮,通过一个字典实现剪刀石头布与123数字对应。

  • 当一方得分足够或者总局数耗尽时while循环终止,玩家输入数字对应一出拳,计算机随机生成数字对应一出拳
import random

total = int(input("请输入总局数:"))
if total % 2:
    # 输入总局数奇数时
    flag = int((total + 1) / 2)
else:
    # 输入总局数为偶数时
    flag = int((total) / 2 + 1)
print("本次游戏共{total}局,{total}局{flag}胜".
      format(total=total, flag=flag))

parscore = 0
copscore = 0
parflag, copflag = flag, flag
i = 0
dictguess = {1: '剪刀', 2: '石头', 3: '布'}
while parflag and copflag and total:
    # 1-剪刀、2-石头、3-布, 计算机随机出拳
    i += 1
    copguess = random.randint(1, 3)
    parguess = int(input("1-剪刀、2-石头、3-布,请玩家第{}次出拳:".format(i)))
    if parguess - copguess == 1 or parguess - copguess == -2:
        parscore += 1
        print("本次 人 得分,双方出拳情况:人-{}、计算机-{}".format(dictguess[parguess], dictguess[copguess]))
        parflag = flag - parscore
    elif parguess - copguess == 0:
        print("出拳相同,请重新输入")
    else:
        copscore += 1
        print("本次 计算机 得分,双方出拳情况:人-{}、计算机-{}".format(dictguess[parguess], dictguess[copguess]))
        copflag = flag - copscore

if parscore > copscore:
    print("\n本局游戏 人 获胜,最终得分人-计算机: {} - {}".format(parscore, copscore))
else:
    print("\n本局游戏 计算机 获胜,最终得分人-计算机: {} - {}".format(parscore, copscore))

运行结果

在这里插入图片描述

posted @ 2021-12-05 12:48  SKPrimin  阅读(388)  评论(0编辑  收藏  举报