Python version of the "Guess number" Game On wenquxing

Source: Internet
Author: User

The classic "guess number" Game On wenquxing, with how many boring times I spent, is still surging in retrospect today. The game was played on winodows mobile a few days ago. Today, I caught up with the free time and wrote it as Python. Haha, python is really python, only about 1/3 of the formerCodeThat's it.
Let's talk about the game rules. It's very simple:ProgramA random four-digit number (for example, 1234) is generated, which requires the player to guess the answer within eight chances. Each quiz, the program will prompt the player in the form of "xaxb", where "A" indicates that the number and position are correct, and "B" indicates that the position is incorrect but the number is correct, "X" indicates the corresponding number. If a player guesses "5678", the prompt is "0a0b". If "4321" Is guessed, the prompt is "0a4b". When "4a0b" is used, that is, all the numbers at the four locations are correct, it is a victory!
How is it? Do you dare to challenge your IQ? The following is the source code: (welcome to help improve, it is best to change it to pys60)

Import random

Class Bingle:
"""
Generate a random answer, and record Guess times, and judge it
"""
A, B = (0, 0) # match falg
Attempttimes = 8 # geuss times
Answer = [0, 0, 0] # the answer
Def buildanswer (Self ):
Random. Seed ()
While 1:
Digit = random. randint (0, 9999)
Self. Answer [0] = digit/1000
Self. Answer [1] = digit % 1000/100
Self. Answer [2] = digit % 100/10
Self. Answer [3] = digit % 10
If self. Answer [0]! = Self. Answer [1] and self. Answer [0]! = Self. Answer [2] and self. Answer [0]! = Self. Answer [3] and self. Answer [1]! = Self. Answer [2] and self. Answer [1]! = Self. Answer [3] and self. Answer [2]! = Self. Answer [3]:
Return
Def _ init _ (Self ):
Answer = [0, 0, 0]
Attempttimes = 0
Self. buildanswer ()
Def istrystringok (self, trystring ):
If trystring. isdigit () and Len (trystring) = 4:
If trystring [0]! = Trystring [1] and trystring [0]! = Trystring [2] and trystring [0]! = Trystring [3] and trystring [1]! = Trystring [2] and trystring [1]! = Trystring [3] and trystring [2]! = Trystring [3]:
Return 1
Return 0

Def judge (self, trystring ):
For I in range (4 ):
If (trystring [I] = STR (self. Answer [I]):
Self. A = self. A + 1
Else:
For J in range (4 ):
If (trystring [I] = STR (self. Answer [J]):
Self. B = self. B + 1
Returnstr = "% da % db" % (self. A, self. B)
Self. A = 0
Self. B = 0
Return returnstr
Class maingame:
"""
Play the game
"""
B = none
Def _ init _ (Self ):
Self. B = Bingle ()
# Print "Answer: % s" % (self. B. Answer)
Print "I 've ready, Please guess me ."

Def play (Self ):
While self. B. attempttimes:
Trystring = raw_input ("% d: \ t" % self. B. attempttimes)
If self. B. istrystringok (trystring ):
Tryresult = self. B. Judge (trystring)
If tryresult = "4a0b ":
Print "you are winer! "
Break
Else:
Print tryresult
Self. B. attempttimes = self. B. AttemptTimes-1
Else:
Print "input error! Type again ,",
Continue
Else:
Print "attempt times is eight. \ nthe finily answer is: % s \ Ngame over! "% Self. B. Answer
If _ name __= = "_ main __":
Try = 1
While try:
Binglegame = maingame ()
Binglegame. Play ()
Data = raw_input ("do you want to try again? \ Ntype [0] for 'no' and others for [Yes] \ n ")
If data. isdigit ():
Try = int (data)
Else:
Try = 1
Else:
Print "game exited! "

 

<The end>

Related Article

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.