예제 #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 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)
예제 #3
0
def generate_game(grid, width, height, clicked_x, clicked_y, density=0.15):
    tiles_total = width * height
    tiles_remaining = tiles_total
    bombs_remaining = math.floor(density * width * height) - 1

    # Place bombs on the grid
    for y in range(0, height):
        row = []
        for x in range(0, width):
            tile = grid[y][x]
            if not (y == clicked_y and x == clicked_x):
                bomb = False
                if random.uniform(0, 1) < bombs_remaining / tiles_remaining:
                    bomb = True
                grid[y][x] = create_tile(True, is_flagged(tile), bomb)
                if bomb:
                    bombs_remaining -= 1
                tiles_remaining -= 1
            else:
                grid[y][x] = create_tile(True, False, False)
        grid.append(row)

    # Count bombs adjacent to each square
    for y in range(0, height):
        for x in range(0, width):
            tile = grid[y][x]
            bomb = is_bomb(tile)
            if not bomb:
                count = 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:
                                if is_bomb(grid[y_probe][x_probe]):
                                    count += 1
                grid[y][x] = set_adjacent(count, tile)

    return grid
예제 #4
0
    def bombs(self):
        """
        Returns a representation of the state that is scrubbed of information that the client does not need to know
        :return: array of arrays, first index is vertical, second is horizontal, values are tile values
        """
        # removing from the end of a list is easier / faster in python, so we reverse the list then pop

        flat_tiles = [int(tile) for tile in self.state.split(',')]
        count = 0

        # x is horizontal, y is vertical, top left is 0, 0
        for tile in flat_tiles:
            if is_bomb(tile):
                count += 1

        return count
예제 #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_bomb(self):
     self.assertEqual(is_bomb(255), True)
     self.assertEqual(is_bomb(32), True)
     self.assertEqual(is_bomb(223), False)
     self.assertEqual(is_bomb(0), False)