Ejemplo n.º 1
0
    def place_fruit(self, pos: Position = None):
        if pos is None:
            pos = Position.random(self.game_map.n, self.game_map.m)

        while pos in self.snake:
            pos = Position.random(self.game_map.n, self.game_map.m)
        self.game_map.set(pos, -1)
        return pos
Ejemplo n.º 2
0
def test_table(table: Table, n: int):
    map = GameMap(n, n)
    game = Game(map, [Position.random(n, n)])
    # game = Game(map, [Position.random(n, n)])
    while True:
        print('Current State')
        game.print()
        current_state = calculate_distances(game)
        best_action = take_action(current_state, table)
        print(f'Action: {best_action}')
        result = game.step(best_action)
        print(f'Result of move: {result}')
        if result < 0:
            print('Game over')
            return
        sleep(2)
Ejemplo n.º 3
0
def start_training(game: Game, table: Table,
                   settings: QLearningSettings) -> Table:
    current_episode = 0
    highest_score = -1
    while current_episode < settings.episodes:
        current_state = calculate_distances(game)
        best_action = take_action(current_state, table)
        if random.random() < settings.epsilon:
            random_action = random.randint(0, 3)
            best_action = list(Direction)[random_action]
        success = update_state(best_action, table, settings, game)
        if not success:
            score = len(game.snake)
            print(f'Score: {score}')
            print(f'Epsiode: {current_episode + 1}')
            if highest_score < score:
                highest_score = score
            start = Position.random(game.game_map.n, game.game_map.n)
            game = Game(game.game_map, [start])
            current_episode += 1
    print(f'Highest Score: {highest_score}')
    return table