def run_baseline(board, fringe, explored_count, unexplored_cells, undiscovered_mines, dim, score, knowledge_base, clue_prob): # print "Running baseline" while (fringe and len(fringe) > 0): # # print "Baseline Fringe: ", fringe ((x_cord, y_cord)) = fringe.pop(0) if explored_count == dim * dim: return explored_count, unexplored_cells, undiscovered_mines, score, knowledge_base # base line update # print "Running baseline on: ", x_cord, y_cord mine_cells, safe_cells = run_clue_check(x_cord, y_cord, board) # print "mine_cells", mine_cells # print "safe_cells", safe_cells neighbors = [] for cords in mine_cells: assert board[cords[0]][cords[1]].is_mine == True neighbors.extend(gb.get_neighbors(board, cords[0], cords[1])) undiscovered_mines = \ mark_cell_as_mine(cords[0], cords[1], board, undiscovered_mines) explored_count += 1 unexplored_cells.remove((cords[0], cords[1])) # print "after marking mine cell knowledge:" # gb.display_knowledge_base(knowledge_base) # gb.visualize_agent_board(game_board) score = score + 1 fringe.extend(mine_cells) if undiscovered_mines == 0: for unexplored_cell in unexplored_cells: if unexplored_cell not in safe_cells: safe_cells.append(unexplored_cell) for cords in safe_cells: assert board[cords[0]][cords[1]].is_mine == False neighbors.extend(gb.get_neighbors(board, cords[0], cords[1])) undiscovered_mines = query_cell(cords[0], cords[1], board, undiscovered_mines, clue_prob) explored_count += 1 unexplored_cells.remove((cords[0], cords[1])) # print "after querying safe cell knowledge:" # gb.display_knowledge_base(knowledge_base) # gb.visualize_agent_board(game_board) fringe.extend(safe_cells) knowledge_base = update_knowledge_base(knowledge_base, board, mine_cells, safe_cells) # remove the duplicates from the fringe and keep the latest entry fringe = list(OrderedDict.fromkeys(fringe)) return explored_count, unexplored_cells, undiscovered_mines, score, knowledge_base
def mine_found_update(row_index, col_index, board): # get all the neighbors of mine cells neighbors = gb.get_neighbors(board, row_index, col_index) for neighbor in neighbors: neighbor_cell = board[neighbor[0]][neighbor[1]] # for each neighbor reduce the number of hidden squares by 1 neighbor_cell.hidden_squares -= 1 # for each neighbor increase the number of mines discovered by 1 neighbor_cell.mines += 1
def run_clue_check(row_index, col_index, board): neighbors = gb.get_neighbors(board, row_index, col_index) neighbors.insert(0, (row_index, col_index)) mine_cells = [] safe_cells = [] for neighbor in neighbors: cell = board[neighbor[0]][neighbor[1]] if cell.value == 0: if cell.clue - cell.mines == cell.hidden_squares and cell.hidden_squares > 0: mine_cells.extend( gb.get_neighbors(board, neighbor[0], neighbor[1])) elif cell.no_of_neigbors - cell.clue - cell.safe_cells == cell.hidden_squares and cell.hidden_squares > 0: safe_cells.extend( gb.get_neighbors(board, neighbor[0], neighbor[1])) return [ list(OrderedDict.fromkeys(remove_uncovered(mine_cells, board))), list(OrderedDict.fromkeys(remove_uncovered(safe_cells, board))) ]
def get_equation(x_cord, y_cord, board, knowledge_base): cell = board[x_cord][y_cord] neighbors = gb.get_neighbors(board, x_cord, y_cord) variables = frozenset(remove_uncovered(neighbors, board)) knowledge_base[variables] = cell.clue - cell.mines return knowledge_base
def mine_found_update(row_index, col_index, board): neighbors = gb.get_neighbors(board, row_index, col_index) for neighbor in neighbors: neighbor_cell = board[neighbor[0]][neighbor[1]] neighbor_cell.hidden_squares = neighbor_cell.hidden_squares - 1 neighbor_cell.mines = neighbor_cell.mines + 1