人和機器猜拳遊戲寫成一個類,有如下幾個函數:
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:歡迎指正~有好的見解,期待你與我分享