python第四章答案猜数游戏_尹科峰的博客:文曲星上的“猜数字”游戏Python版 - 哲思...

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,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,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!"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值