#!/usr/bin/python#-*-coding:utf-8-*- fromRandomImportShuffleclassCaigame:win=False Flag=False Life=12 What=-1S1="'Map1=[] Thenum=0defStart (self): t=range (1,10) Shuffle (t) self.thenum=t[0]*1000+t[1]*100+t[2]*10+t[3] Self.inmap ()PrintSelf.thenum whileSelf.life>0:self. GetResults ()ifSelf.win:Print "you won." elifSelf.flag:Print "you quit." Else:Print "you lost." return defGetResults (self): Self.life-=1S1=raw_input (" "Enter the number you guessed or enter Q or Q to exit or enter a or a to automatically guess" ") ifS1inch['Q','Q']: Self.life=0 Self.flag=Truereturn elifS1inch['A','a']: S1=Self.autodo ()Else: Passself.s1=S1 W=self.getnum (S1) self.what=WPrintS1,":", W[0],"a", W[1],"b" ifW[1]==4: Self.life=0 Self.win=1return return defGetnum (self,x,n1=-1): ifn1==-1:n1=Self.thenum N1=Str (n1) a=len (Filter (LambdaM,n=n1:minchn,str (x))) b=len (Filter (LambdaM,N=N1,K=STR (x): N.find (m) = =K.find (M), str (x))) return[A, b]defInmap (self): R={} t=range (1,10) forIinchT: forJinchT: forMinchT: forNinchT:ifI!=j!=m!=n!=i andJ!=n andm!=I:r[int (str (i)+str (j) +str (m) +str (n))]=1Self.map1=R.keys () self.map1.sort ( )return defAutodo (self):ifSelf.what!=-1: Self.map1=filter (LambdaM,n=self.what,k=self.getnum, ttt=self.s1:n==K (ttt,m), SELF.MAP1)#If Len (self.map1) <9:p rint self.map1 ifself.map1[0]!=SELF.S1:returnSelf.map1[0]Else: returnSelf.map1[-1]defz86 ():#Shuffle Licensingt=[] defg (x): Sh,yu=divmod (x,13) return[Sh,yu]defG2 (x, y): t=[] S1=["2","3","4","5","6","7","8","9","Ten","J","Q","K","A"] forIinchx:ifi[0]==Y:t+=[i[1]] T.sort () t=map (Lambdam,n=s1:n[m],t)returnT S2=[u"Black Peach", u"Red Peach", u"Square Slice", u"Grass Flower"] S3=[u"North", u"South", u"West", u"East"] t=range (52) Shuffle (t) t=map (g,t) T=t[:13],t[13:26],t[26:39],t[39:] m=range (4); forIinchm:PrintS3[i] forJinchm:Prints2[j],g2 (t[i],j)Printdefz93 ():" "* * Hanoi game * * This game is to move a tower you enter a decimal, this number can only be 2~7 this number represents the tower's layer \ n on a wooden board there are three poles, the leftmost pole from top to bottom, from the small to the large string of N-storey tower \ n, the purpose of the game is to move the tower on the leftmost pole to the right side of the pole, provided that only one disk can be moved at a time, \ n and does not allow the market to be placed on a small plate. If you enter any characters other than 2~7, then n=4\n" " defg (x, y): S4=x==1 and ' '*7or "'S3="-"* (ABS (x-y) ==2 and12or5) S3= X>y ands3+">" or "<"+S3PrintS4+STR (x) +s3+Str (y)return defMove (n,z,m,y):ifN>0:move (n-1, Z,y,m) g (z,m) Move (n-1, Y,m,z)returnT=raw_input ("Enter the number of layers in that tower") T=int (t)if not8>t>1:t=4Move (t,0,2,1) returndefz91 ():" "* * Man-machine guessing games * * This game is to guess the number. This number is four digits you enter a four-digit number, the number of people can only be 1~9 computer tells you that this four-digit number is correct, and there are several positions correct, for example, if that number is 1234, you guessed 2354\n then the computer will answer 3A1B says you have a four-digit number that's 3 correct, and there are 1 even positions right \ n" "e=caigame () E.start ( )defz87 ():#Black and white sub-swap defNEX (x):if '012' inchX:returnX.replace ('012',' About') if ' -' inchX:returnX.replace (' -','021') if 'Ten' inchX and '2102' not inchX:returnX.replace ('Ten',' on') if ' Geneva' inchX and '1021' not inchX:returnX.replace (' Geneva',' -') returnN=4T,r='1'*n,'2'*N x=t+'0'+R S=r+'0'+T whilex!=S:Printx x=NEX (x)Printsif __name__=='__main__': z91 ()#z93 ()
Python Exercise 2