def countConnected(self, board, side): """Counts how many pieces for the given side touch another piece of the same side.""" q = FIFO_Queue() visited = set() connected = 0 #get all pieces of one color on the board for row in range(self.size): for col in range(self.size): if board[row][col] == side: q.add( (row,col) ) while len(q) > 0: row, col = q.get() if (row,col) not in visited: visited.add( (row,col) ) isConnected = False for neighbor in self.getNeighbors(row,col): if board[ neighbor[0] ][ neighbor[1] ] == side: isConnected = True if isConnected: connected += 1 return connected
def getDests(self,piece): """ returns list of tuples of possible destinations """ possible = [] jumper= [] row = piece[0] col = piece[1] queue = FIFO_Queue() visited = set() queue.add(piece) neighbors = self.getNeighbors(piece[0],piece[1]) """ adds valid neighbors into list ofpossible adds blocked neighbors to check """ for (r, c) in neighbors: neighbor = self.board[r][c] if neighbor == -1: possible.append((r, c)) while len(queue) != 0: curr = queue.get() visited.add(curr) jumper = [] currNeighs = self.getNeighbors(curr[0],curr[1]) for (r,c) in currNeighs: if self.board[r][c] != -1: jumper.append((r,c)) #print "jumper",jumper for jump in jumper: dest = self.findJump(curr,jump) if dest[0] == -1 or dest in visited: continue possible.append(dest) queue.add(dest) #print "possible: ",possible #print "current: ", piece return possible
def countConnected(self, board, side): """Counts how many pieces for the given side touch another piece of the same side.""" queue = FIFO_Queue() counted = set() acc = 0 for row in range(self.size): for col in range(self.size): if board[row][col] == side: queue.add((row, col)) while len(queue) > 0: row, col = queue.get() for n in self.getNeighbors(row, col): r, c = n if board[r][c] == side and n not in counted: counted.add((r, c)) acc += 1 return (acc * side)
def whiteWins(self, board): """Returns True if white player wins, otherwise False.""" queue = FIFO_Queue() visited = set() # Add all locations of white pieces n the top row to queue for col in range(self.size): if board[0][col] == 'W': queue.add((0, col)) # Try to find a path to the bottom row while len(queue) > 0: row, col = queue.get() visited.add((row, col)) for n in self.getNeighbors(row, col): r, c = n if board[r][c] != 'W': continue if r == self.size-1: return True if n in visited or n in queue: continue queue.add(n) return False
def blackWins(self, board): """Returns True if black player wins, otherwise False.""" queue = FIFO_Queue() visited = set() # Add all locations of black pieces in the leftmost col to queue for row in range(self.size): if board[row][0] == 'B': queue.add((row, 0)) # Try to find a path to the rightmost col while len(queue) > 0: row, col = queue.get() visited.add((row, col)) for n in self.getNeighbors(row, col): r, c = n if board[r][c] != 'B': continue if c == self.size-1: return True if n in visited or n in queue: continue queue.add(n) return False
def whiteWins(self, board): """Returns True if white player wins, otherwise False.""" queue = FIFO_Queue() visited = set() # Add all locations of white pieces n the top row to queue for col in range(self.size): if board[0][col] == -1: queue.add((0, col)) # Try to find a path to the bottom row while len(queue) > 0: row, col = queue.get() visited.add((row, col)) for n in self.getNeighbors(row, col): r, c = n if board[r][c] != -1: continue if r == self.size-1: return True if n in visited or n in queue: continue queue.add(n) return False
def blackWins(self, board): """Returns True if black player wins, otherwise False.""" queue = FIFO_Queue() visited = set() # Add all locations of black pieces in the leftmost col to queue for row in range(self.size): if board[row][0] == 1: queue.add((row, 0)) # Try to find a path to the rightmost col while len(queue) > 0: row, col = queue.get() visited.add((row, col)) for n in self.getNeighbors(row, col): r, c = n if board[r][c] != 1: continue if c == self.size-1: return True if n in visited or n in queue: continue queue.add(n) return False
def countStronglyConnectedPositions(self, board, side): """ 计算牢固连接的位置 @param board - the current board @param side - the current player side """ queue = FIFO_Queue() counted = set() acc = 0 for row in range(self.size): for col in range(self.size): if board[row][col] == side: queue.add((row, col)) while len(queue) > 0: row, col = queue.get() for n in self.getStronglyConnectedPositions(row, col, side): r, c, s = n if board[r][c] == side and (r, c) not in counted: counted.add((r, c)) acc += s return (acc * side)
def countConnectedGap(self, board, side): """ Counts how many pieces for the given side touch another piece of the same side. @param board - the current board @param side - the current player side """ queue = FIFO_Queue() counted = set() acc = 0 for row in range(self.size): for col in range(self.size): if board[row][col] == side: queue.add((row, col)) while len(queue) > 0: row, col = queue.get() for n in self.getNeighborsGap(row, col, side): r, c, s = n if board[r][c] == side and (r, c) not in counted: counted.add((r, c)) acc += s return (acc * side)