猜拳游戏。。。。。。

人和机器猜拳
---------------人机猜拳-----------------
请选择角色 1. 曹操 2. 张飞  3  刘备
请出拳:1 剪刀 2. 石头 3 布 (随便输入一个字符,随机产生一个1-3的数字)
电脑出拳:(随机产生一个1-3的数字),提示电脑出拳XXX
本局对战结果XXX赢  xxx输
是否继续 y/n
请出拳:1 剪刀 2. 石头 3 布 (随便输入一个字符,随机产生一个1-3的数字)
电脑出拳:(随机产生一个1-3的数字),提示电脑出拳XXX
本局对战结果XXX赢  xxx输
是否继续 y/n
----------xxx vs 电脑--------
xxx赢几局
电脑赢几局


  1. class Person:
  2. def __init__(self, name=None, score=0):
  3. self.name = name
  4. self.score = score
  5. def ChoiceName(self):
  6. while True:
  7. print( "请选择")
  8. choice = input( "1.张飞2.吕布3.刘备")
  9. if choice.isdigit():
  10. choice = int(choice)
  11. if choice == 1:
  12. self.name = "张飞"
  13. break
  14. elif choice == 2:
  15. self.name = "吕布"
  16. break
  17. elif choice == 3:
  18. self.name = "刘备"
  19. break
  20. else:
  21. print( "输入有误请重新输入")
  22. else:
  23. print( "只能输入数字")
  24. def chuquan(self):
  25. tga = None
  26. while True:
  27. print( "请出拳")
  28. tga = input( "1.石头 2.剪刀 3.布")
  29. if tga.isdigit():
  30. tga = int(tga)
  31. if tga == 1:
  32. print( "%s出的石头" % self.name)
  33. break
  34. elif tga == 2:
  35. print( "%s出的是剪刀" % self.name)
  36. break
  37. elif tga == 3:
  38. print( "%s出的是布" % self.name)
  39. break
  40. else:
  41. print( "输入有误请重新输入")
  42. else:
  43. print( "输入有误请输入数字")
  44. return tga
  45. import random
  46. class Computer:
  47. name = "电脑"
  48. score = 0 # 电脑得分
  49. def chuquan(self):
  50. num = random.randint( 1, 3)
  51. if num == 1:
  52. print( "电脑出的是石头")
  53. elif num == 2:
  54. print( "电脑出的是剪刀")
  55. elif num == 3:
  56. print( "电脑出的是布")
  57. return num
  58. class Game:
  59. count = 0 # 平局显示的起始分数
  60. person = Person()
  61. computer = Computer()
  62. def Star_Game(self):
  63. print( "-------人机猜拳-------")
  64. self.person.ChoiceName()
  65. while True:
  66. presult = self.person.chuquan()
  67. cresult = self.computer.chuquan()
  68. self.getresult(presult, cresult)
  69. a = input( "输入任意数继续或输入n结束")
  70. if a.lower() == "n":
  71. break
  72. def getresult(self, presult, cresult):
  73. if (presult == 1 and cresult == 2) or (presult == 2 and cresult == 3) or (presult == 3 and cresult == 1):
  74. print( "本局%s赢了" % self.person.name)
  75. self.person.score += 1
  76. elif presult == cresult:
  77. print( "平局")
  78. self.count += 1
  79. else:
  80. print( "本局电脑赢了")
  81. self.computer.score += 1
  82. def vs(self):
  83. print( "-----%s vs 电脑-------" % self.person.name)
  84. print( "%s赢了%s局" % (self.person.name, self.person.score))
  85. print( "电脑赢了%s局" % self.computer.score)
  86. print( "平局%s局" % self.count)
  87. if self.person.score > self.computer.score:
  88. print( "%s最终胜利" % self.person.name)
  89. elif self.person.score < self.computer.score:
  90. print( "电脑最终获得胜利")
  91. else:
  92. print( "最终获得平局")
  93. game = Game()
  94. game.Star_Game()
  95. game.vs()
  1. class Person:
  2. def __init__(self, name=None, score=0):
  3. self.name = name
  4. self.score = score
  5. def ChoiceName(self):
  6. while True:
  7. print( "请选择")
  8. choice = input( "1.张飞2.吕布3.刘备")
  9. if choice.isdigit():
  10. choice = int(choice)
  11. if choice == 1:
  12. self.name = "张飞"
  13. break
  14. elif choice == 2:
  15. self.name = "吕布"
  16. break
  17. elif choice == 3:
  18. self.name = "刘备"
  19. break
  20. else:
  21. print( "输入有误请重新输入")
  22. else:
  23. print( "只能输入数字")
  24. def chuquan(self):
  25. tga = None
  26. while True:
  27. print( "请出拳")
  28. tga = input( "1.石头 2.剪刀 3.布")
  29. if tga.isdigit():
  30. tga = int(tga)
  31. if tga == 1:
  32. print( "%s出的石头" % self.name)
  33. break
  34. elif tga == 2:
  35. print( "%s出的是剪刀" % self.name)
  36. break
  37. elif tga == 3:
  38. print( "%s出的是布" % self.name)
  39. break
  40. else:
  41. print( "输入有误请重新输入")
  42. else:
  43. print( "输入有误请输入数字")
  44. return tga
  45. import random
  46. class Computer:
  47. name = "电脑"
  48. score = 0 # 电脑得分
  49. def chuquan(self):
  50. num = random.randint( 1, 3)
  51. if num == 1:
  52. print( "电脑出的是石头")
  53. elif num == 2:
  54. print( "电脑出的是剪刀")
  55. elif num == 3:
  56. print( "电脑出的是布")
  57. return num
  58. class Game:
  59. count = 0 # 平局显示的起始分数
  60. person = Person()
  61. computer = Computer()
  62. def Star_Game(self):
  63. print( "-------人机猜拳-------")
  64. self.person.ChoiceName()
  65. while True:
  66. presult = self.person.chuquan()
  67. cresult = self.computer.chuquan()
  68. self.getresult(presult, cresult)
  69. a = input( "输入任意数继续或输入n结束")
  70. if a.lower() == "n":
  71. break
  72. def getresult(self, presult, cresult):
  73. if (presult == 1 and cresult == 2) or (presult == 2 and cresult == 3) or (presult == 3 and cresult == 1):
  74. print( "本局%s赢了" % self.person.name)
  75. self.person.score += 1
  76. elif presult == cresult:
  77. print( "平局")
  78. self.count += 1
  79. else:
  80. print( "本局电脑赢了")
  81. self.computer.score += 1
  82. def vs(self):
  83. print( "-----%s vs 电脑-------" % self.person.name)
  84. print( "%s赢了%s局" % (self.person.name, self.person.score))
  85. print( "电脑赢了%s局" % self.computer.score)
  86. print( "平局%s局" % self.count)
  87. if self.person.score > self.computer.score:
  88. print( "%s最终胜利" % self.person.name)
  89. elif self.person.score < self.computer.score:
  90. print( "电脑最终获得胜利")
  91. else:
  92. print( "最终获得平局")
  93. game = Game()
  94. game.Star_Game()
  95. game.vs()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值