石头剪刀布java面相对象_面向对象实现石头剪刀布游戏

本文介绍了一个使用Python编写的石头剪刀布游戏。玩家可以选择喜洋洋、美洋洋或懒洋洋作为角色,与电脑角色慢羊羊进行对决。游戏通过随机数决定电脑出拳,根据石头、剪刀、布的规则判断胜负,并记录分数。玩家可以在三局结束后查看总分,判断胜负。
摘要由CSDN通过智能技术生成

1 importrandom2 importtime3

4

5 classPlayer:6 def __init__(self):7 self.name = "老张"

8 self.fist = "你这样会报错"

9 self.score =010

11 defplayer_name(self):12 name_choice = ['喜洋洋', '美洋洋', '懒洋洋']13 input_name = input("请选择人物:1喜洋洋 2美洋洋 3懒洋洋(输入exit退出):")14 if input_name == "exit":15 self.name = "exit"

16 else:17

18 if input_name == "1":19 self.name =name_choice[0]20 elif input_name == "2":21 self.name = name_choice[1]22 elif input_name == "3":23 self.name = name_choice[2]24 #self.player_fist()

25 print("你选择了{}".format(self.name))26

27 defplayer_fist(self):28 li_fist_p = ["石头", "剪刀", "布"]29 input_fist = input("请出拳:1石头, 2剪刀, 3布(输入exit退出):\n")30 if input_fist == "exit":31 self.fist = "exit"

32 else:33 if input_fist == "1":34 self.fist =li_fist_p[0]35 if input_fist == "2":36 self.fist = li_fist_p[1]37 if input_fist == "3":38 self.fist = li_fist_p[2]39 print("你使出九牛二虎之力出了---{}".format(self.fist))40

41

42 classComputer:43 def __init__(self):44 self.c_name = "慢羊羊"

45 self.c_fist = "未出拳"

46 self.c_score =047

48 defcomputer_name(self):49 c_name_choice = ["慢羊羊", "沸羊羊", "暖羊羊"]50 self.c_name =random.choice(c_name_choice)51 print("敌方派出了超级厉害的---➤{}".format(self.c_name))52

53 defcomputer_fist(self):54 li_fist_c = ["石头", "剪刀", "布"]55 self.c_fist =random.choice(li_fist_c)56 print("{}在千钧一发之际使出了必杀---➤{}".format(self.c_name, self.c_fist))57

58

59 classBegin:60 n = 1

61

62 def __init__(self, pl, com):63 self.player =pl64 self.computer =com65 self.exit =None66

67 defplay_game(self):68 self.player.player_name()69 print("正在出招......\n")70 time.sleep(1)71 print("敌方正在派人与你对战......\n")72 time.sleep(1)73 self.computer.computer_name()74 print("*"*50)75

76 while self.__class__.n <= 3:77 print("----------------第{}回合---------------".format(self.__class__.n))78 if self.player.name == "exit":79 break

80 time.sleep(1)81 self.player.player_fist()82 if self.player.fist == "exit":83 break

84 print("{}夜观天象,掐指一算......\n".format(self.computer.c_name))85 time.sleep(1)86 self.computer.computer_fist()87 self.compare()88 print("*" * 50)89 self.__class__.n += 1

90 time.sleep(1)91 print("正在统计战绩———————➤\n")92 print("三局定胜负, 游戏结束\n")93 print("战绩-------➤\n")94 time.sleep(1)95 print("-------3")96 time.sleep(1)97 print("-------2")98 time.sleep(1)99 print("-------1")100 time.sleep(1)101 print("-------➤\n")102 print("你的总分为------➤{}\n\n\n{}的总分为---➤{}\n".format(self.player.score, self.computer.c_name,103 self.computer.c_score))104 if self.player.score >self.computer.c_score:105 print("-----你是神,你赢了!-----")106 elif self.player.score <107 print elif self.player.score="=self.computer.c_score:109">

111 defscore(self):112 print("你当前分数为:{} {}分数为:{}".format(self.player.score, self.computer.c_name, self.computer.c_score))113

114 defcompare(self):115 if self.player.fist == "石头" and self.computer.c_fist == "剪刀":116 print("你真厉害----你赢了\n")117 self.player.score += 1

118 self.score()119

120 elif self.player.fist == "石头" and self.computer.c_fist == "布":121 print("命途多舛的人生----你输了\n")122 self.computer.c_score += 1

123 self.score()124

125 elif self.player.fist == "石头" and self.computer.c_fist == "石头":126 print("激烈的角逐后----打成平局\n")127 self.score()128

129 elif self.player.fist == "剪刀" and self.computer.c_fist == "石头":130 print("牧童骑黄牛,歌声振林樾----你输了\n")131 self.computer.c_score += 1

132 self.score()133

134 elif self.player.fist == "剪刀" and self.computer.c_fist == "剪刀":135 print("平平淡淡才是真----平局\n")136 self.score()137

138 elif self.player.fist == "剪刀" and self.computer.c_fist == "布":139 print("我好厉害----你赢了\n")140 self.player.score += 1

141 self.score()142

143 elif self.player.fist == "布" and self.computer.c_fist == "剪刀":144 print("曾经的月亮很圆,你却没有发现----你输了\n")145 self.computer.c_score += 1

146 self.score()147

148 elif self.player.fist == "布" and self.computer.c_fist == "石头":149 print("♫♫♫♫♫♫♫♫♫♫----你赢了")150 self.player.score += 1

151 self.score()152

153 elif self.player.fist == "布" and self.computer.c_fist == "布":154 print("回家----平局")155 self.score()156

157

158 player =Player()159 computer =Computer()160 begin =Begin(player, computer)161 begin.play_game()

107>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值