python版的文曲星猜数字游戏代码

代码如下:

# -*- 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()

发表评论