Copy codeThe Code is as follows:
#-*-Coding: UTF-8 -*-
Import random
# Number
Class NumberItem:
# Number of digits
_ GUESS_NUMBER_COUNT _ = 4
Def _ init _ (self ):
Self. _ num _ = []
# Whether the length is standard
Def IsFormat (self ):
Return self. _ num _. _ len _ () = self. _ GUESS_NUMBER_COUNT _
# Generating Random Numbers
Def RestRandomNumber (self ):
AllNum = range (10)
For index in range (self. _ GUESS_NUMBER_COUNT _):
Self. _ num _. append (allNum. pop (random. randrange (10-index )))
# Check input count
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
# Return string
Def GetNumber (self ):
Return self. _ num _
# Quiz result 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
# History items
Class GuessHisItem:
Def _ init _ (self ):
Self. _ guessNum _ = NumberItem ()
Self. _ guessResult _ = NumberCheckResult ()
Def ShowItem (self ):
Print self. _ guessNum _. GetNumber (), '-', self. _ guessResult _. GetCheckResult ()
# Games
Class GuessNumberGame ():
# Number of opportunities
_ MAX_GUESS_TIMES _ = 8
Def _ init _ (self ):
Self. ResetGameDate ()
# Resetting game data
Def ResetGameDate (self ):
Self. _ guessNum _ = NumberItem ()
Self. _ guessHis _ = []
Def GetGuessTimes (self ):
Return self. _ guessHis _. _ len __()
# Format conversion of input strings
Def ChangeInputNumberFormat (self, numberStr ):
ParseNum = NumberItem ()
If (numberStr. isdigit ()):
For eachNum in numberStr:
ParseNum. _ num _. append (int (eachNum ))
Return parseNum
# Print the quiz history
Def ShowGuessHis (self ):
Print''
Print 'you have guessed % d times, Total % d chances' % (self. GetGuessTimes (), self. _ MAX_GUESS_TIMES _)
Print''
For eachItem in self. _ guessHis _:
EachItem. ShowItem ()
Print''
Def ShowHelp (self ):
Print''
Print 'input \ 'help \ 'to display the command list .'
Print 'input \ 'his \ 'display the quiz history'
Print 'input \ 'Cheat \ 'display cheating result'
Print 'input \ 'Quit \ 'end Game'
Print''
Def ShowCheat (self ):
Print''
Print 'the number to guess is ', self. _ guessNum _. GetNumber ()
Print''
Def StartOneGame (self ):
Self. ResetGameDate ()
Print 'start guessing digital games! '
Self. _ guessNum _. RestRandomNumber ()
Print 'random number [* '+', * '* (self. _ guessNum _. _ GUESS_NUMBER_COUNT_-1) +'] has been generated, you have % d chance! '% Self. _ MAX_GUESS_TIMES _
# Quiz mark
Guess_result = False
Quit_flag = False
While (self. GetGuessTimes () <self. _ MAX_GUESS_TIMES _
And guess_result! = True and quit_flag! = True ):
Print 'You have % d chances left. Enter the number you guessed:' % (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:
# Conversion input and Verification
HisItem = GuessHisItem ()
HisItem. _ guessNum _ = self. ChangeInputNumberFormat (input_str)
If (hisItem. _ guessNum _. IsFormat ()! = True ):
Print 'digit format error. Enter it again! '
Else:
# Quiz history
HisItem. _ guessResult _ = self. _ guessNum _. CheckNumber (hisItem. _ guessNum _)
Self. _ guessHis _. append (hisItem)
HisItem. ShowItem ()
# Guessing
If (hisItem. _ guessResult _. _ guess_A _ = NumberItem. _ GUESS_NUMBER_COUNT _):
Guess_result = True
Print 'Congratulations, you guessed the number', self. _ guessNum _. GetNumber (), '% d chances used' % self. GetGuessTimes ()
Elif (self. GetGuessTimes () = self. _ MAX_GUESS_TIMES _):
Print''
Print 'challenge failed. You have guessed % d times, '% self. GetGuessTimes (),' the correct number is ', 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 'another game? (Y/N )'
Input_str = raw_input ();
If (input_str! = 'Y '):
Quit_flag = True
Print 'Goodbye! '
###### MAIN #######
If _ name _ = '_ main __':
Game = GuessNumberGame ()
Game. StartGame ()