exercise014_猜拳游戏

# -*- coding: utf-8 -*- 
# @Time : 14/8/18 下午2:34 
# @Author : debin.lin
# @File : exercise014.py 
# @Software: PyCharm
# @Mail : [email protected] 

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

# import random
# class Game():
#     def __init__(self): #将胜负数作为类属性,方便在下面的方法中计算次数
#         self.win_num = 0
#         self.lose_num = 0
#         self.draw_num = 0
#     '''选择角色,并打印'''
#     def choose_player(self):
#         try:
#             self.role = int(input('there is threee players to choose: 曹操(1)、张飞(2)、刘备(3) '))
#         except ValueError:
#             print('please input a digit!')
#         else:
#             if self.role == 1:
#                 print('your role is: 曹操')
#             elif self.role == 2:
#                 print('your role is: 张飞')
#             elif self.role == 3:
#                 print('your role is: 刘备')
#             else:
#                 print('there is no player for you, try again!')
#
#     '''用户开始猜拳,并打印'''
#     def player_guessing(self):
#         try:
#             self.plyer_choice = int(input('which one will you guess: 剪刀(1)、石头(2)、布(3)'))
#         except ValueError:
#             print('please input a digit!')
#         else:
#             if self.plyer_choice == 1:
#                 print('your choice is: 剪刀')
#             elif self.plyer_choice == 2:
#                 print('your choice is: 石头')
#             elif self.plyer_choice == 3:
#                 print('your choice is: 布')
#             else:
#                 print('try again!')
#
#     '''随机产生1个1-3的数字,提示电脑出拳结果'''
#     def computer_guessing(self):
#         self.computer_choice = random.randint(1, 3)
#         if self.computer_choice==1:
#             print("computer's choice is: 剪刀")
#         elif self.computer_choice==2:
#             print("computer's choice is: 石头")
#         elif self.computer_choice==3:
#             print("computer's choice is: 布")
#
#     '''人机对战,判断胜平负'''
#     def pk(self):
#         if (self.plyer_choice==1 and self.computer_choice==3)or(self.plyer_choice==2 and self.computer_choice==1)or(self.plyer_choice==3 and self.computer_choice==2):
#             print('you win!')
#             self.win_num+=1 #以上几种胜利情况出现时,将表示胜利次数变量加1
#         elif self.plyer_choice==self.computer_choice:
#             print('draw')
#             self.draw_num+=1 # 平局时+1
#         elif self.plyer_choice>3:
#             print('input error! invaild!')
#         else:
#             print('you lose')
#             self.lose_num+=1 # 败北时记录+1
#
# guessing_gama=Game() #创建猜拳游戏的实例
#
# guessing_gama.choose_player()
# while(1):
#     guessing_gama.player_guessing()
#     guessing_gama.computer_guessing()
#     guessing_gama.pk()
#     a = input('continue? enter anything to contiune, enter "N" to quit')
#     if a.lower() == 'n':
#         print('game over!')
#         break
#     else:
#         continue
# print('The game result:')
# print('win: ',guessing_gama.win_num)
# print('lose: ',guessing_gama.lose_num)
# print('draw: ',guessing_gama.draw_num)

from random import randint

class Morra():
    def __init__(self,):
        pass

    def computer_player(self):
        while True:
            role_num = input ( 'Please enter a number to select the role.(1:曹操,2:张飞,3:刘备)' )
            if role_num == '1':
                print ( "曹操:曹某在此,谁敢在此胡闹!" + "\n曹操:少侠,出招吧!")
                return "曹操"
                break
            elif role_num == '2':
                print ( "张飞:贼子!交出你的首级!" + "\n张飞:小子,让我看看你的本事!")
                return "张飞"
                break
            elif role_num == '3':
                print("刘备:阿斗!爹来了。" + "\n刘备:英雄,手下留情!")
                return "刘备"
                break
            else:
                print("The input is wrong, please reenter it")
            continue

    def play_user(self):
        while True:
            user_num = input('Please enter a number:(1:石头,2:剪刀,3:布)')
            if user_num == '1':
                print("你:石头")
                return {'石头': 0}
                break
            elif user_num == '2':
                print("你:剪刀")
                return {'剪刀': 1}
                break
            elif user_num == '3':
                print("你:布")
                return {'布': 2}
                break
            else:
                print("The input is wrong, please reenter it")
            continue

    def play_computer(self):
        computer_num = randint(1, 3)
        if computer_num == 1:
            return {'石头': 0}
        elif computer_num == 2:
            return {'剪刀': 1}
        else:
            return {'布': '2'}

    def play_game(self, user, computer):
        if user == int(computer):
            print("平局!")
            return "平局"
        elif (int(user) - int(computer) == -1) or (int(user) - int(computer) == 2):
            print("你赢了!")
            return "胜"
        else:
            print("你输了!")
            return '负'

p1 = Morra()
computer_name = p1.computer_player()
message = ''
user_score = {'胜': 0, '负': 0, '平': 0}
computer_score = {'胜': 0, '负': 0, '平': 0}
while message != 'n':
    p1_player = list(p1.play_user().values())[0]
    computer_player = p1.play_computer()
    print(computer_name + ':' + list(computer_player.keys())[0])
    result = p1.play_game(p1_player, list(computer_player.values())[0])
    print(result)
    if result == '平局':
        user_score['平'] += 1
        computer_score['平'] += 1
    elif result == '胜':
        user_score['胜'] += 1
        computer_score['负'] += 1
    elif result == '负':
        user_score['负'] += 1
        computer_score['胜'] += 1
    message = input("是否继续?" + "\ny:继续,n:退出")

print("你的成绩: " + str(user_score) + '\n电脑的成绩:' + str(computer_score))
Please enter a number to select the role.(1:曹操,2:张飞,3:刘备)1
曹操:曹某在此,谁敢在此胡闹!
曹操:少侠,出招吧!
Please enter a number:(1:石头,2:剪刀,3:布)1
你:石头
曹操:石头
平局!
平局
是否继续?
y:继续,n:退出y
Please enter a number:(1:石头,2:剪刀,3:布)1
你:石头
曹操:石头
平局!
平局
是否继续?
y:继续,n:退出y
Please enter a number:(1:石头,2:剪刀,3:布)1
你:石头
曹操:剪刀
你赢了!
胜
是否继续?
y:继续,n:退出

Please enter a number:(1:石头,2:剪刀,3:布)The input is wrong, please reenter it
Please enter a number:(1:石头,2:剪刀,3:布)2
你:剪刀
曹操:石头
你输了!
负
是否继续?
y:继续,n:退出

猜你喜欢

转载自blog.csdn.net/weixin_42652708/article/details/81666881