Files
lab_reinforcement_learning/games/snake/observation.py
Chris Proctor a9385f8296 Refactoring lab
2026-06-26 13:27:11 -04:00

19 lines
699 B
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import numpy as np
from retro.views.headless import HeadlessView
from retro_gamer.observation import egocentric_board, encode_board, encode_state
CHARACTER_SET = ["@", "*", ">", "<", "^", "v"]
RADIUS = 8
def egocentric_observation(game):
"""17×17 window centered on the snake's head, plus apple_dx and apple_dy."""
view = HeadlessView()
view.on_game_start(game)
view.render(game)
head = game.get_agent_by_name("Snake head")
cropped = egocentric_board(view.board_characters, head.position, RADIUS)
board_vec = encode_board(cropped, CHARACTER_SET).flatten()
extras = encode_state(game.state, ["apple_dx", "apple_dy"])
return np.concatenate([board_vec, extras])