复制代码 代码如下:
-- coding: utf-8 --
import random
数字类
class NumberItem:
数字个数
_GUESS_NUMBERCOUNT = 4
def init(self):
self.num = []
长度是否标准
def IsFormat(self):
return self.num.len() == self._GUESS_NUMBERCOUNT
生成随机数
def RestRandomNumber(self):
allNum = range(10)
for index in range(self._GUESS_NUMBERCOUNT):
self.num.append(allNum.pop(random.randrange(10-index)))
校验输入数
def CheckNumber(self, input_number):
result = NumberCheckResult()
for index in range(self._GUESS_NUMBERCOUNT):
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._guessA = 0
self._guessB = 0
def GetCheckResult(self):
return '%d A %d B' % (self._guessA, self._guessB)
def Add_A(self):
self._guessA += 1
def Add_B(self):
self._guessB += 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_GUESSTIMES = 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_GUESSTIMES)
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_NUMBERCOUNT-1) + ']已经生成,你有%d次机会!' % self._MAX_GUESSTIMES
猜中标志
guess_result = False
quit_flag = False
while (self.GetGuessTimes() < self._MAX_GUESSTIMES
and guess_result != True and quit_flag != True):
print '你还剩%d次机会,输入你猜的数字:' % (self._MAX_GUESSTIMES- 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._guessA == NumberItem._GUESS_NUMBERCOUNT):
guess_result = True
print '恭喜,你猜中了数字' , self.guessNum.GetNumber() , ' 用了%d次机会' %
self.GetGuessTimes()
elif(self.GetGuessTimes() == self._MAX_GUESSTIMES ):
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()