复制代码 代码如下:
# -*- coding: utf-8 -*-

import random

#数字类
class NumberItem:

    #数字个数
    _GUESS_NUMBER_COUNT_ = 4

    def __init__(self):
        self._num_ = []

    #长度是否标准   
    def IsFormat(self):
        return self._num_.__len__() == self._GUESS_NUMBER_COUNT_

    #生成随机数   
    def RestRandomNumber(self):
        allNum = range(10)
        for index in range(self._GUESS_NUMBER_COUNT_):
            self._num_.append(allNum.pop(random.randrange(10-index)))

    #校验输入数       
    def CheckNumber(self, input_number):
        result = NumberCheckResult()

        for index in range(self._GUESS_NUMBER_COUNT_):
            if (input_number._num_[index] == self._num_[index]):
                result.Add_A()
            elif(input_number._num_[index] in self._num_):
                result.Add_B()

        return result

    #返回字符串     
    def GetNumber(self):
        return self._num_

   
#竞猜结果 xAxB   
class NumberCheckResult:
    def __init__(self):
        self._guess_A_ = 0
        self._guess_B_ = 0

    def GetCheckResult(self):
        return '%d A %d B' % (self._guess_A_, self._guess_B_)

    def Add_A(self):
        self._guess_A_ += 1

    def Add_B(self):
        self._guess_B_ += 1

   
#竞猜历史项   
class GuessHisItem:
    def __init__(self):
        self._guessNum_ = NumberItem()
        self._guessResult_ = NumberCheckResult()

    def ShowItem(self):
        print self._guessNum_.GetNumber() , ' - ' , self._guessResult_.GetCheckResult()

#游戏类
class GuessNumberGame():

    #机会次数
    _MAX_GUESS_TIMES_ = 8

    def __init__(self):
        self.ResetGameDate()

    #重置游戏数据   
    def ResetGameDate(self):
        self._guessNum_ = NumberItem()
        self._guessHis_ = []

    def GetGuessTimes(self):
        return self._guessHis_.__len__()

    #输入字符串的格式转换
    def ChangeInputNumberFormat(self, numberStr):
        parseNum = NumberItem()
        if (numberStr.isdigit()):
            for eachNum in numberStr:
                parseNum._num_.append(int(eachNum))

        return parseNum

   
    #打印竞猜历史
    def ShowGuessHis(self):
        print ''
        print '你已经猜了 %d次, 总共%d次机会' % (self.GetGuessTimes(), self._MAX_GUESS_TIMES_)
        print ''

        for eachItem in self._guessHis_:
            eachItem.ShowItem()
        print ''

    def ShowHelp(self):
        print ''
        print '输入 \'help\' 显示命令列表.'
        print '输入 \'his\' 显示竞猜历史'
        print '输入 \'cheat\' 显示作弊结果'
        print '输入 \'quit\' 结束游戏'
        print ''

    def ShowCheat(self):
        print ''
        print '要猜的数字是 ', self._guessNum_.GetNumber()
        print ''

       
    def StartOneGame(self):

        self.ResetGameDate()

        print '开始猜数字游戏!'

        self._guessNum_.RestRandomNumber()

        print '随机号码 [*' + ',*'*(self._guessNum_._GUESS_NUMBER_COUNT_-1) + ']已经生成,你有%d次机会!' % self._MAX_GUESS_TIMES_

        #猜中标志
        guess_result = False
        quit_flag = False

        while (self.GetGuessTimes() < self._MAX_GUESS_TIMES_
               and guess_result != True and quit_flag != True):

            print '你还剩%d次机会,输入你猜的数字:' % (self._MAX_GUESS_TIMES_- self.GetGuessTimes())
            input_str = raw_input();

            if (input_str == 'help'):
                self.ShowHelp()
            elif(input_str == 'his'):
                self.ShowGuessHis()
            elif(input_str == 'cheat'):
                self.ShowCheat()
            elif(input_str == 'quit'):
                quit_flag = True
            else:
                #转换输入并校验
                hisItem = GuessHisItem()

                hisItem._guessNum_ = self.ChangeInputNumberFormat(input_str)
                if (hisItem._guessNum_.IsFormat() != True):
                    print '数字格式错误, 重新输入!'
                else:

                    #竞猜历史
                    hisItem._guessResult_ = self._guessNum_.CheckNumber(hisItem._guessNum_)
                    self._guessHis_.append(hisItem)

                    hisItem.ShowItem()

                    #猜中
                    if (hisItem._guessResult_._guess_A_ == NumberItem._GUESS_NUMBER_COUNT_):
                        guess_result = True
                        print '恭喜,你猜中了数字' , self._guessNum_.GetNumber() , ' 用了%d次机会' % self.GetGuessTimes()
                    elif(self.GetGuessTimes() == self._MAX_GUESS_TIMES_ ):
                        print ''
                        print '挑战失败,你已经猜了%d次,' % self.GetGuessTimes(), '正确的数字是', self._guessNum_.GetNumber()

        return quit_flag
                       

    def StartGame(self):
        quit_flag = False
        while(quit_flag != True):

            quit_flag = self.StartOneGame()

            if (quit_flag != True):
                print ''
                print '再玩一局?(Y/N)'
                input_str = raw_input();

                if (input_str != 'Y'):
                    quit_flag = True

        print '再见!'

       
###### MAIN #######

if __name__ == '__main__':

    game = GuessNumberGame()
    game.StartGame()       

标签:
文曲星,猜数字

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
评论“Python版的文曲星猜数字游戏代码”
暂无“Python版的文曲星猜数字游戏代码”评论...

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。