Esempio n. 1
0
def run_baseline(board, fringe, explored_count, unexplored_cells,
                 undiscovered_mines, dim, score, knowledge_base):
    # 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)
            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
Esempio n. 2
0
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
    if knowledge_base[variables] < 0:
        knowledge_base[variables] = 0
    return knowledge_base
Esempio n. 3
0
def safe_cell_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 safe cells discovered by 1
        neighbor_cell.safe_cells += 1
Esempio n. 4
0
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.hidden_squares > 0 and (
                    cell.no_of_neigbors - cell.clue -
                    cell.safe_cells) == cell.hidden_squares:
                safe_cells.extend(
                    gb.get_neighbors(board, neighbor[0], neighbor[1]))

    # remove the duplicates and remove the uncovered cells from the list
    covered_mine_cells = list(
        OrderedDict.fromkeys(remove_uncovered(mine_cells, board)))
    covered_safe_cells = list(
        OrderedDict.fromkeys(remove_uncovered(safe_cells, board)))
    return covered_mine_cells, covered_safe_cells