コード例 #1
0
def create_fleet(ai_settings, screen, miness):
    exmine = Mines(ai_settings, screen)
    exmine_height = exmine.rect.height
    for mines_number in range(ai_settings.mines_counter):
        exmine = Mines(ai_settings, screen)
        exmine.y = exmine_height + 2 * exmine_height * mines_number
        exmine.rect.y = exmine.y
        miness.add(exmine)
コード例 #2
0
def solve_tiny():
    puzzle = Mines(
        """
        2---
        --1-
        -2--
        ---1
    """
    )
    puzzle.solve()
コード例 #3
0
 def __init__(self, board_size, num_mines):
     self.board_size = board_size
     self.num_mines = num_mines
     self.m = Mines(self.board_size, self.num_mines)
     self.grid = self.m.checkcell((0, 0))
     self.m.showcurrent()
     self.sentence_list = []
     self.unmarked = []
     self.safe = set()
     self.safe_moves = []
コード例 #4
0
def solve_small():
    puzzle = Mines(
        """
        --2-3-
        2-----
        --24-3
        1-34--
        -----3
        -3-3--
    """
    )
    puzzle.solve()
コード例 #5
0
    def __init__(self, world):

        self.id = type(self).instance_num # const and public
        type(self).instance_num += 1

        # TODO: switches and goals and hives
        self.spawner = Spawner(world)
        self.wall    = Wall(world)
        self.mines   = Mines(world)
        self.balls   = Balls(world)

        self.elapsed_time = KahanSum()

        self.can_mutate = True
コード例 #6
0
ファイル: Fishh.py プロジェクト: leerobby/sir_jude_task
def run_game():
    pygame.init()
    ai_settings = Settings()
    screen = pygame.display.set_mode(
        (ai_settings.screen_width, ai_settings.screen_height))
    pygame.display.set_caption("Fish")
    fish = Fish(ai_settings, screen)
    mines = Mines(ai_settings, screen)
    stats = Gamestats(ai_settings)
    sb = Scoreboard(ai_settings, screen, stats)
    miness = Group()
    gf.create_fleet(ai_settings, screen, miness)
    play_button = Button(ai_settings, screen, "Play")
    pygame.mouse.set_visible(True)
    while True:
        gf.check_events(ai_settings, screen, stats, sb, play_button, fish,
                        miness)
        gf.update_screen(ai_settings, screen, stats, sb, fish, mines, miness,
                         play_button)
        if stats.game_active:
            fish.update()
            gf.update_miness(ai_settings, stats, sb, screen, fish, miness)
コード例 #7
0
class PhysicsState:

    instance_num = 0

    def update_ball_vels(self, ball_inputs):
        vels = []
        ids = self.balls.get_ids()
        for ball_index in range(len(ids)):
            input_index = ids[ball_index]
            input = ball_inputs[input_index]
            # The AI needs to know everything that could possibly
            # affect the ball - that is, the whole const PhysicsState -
            # so pass `self`.
            # Also give a ball index to the input so the AI knows
            # which ball it's controlling.
            vel = input.calc_vel(ball_index, self)
            vels.append(vel)
        self.invalidate_public_data()
        self.balls.set_vels(vels)

    def get_nearest_ball_sqr_dist(self, pos):
        # TODO: `sqr_dist` should be shortest path distance.
        nearest_ball_pos, sqr_dist = get_nearest(pos, self.balls.get_poss())
        return sqr_dist

    def can_add_mine(self, pos):
        is_collision = (self.wall.is_circle_collision(pos, self.mines.rad))
        sqr_dist = self.get_nearest_ball_sqr_dist(pos)
        return self.spawner.can_add_mine(sqr_dist) and not is_collision

    def maybe_add_mine(self, mine_input):

        pos = mine_input.calc_pos(self) # const self
        self.invalidate_public_data()
        if not pos:
            return

        sqr_dist = self.get_nearest_ball_sqr_dist(pos)
        if not self.spawner.try_add_mine(pos, sqr_dist):
            # TODO: Notify of failure to spawn mine at `pos`.
            pass

    def maybe_spawn_mine(self):
        pos = self.spawner.try_take_live_mine()
        if pos is not None:
            self.mines.add(pos)

    # For SimplePhysicsState:
    # def move_mines_without_walls(self, dt): # Mines could be moved in parallel.
    #     for i in range(0, len(self.mine_poss)):
    #         self.update_mine_vel(i, self.ball_pos)
    #         self.mine_poss[i] += vec_scale(self.mine_vels[i], dt)

