from Predicate import * '''the rules (stored as desiredState) for a QuantityPredicate are a dictionary mapping states onto quantities''' class QuantityPredicate(TestPredicate): def holds(self, board, coordinate): for state in self.desiredState.keys(): statecount = 0 for i in (coordinate[0]-1,coordinate[0],coordinate[0]+1): for j in (coordinate[1]-1,coordinate[1],coordinate[1]+1): if not (coordinate[0]==i and coordinate[1]==j): try: if board[i][j].state == state: statecount+=1 except AttributeError: continue if not statecount == self.desiredState[state]: return False return True def __str__(self): '''This function looks like one line of unreadable nonsense. This is what happens: 1. join the pairs of directions to states with a colon, making: direction:state 2. join that list using spaces 3. join QUANTITY, the result from 2, and some parenthesis ''' return ''.join(["(QUANTITY ",\ ' '.join((''.join([k.__str__(), ':', \ v.__str__()]) for (k, v) in self.desiredState.items())), ")"])