Tron game for python bots.
- Clone this repo
- Install it in a virtual env with
pip install -e .
You can run a game using some defaults bot, with the following command:
pytron dumb_bot spiral
This will run a game with only two players: dumb_bot
(which will make random
movements) and spiral
(which will make a spiral movement)
The winner will be the bot that can make the longest movement without hit itself, other bot, or a border.
After running the game, you can visualize the match in a browser. Try it out!
To create a new bot, create a module in /pytron/bots/<your_bot_name>/main.py
, and
define a PlayerBot class, as follows:
import random
from pytron.bot import Bot, Action
class PlayerBot(Bot):
def get_action(self, board):
return Action.Right
In get_action
you can define the logic for your bot. That method will be called in
each iteration of the game. For example, in the previous example, the bot
will turn right in each iteration. The possible actions are Right
, Left
and
Forward
.
To create a smarter bot, you may need to know the current state of the game. You can
know everything you need about the state with the param board
.
board
is an instance of GameState
, and have the following attributes:
bots_path
: a list of all the positions for each bot, in each iteration. For example, if a bot starts in position (1, 1), and moves to (1, 2), and another bot starts in (3, 2) and moves to (2, 2), the current state ofbots_path
is:
[
# 1st it, 2nd it
[(1, 1), (1 2)], # first bot
[(3, 2), (2 2)], # second bot
]
The positions are read as (row_number, column_number).
-
bots_orientation
: the orientation of each bot, which means it will be the direction where it will move if the returend action isForward
. The possible values areOrientation.North
,Orientation.East
,Orientation.South
andOrientation.West
. -
used_positions
: A list with all the already occupied positions.
You can also found some important info in the bot instance (self
inside the
get_action
method.)
-
id
: id of the bot, which is also its index inboard.bots_path
andboard.bots_orientation
. Following the previous example ifself.id == 0
that meansself
is the bot that started in the position (1, 1) and now is currently in (1, 2). -
name
: the name of your bot (from the folder that contains themain.py
file) -
board_row_size
: the number of rows in the board. For example, ifboard_row_size == 10
, the last valid row index is9
, and if your bot goes to (10, 0) it will loose). -
board_column_size
: the number of columns in the board.