exercise014_猜拳遊戲
阿新 • • 發佈:2019-01-04
# -*- 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:退出