Python version of the song Star Guess number game code

Source: Internet
Author: User
Copy CodeThe code is as follows:


#-*-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. 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 ' You have guessed%d times, total%d chance '% (self. Getguesstimes (), Self._max_guess_times_)
print "

for Eachitem in Self._guesshis_:
Eachitem.showitem ()
print '

def ShowHelp (self):
print '
print ' input \ ' help\ ' displays a list of commands. '
print ' input \ ' his\ ' show Quiz history '
print ' input \ ' cheat\ ' show cheat result '
print ' input \ ' quit\ ' end Game '
print '

def SHOWC Heat (self):
print '
print ' to guess the number is ', self._guessnum_. GetNumber ()
print '


def startonegame (self):

Self. Resetgamedate ()

print ' Start guessing the numbers game! '

Self._guessnum_. Restrandomnumber ()

print ' Random number [* ' + ', * ' * (self._guessnum_._guess_number_count_-1) + '] has been generated and you have%d chances! '% 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 ' 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:
#转换输入并校验
Hisitem = Guesshisitem ()
hisitem._guessnum_ = self. Changeinputnumberformat (INPUT_STR)
if (hisitem._guessnum_. Isformat ()! = True):
print ' number format is incorrect, re-enter! '
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 ' Congratulations, you guessed the number ', Self._guessnum_. GetNumber (), ' used%d chance '% 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 ' Play one more game? (y/n) '
Input_str = Raw_input ();

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

print ' Good-bye! '


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

if __name__ = = ' __main__ ':

Game = Guessnumbergame ()
Game. Startgame ()

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.