Added a demo file showing how to use retro.graph.Graph

Note that you will need to run `mwc update` to update retro-games
to at least 1.1.1.
This commit is contained in:
Chris Proctor
2025-12-10 15:18:56 -05:00
parent 9805e9d9e4
commit 93becc1a2b
3 changed files with 58 additions and 27 deletions

41
graph_demo.py Normal file
View File

@@ -0,0 +1,41 @@
from retro.game import Game
from retro.graph import Graph
from retro.agent import ArrowKeyAgent
class Player(ArrowKeyAgent):
"""Player is a subclass of ArrowKeyAgent, so it inherits all of that
class's attributes. However, I'm redefining `try_to_move`: Player
only moves if the new position is on the board and unoccupied.
"""
def try_to_move(self, position, game):
if game.on_board(position) and game.is_empty(position):
self.position = position
g = Graph()
g.get_or_create_edge(15, 16, 16, 16)
g.get_or_create_edge(16, 16, 16, 14)
g.get_or_create_edge(16, 14, 13, 14)
g.get_or_create_edge(13, 14, 13, 18)
g.get_or_create_edge(13, 18, 18, 18)
g.get_or_create_edge(18, 18, 18, 12)
g.get_or_create_edge(18, 12, 11, 12)
g.get_or_create_edge(11, 12, 11, 20)
g.get_or_create_edge(11, 20, 20, 20)
g.get_or_create_edge(20, 20, 20, 10)
g.get_or_create_edge(20, 10, 9, 10)
g.get_or_create_edge( 9, 10, 9, 22)
g.get_or_create_edge( 9, 22, 22, 22)
g.get_or_create_edge(22, 22, 22, 10)
agents = g.get_agents()
for agent in agents:
agent.color = "gray_on_black"
player = Player()
player.position = (15, 15)
agents.append(player)
game = Game(agents, {}, board_size=(30, 30))
game.play()

42
poetry.lock generated
View File

@@ -1,4 +1,4 @@
# This file is automatically @generated by Poetry 2.0.0 and should not be changed by hand.
# This file is automatically @generated by Poetry 2.1.3 and should not be changed by hand.
[[package]]
name = "ansicon"
@@ -15,21 +15,23 @@ files = [
[[package]]
name = "blessed"
version = "1.20.0"
version = "1.25.0"
description = "Easy, practical library for making terminal apps, by providing an elegant, well-documented interface to Colors, Keyboard input, and screen Positioning capabilities."
optional = false
python-versions = ">=2.7"
python-versions = ">=3.7"
groups = ["main"]
files = [
{file = "blessed-1.20.0-py2.py3-none-any.whl", hash = "sha256:0c542922586a265e699188e52d5f5ac5ec0dd517e5a1041d90d2bbf23f906058"},
{file = "blessed-1.20.0.tar.gz", hash = "sha256:2cdd67f8746e048f00df47a2880f4d6acbcdb399031b604e34ba8f71d5787680"},
{file = "blessed-1.25.0-py3-none-any.whl", hash = "sha256:e52b9f778b9e10c30b3f17f6b5f5d2208d1e9b53b270f1d94fc61a243fc4708f"},
{file = "blessed-1.25.0.tar.gz", hash = "sha256:606aebfea69f85915c7ca6a96eb028e0031d30feccc5688e13fd5cec8277b28d"},
]
[package.dependencies]
jinxed = {version = ">=1.1.0", markers = "platform_system == \"Windows\""}
six = ">=1.9.0"
wcwidth = ">=0.1.4"
[package.extras]
docs = ["Pillow", "Sphinx (>3)", "sphinx-paramlinks", "sphinx_rtd_theme", "sphinxcontrib-manpage"]
[[package]]
name = "jinxed"
version = "1.3.0"
@@ -48,44 +50,32 @@ ansicon = {version = "*", markers = "platform_system == \"Windows\""}
[[package]]
name = "retro-games"
version = "1.1.0"
version = "1.1.1"
description = "A simple framework for Terminal-based games"
optional = false
python-versions = "<4.0,>=3.10"
groups = ["main"]
files = [
{file = "retro_games-1.1.0-py3-none-any.whl", hash = "sha256:c621117e4dd528b1e4870d897d00c4365566ab3ba965177e3996ed3c889dd9f8"},
{file = "retro_games-1.1.0.tar.gz", hash = "sha256:2167b574f42fe1e739b7c9ec75e98a9b76df42e2166376b85559291b3dc58f82"},
{file = "retro_games-1.1.1-py3-none-any.whl", hash = "sha256:2b2eac8c2667c69f1dd90c083a0f58b948e6fdecb184720133b819fa78f8a57f"},
{file = "retro_games-1.1.1.tar.gz", hash = "sha256:67ce475191f78d13148028de17b97de099226c4c581d5cf811cd9dfc7f5bb420"},
]
[package.dependencies]
blessed = ">=1.20.0,<2.0.0"
[[package]]
name = "six"
version = "1.17.0"
description = "Python 2 and 3 compatibility utilities"
optional = false
python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,>=2.7"
groups = ["main"]
files = [
{file = "six-1.17.0-py2.py3-none-any.whl", hash = "sha256:4721f391ed90541fddacab5acf947aa0d3dc7d27b2e1e8eda2be8970586c3274"},
{file = "six-1.17.0.tar.gz", hash = "sha256:ff70335d468e7eb6ec65b95b99d3a2836546063f63acc5171de367e834932a81"},
]
[[package]]
name = "wcwidth"
version = "0.2.13"
version = "0.2.14"
description = "Measures the displayed width of unicode strings in a terminal"
optional = false
python-versions = "*"
python-versions = ">=3.6"
groups = ["main"]
files = [
{file = "wcwidth-0.2.13-py2.py3-none-any.whl", hash = "sha256:3da69048e4540d84af32131829ff948f1e022c1c6bdb8d6102117aac784f6859"},
{file = "wcwidth-0.2.13.tar.gz", hash = "sha256:72ea0c06399eb286d978fdedb6923a9eb47e1c486ce63e9b4e64fc18303972b5"},
{file = "wcwidth-0.2.14-py2.py3-none-any.whl", hash = "sha256:a7bb560c8aee30f9957e5f9895805edd20602f2d7f720186dfd906e82b4982e1"},
{file = "wcwidth-0.2.14.tar.gz", hash = "sha256:4d478375d31bc5395a3c55c40ccdf3354688364cd61c4f6adacaa9215d0b3605"},
]
[metadata]
lock-version = "2.1"
python-versions = ">=3.10,<4.0"
content-hash = "03cc38c17964eb2c920ecf014cbfcf966c0c719418a127947b33382f086a0a6e"
content-hash = "ef9faaca1f4921bdb0f3ad32c00827ffc8373223b745fd6732e8e95d90972543"

View File

@@ -9,7 +9,7 @@ license = {text = "MIT"}
readme = "README.md"
requires-python = ">=3.10,<4.0"
dependencies = [
"retro-games (>=1.1.0,<2.0.0)"
"retro-games (>=1.1.1,<2.0.0)"
]