python实现人和机器剪刀石头布猜拳游戏类

≯℡__Kan透↙ 提交于 2019-11-27 20:19:46
人和机器猜拳游戏写成一个类,有如下几个函数:
1)函数1:选择角色 1 曹操 2张飞 3 刘备
2)函数2:角色猜拳 1剪刀 2石头 3布 玩家输入一个1-3的数字
3)函数3:电脑出拳 随机产生1个1-3的数字,提示电脑出拳结果
4)函数4:角色和机器出拳对战,对战结束后,最后出示本局对战结果...赢...输,然后提示用户是否继续?按y继续,按n退出。
5)最后结束的时候输出结果 角色赢几局 电脑赢几局,平局几次 游戏结束

华华老师的写法:

class HumanVSMachine:
    def choose_role(self):
        while True:
            role_info={"1":"曹操","2":"张飞","3":"刘备"}
            role_num=input("请选择你喜欢的角色:1:曹操 2:张飞 3:刘备")
            if role_num in role_info.keys():
                print("你选择的角色是%s"%role_info[role_num])
                break
            else:
                print("角色选择错误,请重新选择!")
                continue
        #返回一个值  返回角色值
        return role_info[role_num]
     #方法一:
    def cq(self,role,mode):#mode=1  人出拳 mode=2  就是电脑出拳
        cq_info={"1":"石头","2":"剪刀","3":"布"}
        if mode==1:
            cq_num=input("请输入对应的数字出拳:1石头 2剪刀 3布")
        elif mode==2:
            cq_num=str(random.randint(1,3))
        if cq_num in cq_info.keys():
            print(role+"出的是%s"%cq_info[cq_num])
        else:
            print("出拳错误!")
        return cq_num
    # 方法二:
    # def role_cq(self,role_name):
    #     cq_num=input("请输入对应的数字出拳:1剪刀 2石头 3布")
    #     cq_info={"1":"剪刀","2":"石头","3":"布"}
    #     if cq_num in cq_info.keys():
    #         print(role_name+",你出的是%s"%cq_info[cq_num])
    #     else:
    #         print("出拳错误!")
    #     return cq_num
    #
    # def machine_cq(self):
    #     cq_num=str(random.randint(1,3))#需要转换一下格式
    #     cq_info={"1":"剪刀","2":"石头","3":"布"}
    #     if cq_num in cq_info.keys():
    #         print("电脑出的是%s"%cq_info[cq_num])
    #     else:
    #         print("出拳错误!")
    #     return cq_num#字符串

    def human_vs_machine(self):
        #人机对战
        role=self.choose_role()#角色
        #变量:
        human_win=0
        ping=0
        machine_win=0
        while True:
            #方法一:
            # human_cq=int(self.role_cq(role))#角色出拳
            # machine_cq=int(self.machine_cq())#机器出拳
            #方法二
            human_cq=int(self.cq(role,1))
            machine_cq=int(self.cq("电脑",2))

            if human_cq!=machine_cq:#数值比较 1 石头  2 剪刀  3布
                if human_cq==1 and machine_cq==2:
                    human_win+=1
                elif human_cq==2 and machine_cq==3:
                    human_win+=1
                elif human_cq==3 and machine_cq==1:
                    human_win+=1
                else:
                    machine_win+=1
            else:
                ping+=1
            choice=input("是否要继续猜拳?按y继续,按n退出")
            if choice=='y':
                continue
            else:
                break
        print("对战结束:人赢了%s局,电脑赢了%s局,平了%s局"%(human_win,machine_win,ping))

HumanVSMachine().human_vs_machine()

我的解法:

class Game:
    def __init__(self,name):
        self.name = name


    def get_name(self):
        name_dict = {1: '曹操', 2: '张飞', 3: '刘备'}
        real_name = name_dict[self.name]
        return real_name

    def get_player_guess(self):
        a = int(input('请输入1-3任一数字:'))
        return a

    def get_pc_random(self):
        b = random.randint(1, 3)
        return b

    def game_result(self):
        game_dict = {1: '剪刀', 2: '石头', 3: '布'}
        count_1 = 0
        count_2 = 0
        count_3 = 0

        while True:
            player_guess = self.get_player_guess()
            pc_guess = self.get_pc_random()

            print(self.get_name() + '出拳:', game_dict[player_guess])
            print('电脑出拳:', game_dict[pc_guess])

            if player_guess == pc_guess:
                count_1 = count_1 + 1
                print('平局')
            elif (player_guess == 1 and pc_guess == 3) \
                    or (player_guess == 2 and pc_guess == 1) \
                    or (player_guess == 3 and pc_guess == 1):
                count_2  = count_2 + 1
                print(self.get_name() + '胜')
            else:
                count_3 = count_3 + 1
                print('电脑胜')

            again = input('是否继续?y/n:')

            if again == 'n':
                print(self.get_name()+'赢:'+str(count_2)+'局')
                print('电脑赢:' + str(count_3) + '局')
                print('平局:' + str(count_1 ))
                print('游戏结束')
                break

game = Game()
game.role_vs_robot()

剪刀石头布猜拳游戏另外两种写法:
第一种:

cq_info={"1":"剪刀","2":"石头","3":"布"}
while True:
    a = int(input('输入1-3(1剪刀 2石头 3布):'))
    b = random.randint(1,3)
    print('玩家出拳:',cq_info[str(a)])
    print('电脑出拳:', cq_info[str(b)])

    if a > b and (b-1)>0 or a<b and b>(a+1):
        print('玩家胜')
    elif a == b:
        print('平局')
    else:
        print('电脑胜')

    msg = input('y/n')
    if msg == 'n':

        break

第二种:

cq_info={"1":"剪刀","2":"石头","3":"布"}
win_list = [[2, 1], [1, 3], [3, 2]]  # 定义胜利组合
while True:
    a = int(input('输入1-3(1剪刀 2石头 3布):'))
    b = random.randint(1,3)
    print('玩家出拳:',cq_info[str(a)])
    print('电脑出拳:', cq_info[str(b)])

    if [a,b] in win_list:
        print('玩家胜')
    elif a == b:
        print('平局')
    else:
        print('电脑胜')

    msg = input('y/n')
    if msg == 'n':

        break

第三种:网上看到的

cq_info = {"1": "剪刀", "2": "石头", "3": "布"}
while True:
    a = int(input('输入1-3(1剪刀 2石头 3布):'))
    b = random.randint(1, 3)
    print('玩家出拳:', cq_info[str(a)])
    print('电脑出拳:', cq_info[str(b)])

    if a - b == 1 or a - b == -2:
        print('玩家胜')
    elif a == b:
        print('平局')
    else:
        print('电脑胜')

    msg = input('y/n')
    if msg == 'n':
        break

ps:欢迎指正~有好的见解,期待你与我分享

 

 

 

 

 

 

 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!