# Public (including constants):

    def reset(self, initial_wall_bitmap):

        assert(self.can_mutate)

        self.elapsed_time = KahanSum()

        # TODO: switches and goals and hives
        self.wall.reset(initial_wall_bitmap)
        self.spawner.reset()
        self.balls.reset()
        self.mines.reset()

    def __init__(self, world):

        self.id = type(self).instance_num # const and public
        type(self).instance_num += 1

        # TODO: switches and goals and hives
        self.spawner = Spawner(world)
        self.wall    = Wall(world)
        self.mines   = Mines(world)
        self.balls   = Balls(world)

        self.elapsed_time = KahanSum()

        self.can_mutate = True

    # `get_*` functions return constant values (including elements of lists).

    # If public data has been retrieved by, for example, calling
    # `physics_state.get_mines`, then it must be invalidated before
    # mutating the PhysicsState (for example, by calling `advance`).
    # After invalidation, `get_mines` must be called again for new data.
    def invalidate_public_data(self):
        self.can_mutate = True

    def get_elapsed_time(self):
        return self.elapsed_time.get()

    def get_mines(self):
        self.can_mutate = False
        return self.mines

    def get_balls(self):
        self.can_mutate = False
        return self.balls

    def get_spawner(self):
        self.can_mutate = False
        return self.spawner     # Also constant - can't spawn mines.

    def get_wall(self):
        self.can_mutate = False
        return self.wall

    # TODO: switches, goals, hives

    def has_finished(self):
        arent_any_balls = (len(self.balls.get_poss()) == 0)
        are_all_goals_reached = False # TODO
        return arent_any_balls or are_all_goals_reached

    def toggle_invincible_balls(self):
        assert(self.can_mutate)
        self.balls.toggle_invincibility()

    def advance(self, mine_input, ball_inputs, dt):

        assert(self.can_mutate)

        if self.has_finished():
            return False

        # Simulate spawner.
        self.spawner.advance(dt)
        self.maybe_spawn_mine()

        # Use inputs.
        self.update_ball_vels(ball_inputs)
        self.maybe_add_mine(mine_input)    # Allow AI to prevent explosion.
        if self.spawner.is_exploding():
            self.mines.explode()

        # Simulate balls.
        self.balls.advance(self.wall, dt)

        # Simulate mines.
        ball_poss = self.balls.get_poss()
        self.mines.advance(ball_poss, self.wall, dt)

        # Simulate ball-mine collisions.
        collisions = self.mines.find_ball_collisions(ball_poss, self.balls.rad)
        self.balls.update_collided(collisions)
        if any(collisions):
            print('time:\t' + str(self.get_elapsed_time())) # For debugging

        # Finish step.
        self.elapsed_time.add(dt)
        return True
