通过面向对象来完成猜拳游戏案例

猜拳游戏:
人和机器猜拳
---------------人机猜拳-----------------
请选择角色 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赢几局
电脑赢几局
class Person:
    def __init__(self, name=None, score=0):
        self.name = name
        self.score = score

    def ChoiceName(self):
        while True:
            print("请选择")
            choice = input("1.张飞2.吕布3.刘备")
            if choice.isdigit():
                choice = int(choice)
                if choice == 1:
                    self.name = "张飞"
                    break
                elif choice == 2:
                    self.name = "吕布"
                    break
                elif choice == 3:
                    self.name = "刘备"
                    break
                else:
                    print("输入有误请重新输入")

            else:
                print("只能输入数字")

    def chuquan(self):
        tga = None
        while True:
            print("请出拳")
            tga = input("1.石头 2.剪刀 3.布")
            if tga.isdigit():
                tga = int(tga)
                if tga == 1:
                    print("%s出的石头" % self.name)
                    break
                elif tga == 2:
                    print("%s出的是剪刀" % self.name)
                    break
                elif tga == 3:
                    print("%s出的是布" % self.name)
                    break
                else:
                    print("输入有误请重新输入")
            else:
                print("输入有误请输入数字")
        return tga


import random


class Computer:
    name = "电脑"
    score = 0  # 电脑得分

    def chuquan(self):
        num = random.randint(1, 3)
        if num == 1:
            print("电脑出的是石头")
        elif num == 2:
            print("电脑出的是剪刀")
        elif num == 3:
            print("电脑出的是布")
        return num


class Game:
    count = 0  # 平局显示的起始分数
    person = Person()
    computer = Computer()

    def Star_Game(self):
        print("-------人机猜拳-------")
        self.person.ChoiceName()
        while True:
            presult = self.person.chuquan()
            cresult = self.computer.chuquan()
            self.getresult(presult, cresult)
            a = input("输入任意数继续或输入n结束")
            if a.lower() == "n":
                break

    def getresult(self, presult, cresult):
        if (presult == 1 and cresult == 2) or (presult == 2 and cresult == 3) or (presult == 3 and cresult == 1):
            print("本局%s赢了" % self.person.name)
            self.person.score += 1
        elif presult == cresult:
            print("平局")
            self.count += 1
        else:
            print("本局电脑赢了")
            self.computer.score += 1

    def vs(self):
        print("-----%s vs 电脑-------" % self.person.name)
        print("%s赢了%s局" % (self.person.name, self.person.score))
        print("电脑赢了%s局" % self.computer.score)
        print("平局%s局" % self.count)
        if self.person.score > self.computer.score:
            print("%s最终胜利" % self.person.name)
        elif self.person.score < self.computer.score:
            print("电脑最终获得胜利")
        else:
            print("最终获得平局")


game = Game()
game.Star_Game()
game.vs()


猜你喜欢

转载自blog.csdn.net/mr_li1/article/details/80755570