def __init__(self): self.fps_clock = pygame.time.Clock() self.display = pygame.display self.font = pygame.font.Font('freesansbold.ttf', 15) self.difficulty = self.select_difficulty_menu() self.board_state = BoardState(self.difficulty[0], self.difficulty[1], self.difficulty[2]) self.loop()
def parse_next_game(self): """ IT'S A DOCSTRING NOW """ game = chess.pgn.read_game(self.pgn) if game is None: raise ValueError("End of file") board = game.board() if len(list(game.main_line())) <= 1: return None stop = randint(1, len(list(game.main_line()))) node = game while stop != 0: stop -= 1 next_node = node.variations[0] if next_node.comment.find("eval") == -1: return None board.push(next_node.move) node = next_node state_evaluation = float( node.comment.split(']')[0].replace('[%eval ', '').replace("#", "")) return BoardState(board, state_evaluation)
def iterative_dfs(board: [[]]): """ """ # Initializing boardstate # Checking size size = len(board) for i in range(size): if len(board[i]) != size: raise Exception("Board matrix is not a square") # Creating BoardState from a matrix root_board = BoardState(board) root_board.generate_islands() root_board.evaluate() # IDFS Algorithm global visited visited = 0 depth = 0 final_node = None while final_node == None: depth += 1 final_node, final_tree = dfs(root_board, depth) return final_node, depth, final_tree, visited
from boardstate import BoardState b = BoardState() b[(0, 0)] = 0 b[(1, 0)] = 1 b[(2, 0)] = 2 b[(3, 0)] = 3 assert b.win_state == BoardState.WinType.VERTICAL b = BoardState() b[(0, 0)] = 0 b[(0, 1)] = 1 b[(0, 2)] = 2 b[(0, 3)] = 3 assert b.win_state == BoardState.WinType.HORIZONTAL b = BoardState() b[(0, 0)] = 0 b[(1, 1)] = 1 b[(2, 2)] = 2 b[(3, 3)] = 3 assert b.win_state == BoardState.WinType.DIAGNAL b = BoardState()
def a_star(board: [[]]): """Function a_star Searches for a solution using the A* algorithm args: board ([[]]): the starting board returns: final_node (Tree.Node): the final node of the tree containing the solution moves_tree (Tree): the whole tree generated in the process nodes_visited (int): the number of tree nodes visited by the algorithm """ # Checking size size = len(board) for i in range(size): if len(board[i]) != size: raise Exception("Board matrix is not a square") # Creating BoardState from a matrix root_board = BoardState(board) root_board.generate_islands() root_board.evaluate() root_board.objective_function = 2 * heuristic.board_cohesion( root_board) - heuristic.board_mass(root_board) island_count = len(root_board.islands) # Initiating Tree moves_tree = Tree(root_board) # Initiating stack to_visit = [moves_tree.root] nodes_visited = 0 final_node = None while len(to_visit) > 0: # Take element from stack to_visit.sort(key=lambda node: node.content.objective_function, reverse=True) node_current = to_visit.pop() nodes_visited += 1 node_current.content.evaluate() if node_current.content.solved: final_node = node_current return final_node, moves_tree, nodes_visited # Generate neighbors board_next = deepcopy(node_current).content # Making all possible moves for i in range(island_count): for j in range(i + 1, island_count): # If bridge added succesfully, add to tree and stack and copy again if board_next.add_bridge(i, j): board_next.objective_function = 2 * heuristic.board_cohesion( board_next) - heuristic.board_mass(board_next) child = node_current.add_child(board_next) to_visit.append(child) board_next = deepcopy(node_current).content return final_node, moves_tree, nodes_visited