コード例 #8
0
class Solver2:
    def __init__(self, board_size, num_mines):
        self.board_size = board_size
        self.num_mines = num_mines
        self.m = Mines(self.board_size, self.num_mines)
        self.grid = self.m.checkcell((0, 0))
        self.m.showcurrent()
        self.sentence_list = []
        self.unmarked = []
        self.safe = set()
        self.safe_moves = []

    def sweep(self):
        self.__init_safe()
        self.__init_unmarked()
        #
        # before while loop
        #

        while not self.m.checkmines():

            self.__build_knowledge()

            for s1 in self.sentence_list:
                for s2 in self.sentence_list:
                    if s2.cells.issubset(
                            s1.cells) and s2 != s1 and len(s2.cells) != 0:
                        new_cells = s1.cells.difference(s2.cells)
                        new_count = s1.count - s2.count
                        new_sentence = Sentence(new_cells, new_count)
                        if not self.sentence_list.__contains__(new_sentence):
                            self.sentence_list.append(new_sentence)

            # sets all cells in sentences to mines that have equal values to their count
            for sen in self.sentence_list:
                cells_copy = sen.cells.copy()
                if sen.count == len(cells_copy):
                    # self.sentence_list.remove(sen)
                    for cell in cells_copy:
                        sen.mark_mine(cell)
                        for s in self.sentence_list:
                            s.mark_mine(cell)
                        if not set(self.m.flags).__contains__(cell):
                            self.m.flags.append(cell)
                if sen.count == 0:
                    for cell in cells_copy:
                        if self.unmarked.__contains__(cell) and not set(
                                self.m.flags).__contains__(cell):
                            self.safe_moves.append(cell)
                            sen.mark_safe(cell)
                            for s in self.sentence_list:
                                s.mark_safe(cell)

            if len(self.safe_moves) == 0:
                self.__make_random_move()

            #makes all the safe moves
            for m in self.safe_moves:
                self.__mark_safe(m)
                self.m.showcurrent()
            self.safe_moves = []

            self.m.showcurrent()

    def __make_random_move(self):
        for i in range(self.board_size):
            for j in range(self.board_size):
                if self.grid[i][j] == ' ' and not set(
                        self.m.flags).__contains__((i, j)):
                    self.__mark_safe((i, j))
                    print("MARKING SAFE:", (i, j))
                    self.m.showcurrent()
                    return

    def __mark_safe(self, cell):
        self.grid = self.m.checkcell(cell)
        self.safe.add(cell)
        self.unmarked.remove(cell)

    def __init_unmarked(self):
        for i in range(self.board_size):
            for j in range(self.board_size):
                if not self.grid[i][j].isnumeric() and not set(
                        self.m.flags).__contains__((i, j)):
                    self.unmarked.append((i, j))

    def __init_safe(self):
        for i in range(self.board_size):
            for j in range(self.board_size):
                if self.grid[i][j].isnumeric():
                    self.safe.add((i, j))

    def __build_knowledge(self):
        self.sentence_list = []
        #goes through the grid and finds a number other than zero and creates a sentence from it
        for i in range(self.board_size):
            for j in range(self.board_size):
                added = False
                if self.grid[i][j] != "0" and self.grid[i][
                        j] != ' ' and not set(self.m.flags).__contains__(
                            (i, j)):
                    added = True
                    tup = (i, j)
                    c_value = int(self.grid[tup[0]][tup[1]])
                    adj_set = self.__get_adj_set(tup)
                    new_sentence = Sentence(adj_set, c_value)
                    self.sentence_list.append(new_sentence)

                if j == self.board_size - 1 and not added:
                    break

    def __get_adj_set(self, tup):
        #creates a set of all empty adjacent cells of tup

        x = tup[0]
        y = tup[1]
        adj_set = set()

        if x - 1 >= 0 and y - 1 >= 0:
            top_left = self.grid[x - 1][y - 1]
            if top_left == ' ':
                adj_set.add((x - 1, y - 1))

        if x - 1 >= 0:
            top_center = self.grid[x - 1][y]
            if top_center == ' ':
                adj_set.add((x - 1, y))

        if x - 1 >= 0 and y + 1 <= self.board_size - 1:
            top_right = self.grid[x - 1][y + 1]
            if top_right == ' ':
                adj_set.add((x - 1, y + 1))

        if y + 1 <= self.board_size - 1:
            middle_right = self.grid[x][y + 1]
            if middle_right == ' ':
                adj_set.add((x, y + 1))

        if x + 1 <= self.board_size - 1 and y + 1 <= self.board_size - 1:
            bottom_right = self.grid[x + 1][y + 1]
            if bottom_right == ' ':
                adj_set.add((x + 1, y + 1))

        if x + 1 <= self.board_size - 1:
            bottom_middle = self.grid[x + 1][y]
            if bottom_middle == ' ':
                adj_set.add((x + 1, y))

        if x + 1 <= self.board_size - 1:
            bottom_left = self.grid[x + 1][y - 1]
            if bottom_left == ' ':
                adj_set.add((x + 1, y - 1))

        if y - 1 >= 0:
            middle_left = self.grid[x][y - 1]
            if middle_left == ' ':
                adj_set.add((x, y - 1))

        return adj_set
コード例 #9
0
def main():

    print("Field Test One\n")
    minefield = Mines()
    print(minefield)
    minefield.set_mines(6)
    minefield.place_mines()
    print(minefield)

    print("\nField Test Two\n")
    minefield = Mines('#', 8, 9)
    print(minefield)
    minefield.set_mines(8)
    minefield.place_mines()
    print(minefield)