#!/usr/bin/env python import sys import random sticker=['red','gre','blu','lil','ora','tur'] places=4 wholelist=[] class Row: def __init__(self,list): self.place=list dict={} for item in self.place: if dict.has_key(item): dict[item]=1 else: dict[item]=1 self.colorcount=len(dict) del dict def printcol(self): string="" pre="\033[0;" post="\033[0m" for i in range(len(self.place)): if self.place[i]=="red": char="41" elif self.place[i]=="gre": char="42" elif self.place[i]=="ora": char="43" elif self.place[i]=="blu": char="44" elif self.place[i]=="lil": char="45" elif self.place[i]=="tur": char="46" string=string+pre+char+"m"+" "+post+" " return string class Rating: def __init__(self,black,white): self.black=black self.white=white def createrandom(): a=[] for i in range(places): a.append(sticker[random.randint(0,len(sticker)-1)]) return Row(a) def randomrate(): black=random.randint(0,places) white=random.randint(0,places-black) return Rating(black,white) def testthetry(row1,rate,row2,debug=0): erased1=[0,0,0,0] erased2=[0,0,0,0] # do the black ones blackmatch=0 for i in range(places): if row1.place[i]==row2.place[i]: blackmatch=blackmatch+1 erased1[i]=1 erased2[i]=1 if blackmatch != rate.black: return 0 # number of black ones is correct # count white matches whitematch=0 for i in range(places): if erased1[i]: continue color=row1.place[i] for j in range(places): if erased2[j]: continue if row2.place[j]==color: whitematch=whitematch+1 erased1[i]=1 erased2[j]=1 break if whitematch != rate.white: return 0 else: if debug: print erased1,erased2 print blackmatch,whitematch return 1 def initlist(): for i in range(len(sticker)): for j in range(len(sticker)): for k in range(len(sticker)): for l in range(len(sticker)): wholelist.append(Row([sticker[i],sticker[j],sticker[k],sticker[l]])) def userinput(): noerror=1 while noerror: black=int(raw_input("black: ")) if black==4: print "white: 0" white=0 else: white=int(raw_input("white: ")) noerror= not(black>=0 and white>=0 and ((black+white)<=4)) return Rating(black,white) def guess(rowlist): maxtillnow=0 maxatall=places for item in rowlist: if item.colorcount>maxtillnow: maxtillnow=item.colorcount bestrow=item if maxtillnow==maxatall: break return bestrow initlist() while 1: if len(wholelist) == 0: print "Error: Nothing left!" sys.exit() if len(wholelist) == 1: print "I got it!!" print "The result is:" print wholelist[0].printcol() sys.exit() print len(wholelist), "remaining possibilities, I try:" myguess=guess(wholelist) print myguess.printcol() result=userinput() # remove impossible rows for i in range(len(wholelist)-1,-1,-1): if not testthetry(myguess,result,wholelist[i]): del wholelist[i] sys.exit() for i in range(len(wholelist)): print wholelist[i].printcol() count=count+1 if count%25 == 0: raw_input("--> ") count=1 while 1: a=createrandom() b=createrandom() rate=randomrate() resu=testthetry(a,rate,b) if rate.black==0 and resu==1: print a.printcol(),rate.black,rate.white print b.printcol(),'valid: ',resu print count=count+1 if count % 6 == 0: i=raw_input("--> ")