예제 #1
0
    def reveal(self, x, y):
        grid = deserialize_game(self.state, self.width, self.height)

        if self.game_state == "C":
            grid = generate_game(grid, self.width, self.height, x, y, 0.15)
            self.start_time = datetime.datetime.utcnow()
            self.game_state = "S"

        tile = grid[y][x]
        bomb = is_bomb(tile)
        hidden = is_hidden(tile)

        if not hidden:
            raise ValueError('Cannot reveal a tile that is not hidden')
        grid[y][x] = set_hidden(False, tile)
        grid = propagate_unhide(grid, self.width, self.height)
        self.state = serialize_game(grid)

        if bomb:
            self.game_state = 'L'
            self.end_time = datetime.datetime.utcnow()
        else:
            if count_hidden(grid) == self.bombs:
                self.game_state = 'W'
                self.end_time = datetime.datetime.utcnow()
예제 #2
0
def count_hidden(grid):
    count = 0
    for row in grid:
        for tile in row:
            if is_hidden(tile):
                count += 1

    return count
예제 #3
0
def mask_hidden_data(tile):
    hidden = is_hidden(tile)
    flagged = is_flagged(tile)
    bomb = is_bomb(tile)
    adjacent = extract_adjacent(tile)

    if hidden:
        return create_tile(hidden, flagged, False, False)
    else:
        return create_tile(hidden, False, bomb, adjacent)
예제 #4
0
    def flag(self, x, y):
        grid = deserialize_game(self.state, self.width, self.height)
        tile = grid[y][x]
        hidden = is_hidden(tile)
        flagged = is_flagged(tile)

        if not hidden:
            raise ValueError('Cannot flag a tile that is not hidden')
        grid[y][x] = set_flagged(not flagged, tile)
        self.state = serialize_game(grid)
예제 #5
0
def propagate_unhide(grid, width, height):
    modified = 0
    for y in range(0, height):
        for x in range(0, width):
            tile = grid[y][x]
            if not is_hidden(tile) and not is_bomb(tile) and extract_adjacent(tile) == 0:
                for y_offset in range(-1, 2):
                    y_probe = y + y_offset
                    if 0 <= y_probe < height:
                        for x_offset in range(-1, 2):
                            x_probe = x + x_offset
                            if 0 <= x_probe < width:
                                tile_probe = grid[y_probe][x_probe]
                                if is_hidden(tile_probe):
                                    grid[y_probe][x_probe] = set_hidden(False, tile_probe)
                                    modified += 1

    if modified > 0:
        return propagate_unhide(grid, width, height)
    return grid
예제 #6
0
 def test_is_hidden(self):
     self.assertEqual(is_hidden(255), True)
     self.assertEqual(is_hidden(128), True)
     self.assertEqual(is_hidden(127), False)
     self.assertEqual(is_hidden(0), False)