def test_adjacent_indices(self):
        """ Should return the list of indices, with none out of range. """
        # Index is not at either edge
        index = 1
        expected = [index-1, index, index+1]
        limit = index+1
        self.assertListEqual(expected, Matrix._adjacent_indices(index, limit))

        # Index == limit
        index = limit
        expected = [index-1, index]
        self.assertListEqual(expected, Matrix._adjacent_indices(index, limit))

        # Index == 0 (first)
        index = 0
        expected = [index, index+1]
        self.assertListEqual(expected, Matrix._adjacent_indices(index, limit))
Example #2
0
def cascade_reveal(start_x, start_y, mine_matrix, click_map_id):
    """ Returns the list of dicts, representing the values to reveal. """
    # Where the revealed values go. The position records the x, y coordinates
    # Faster lookup than using the reveal list of dicts
    placeholder = Matrix(mine_matrix.height, width=mine_matrix.width,
                         init_value=None)
    blank_spaces = [(start_x, start_y)]  # queue of spaces to explore
    reveal = []

    # Look around each blank space
    while(blank_spaces):
        (focus_x, focus_y) = blank_spaces.pop()
        for x in Matrix._adjacent_indices(focus_x, placeholder.height-1):
            for y in Matrix._adjacent_indices(focus_y, placeholder.width-1):

                # Have we already seen this entry?
                if (x, y) == (focus_x, focus_y) or placeholder[x][y] is not None:
                    continue  # skip this entry

                value = mine_matrix[x][y]
                # We haven't come across this entry yet, but it still may
                # already be clicked
                placeholder[x][y] = value

                # Check if there's a ClickMap entry for this datapoint
                click = DBSession.query(models.PlayerMapData).filter_by(
                    player_map_id=click_map_id, row_num=x, col_num=y).first()
                if click is None:
                    # Save a ClickMap entry for this cell
                    recorded_click = models.PlayerMapData(
                        player_map_id=click_map_id, row_num=x, col_num=y,
                        value=const.PlayerMapDataValue.CLICKED)
                    DBSession.add(recorded_click)
                    reveal.append({'x': x, 'y': y, 'value': value})

                # If the value is another blank space, save to look later
                if value is const.CellStates.CLUE[0]:
                    blank_spaces.append((x, y))

    return reveal