def getChainMoves(self): chainMoves = set() for x in range(self.width_): for y in range(self.height_): box = self.grid_.getBox(x, y) chain = [] chainBox = None if box.edgeCount() == 3: edgeType = box.getMissingEdges()[0] chain.append(Move(x, y, edgeType)) chainX, chainY = structure.getNeighborCoordinates(x, y, edgeType) chainBox = self.grid_.getBox(chainX, chainY) while chainBox is not None and chainBox.edgeCount() == 2: # Half-open chains edges = chainBox.getMissingEdges() if edges[0] == structure.oppositeEdge(edgeType): edgeType = edges[1] else: edgeType = edges[0] chain.append(Move(chainX, chainY, edgeType)) chainX, chainY = \ structure.getNeighborCoordinates(chainX, chainY, edgeType) chainBox = self.grid_.getBox(chainX, chainY) if chainBox is not None and chainBox.edgeCount() == 3: # Closed chain if len(chain) > 0: chainMoves.add(chain[0]) if len(chain) == 3: # 4 hard-hearted handout chainMoves.add(chain[1]) else: # Half-open chain if len(chain) == 1 or len(chain) >= 3: chainMoves.add(chain[0]) elif len(chain) == 2: # Hard-hearted handout chainMoves.add(chain[0]) chainMoves.add(chain[-1]) return chainMoves
def __hash__(self): if self.edgeType == structure.Edge.LEFT or \ self.edgeType == structure.Edge.TOP: return hash((self.x, self.y, self.edgeType)) neighborX, neighborY = \ structure.getNeighborCoordinates(self.x, self.y, self.edgeType) return hash((neighborX, neighborY, structure.oppositeEdge(self.edgeType)))
def __eq__(self, other): if other is None and self is not None: return False if other is not None and self is None: return False if self is None and other is None: return True if self.x == other.x and \ self.y == other.y and \ self.edgeType == other.edgeType: return True neighborX, neighborY = structure.getNeighborCoordinates(self.x, self.y, self.edgeType) if neighborX == other.x and \ neighborY == other.y and \ structure.oppositeEdge(self.edgeType) == other.edgeType: return True return False