#!/usr/bin/env python import sys import random import copy # Fill this out! places=4 sticker=['red','green','blue','brown','orange','yellow','black','white'] colorcount=len(sticker) wholelist=[] class Row: """store a row, colorcount is important""" def __init__(self,list=None): if not list: list=[] for i in range(places): list.append(0) 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) def __str__(self): string="" for i in self.place: string=string+sticker[i]+" " return string def next(self): r=copy.copy(self.place) i=places-1 success=None while not success: r[i]+=1 if r[i] >= colorcount: # try to shift the next position if i == 0: raise "No rows left after %s" % self r[i]=0 i-=1 else: success=1 return Row(r) class Rating: """A rating of a try, black and white stickers""" def __init__(self,black,white): self.black=black self.white=white if (black+white) > places: raise "Impossible Rating: %d black and %d white" % (black,white) def testthetry(row1,rate,row2,debug=0): erased1=[] erased2=[] for i in range(places): erased1.append(0) erased2.append(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 "Erased 1 and 2", erased1,erased2 print "Row 1 and 2", row1, row2 print "blackmatch, whitematch", blackmatch,whitematch return 1 def initlist(): themax=colorcount ** places wholelist.append(Row()) while len(wholelist) < themax: wholelist.append(wholelist[-1].next()) def userinput(): noerror=1 while noerror: black=int(raw_input("black: ")) if black==places: print "white: 0" white=0 else: white=int(raw_input("white: ")) noerror= not(black>=0 and white>=0 and ((black+white)<=places)) return Rating(black,white) def guess(rowlist): """Returns the first row with the most different colors for getting the max info""" 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] sys.exit() print len(wholelist), "remaining possibilities, I try:" myguess=guess(wholelist) print myguess 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] 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,rate.black,rate.white print b,'valid: ',resu print count=count+1 if count % colors == 0: i=raw_input("--> ") def createrandom(): """Create a random row""" 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)