Skip to content

Input: The Game Object

beyarkay edited this page Dec 22, 2020 · 1 revision

The Game Object

The game object is where you can find all the important information about the game so that your bot can make a good move.

The game object contains things like:

  • A list of every bot in the game
  • A list of every battleground in the game
  • A description of how the battlegrounds connect to each other with ports
  • How long the game has been going on for, and other basic game information

Bots and Bot things

Make sure your script at the very least includes these lines at the top, in order to read in the game object:

import pickle
import sys
sys.path.append(sys.argv[1])
import util

def main():
    with open("game.pickle", "rb") as gamefile:
        game = pickle.load(gamefile)
    
    # Your game logic goes after this

Getting all the bots

Accessing every bot in the game (this list is not sorted):

all_the_bots = game.bots 

Get a specific bot's character representation

Get the upper-case character that represents the bot at index 0 in the game.bots list:

my_bot = game.bots[0]
icon_of_the_zeroth_bot = my_bot.bot_icon  # This will be a character like 'A', 'B', 'C', ..., 'Z'

Get a bot's stdout and stderr

When a bot's script is executed by the Genghis server, the stdout and the stderr can be retrieved like so:

my_bot = game.bots[0]
bots_std_out = my_bot.stdout # This will contain anything your bot printed while being run by the server
bots_std_err = my_bot.stderr # This will contain any errors produced by your bot while being run by the server

Get a bot's move_dict

The move_dict of any bot's previous turn is also stored, and can be accessed like so:

my_bot = game.bots[0]
my_bots_move_dict = my_bot.move_dict

The move_dict is formatted exactly like you would expect, for example:

{
    "action": "walk", 
    "direction": "ru"
}

Get a list of the bot's coins

A list of the coins owned by any bot is also accesible. Note that each item in the list is a Coins object, explained later.

my_bot = game.bots[0]
my_bots_coins = my_bot.coins

Get a dictionary representation of the bot

Each bot also has a method .to_dict() that will return a python dictionary with all of the information about the bot as key-value pairs. All the information available in the dictionary is accessible directly from the bot object

my_bot = game.bots[0]
my_bot_as_a_dictionary = my_bot.to_dict()

The above code would result in a dictionary that looks something like this:

{                                                                    
  "game_dir": "games/2020-11-10T02:52:25.301462",
  "username": "andy",
  "bot_path": "games/2020-11-10T02:52:25.301462/andy/template_bot.py",
  "abbreviations": ["A"],
  "bot_url": "https://people.cs.uct.ac.za/~KNXBOY001/genghis_client/template_bot.py",
  "name": "template bot",
  "coin_icon": "c",
  "bot_icon": "C",
  "stderr": "",
  "stdout": "Making motion GREEDY, bot is at (3, 14)",
  "move_dict": {"action": "walk", "direction": "ru"},
  "coins": [{"originator_icon": "C", "value": 3}],
}

Battlegrounds and Battleground things

Make sure your script at the very least includes these lines at the top, in order to read in the game object:

import pickle
import sys
sys.path.append(sys.argv[1])
import util

def main():
    with open("game.pickle", "rb") as gamefile:
        game = pickle.load(gamefile)
    
    # Your game logic goes after this

Getting all the battlegrounds

Access every battleground in the game (this list is not sorted):

all_the_battlegrounds = game.battlegrounds

Get the 2D array of a battleground

bg = game.battlegrounds[0]
battleground_map = bg.bg_map

Getting the value of a certain cell in the battleground's map

bg = game.battlegrounds[0]
row_index = 4
column_index = 8

cell_at_row_4_column_8 = bg.bg_map[row_index][column_index]

Getting the character that represents the battleground's port

Each battleground is represented by a single digit number 1 to 9 which is unique in a game.

bg = game.battlegrounds[0]
battleground_port_icon = bg.port_icon # this will be a single digit character, one of '1', '2', ..., '9'

Get a list of the locations of every port on the battleground

The port locations are stored as (row_index, column_index) pairs in a list:

bg = game.battlegrounds[0]
port_locations = bg.port_locations

You could iterate over all the port locations like so:

bg = game.battlegrounds[0]
for port_location in bg.port_locations:
    print("There is a port at row {}, column {}".format(port_location[0], port_location[1]))

Getting all the possible spawning locations of the battleground

Each battleground will only spawn new bots in certain locations, which are stored as (row_index, column_index) pairs in a list:

bg = game.battlegrounds[0]
all_bot_spawning_locations = bg.spawn_locations

You could iterate over all the bot spawning locations like so:

bg = game.battlegrounds[0]
for spawn_location in bg.spawn_locations:
    print("A bot could spawn at row {}, column {}".format(spawn_location[0], spawn_location[1]))

Get a dictionary representation of the battleground

Each battleground also has a method .to_dict() that will return a python dictionary with all of the information about the battleground as key-value pairs. All the information available in the dictionary is accessible directly from the battleground object

my_battleground = game.battlegrounds[0]
my_battleground_as_a_dictionary = my_battleground.to_dict()

The above code would result in a dictionary that looks something like this:

{
    "port_locations": [[18, 20], [6, 20], [4, 20]],
    "port_icon": "1",
    "bot_icons": ["A"],
    "game_dir": "games/2020-11-10T02:52:25.301462",
    "username": "abby",
    "battleground_path": "games/2020-11-10T02:52:25.301462/abby/template_battleground.txt",
    "battleground_url": "https://people.cs.uct.ac.za/~KNXBOY001/genghis_client/template_battleground.txt",
    "name": "template battleground",
    "spawn_locations": [
        [19, 9], [4, 9], [3, 9], [20, 9], [2, 9], [18, 10], [9, 10], [16, 10], [18, 9],
        [13, 9], [7, 9], [2, 10], [12, 10], [5, 10], [1, 9], [8, 10], [15, 9], [13, 10],
        [11, 9], [17, 10], [19, 10], [14, 10], [5, 9], [10, 9], [17, 9], [6, 9], [4, 10],
        [6, 10], [10, 10], [3, 10], [7, 10], [9, 9], [1, 10], [15, 10], [11, 10], [12, 9],
        [8, 9], [14, 9], [20, 10], [16, 9]
    ],
    "port_spawn_locations": [
        [18, 20], [6, 20], [4, 20], [2, 20], [17, 20], [9, 20], [4, 2], [5, 20], [16, 2],
        [17, 2], [5, 2], [18, 2], [7, 20], [3, 20], [10, 20], [8, 2], [11, 20], [12, 2],
        [15, 20], [19, 20], [15, 2], [11, 2], [6, 2], [10, 2], [16, 20], [14, 2], [2, 2],
        [8, 20], [12, 20], [3, 2], [13, 2], [9, 2], [13, 20], [7, 2], [19, 2], [14, 20]
    ],
    "bg_map": [
        [ "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#" ],
        [ "#", " ", " ", " ", " ", " ", " ", " ", " ", " ", "a", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "#" ],
        [ "#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "#" ],
        [ "#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "#" ],
        [ "#", " ", " ", " ", " ", " ", " ", " ", " ", " ", "A", " ", " ", " ", " ", " ", " ", " ", " ", " ", "4", "#" ],
        [ "#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "#" ],
        [ "#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "4", "#" ],
        [ "#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "#" ],
        [ "#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "#" ],
        [ "#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "#" ],
        [ "#", "#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "#" ],
        [ "#", "#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "a", " ", " ", "#" ],
        [ "#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "a", " ", "#" ],
        [ "#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "#" ],
        [ "#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "#" ],
        [ "#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "#" ],
        [ "#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "#" ],
        [ "#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "a", " ", " ", " ", " ", " ", "#" ],
        [ "#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "2", "#" ],
        [ "#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "#" ],
        [ "#", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "#" ],
        [ "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#" ]
    ]
}

The port graph: How all the battlegrounds connect together

The port graph is where you need to look to figure out how to get from one battleground to another. The port graph has to be in a certain format to interact with external code libraries (D3.js) and so has a slightly different format to the rest of the Game object. You can get the port graph like so:

graph = game.port_graph

It is a python dictionary with two keys:

  • graph['links']: This is a list of every link that connects two battlegrounds together, and is a dictionary with three keys:
    • graph['links']['source']: this is the 'source' (starting) battleground's port_icon character (like '1', '2', ..., '9')
    • graph['links']['target']: this is the 'target' (destination) battleground's port_icon character (like '1', '2', ..., '9')
    • graph['links']['value']: This is always 1 and is used by D3.js for visualisations. Don't worry about it
  • graph['nodes']: This is a list of every battleground. Each battleground has 2 keys:
    • graph['nodes']['id']: This is the battleground's port_icon character, like '1', '2', ..., '9'
    • graph['nodes']['label']: Deprecated. Don't worry about it.

So you could iterate over every link between battlegrounds like so:

graph = game.port_graph
for link in graph['links']:
    print("You can go from battleground {} to battleground {}".format(link['source'], link['target']))

Duplicate entries in the graph['links'] list are allowed, and means that there are multiple ports on the source battleground that all connect to the target battleground.

An example port_graph object might look like this:

{
    "links": [
        {"source": "1", "target": "2", "value": 1},
        {"source": "1", "target": "4", "value": 1},
        {"source": "1", "target": "4", "value": 1},
        {"source": "2", "target": "3", "value": 1},
        {"source": "2", "target": "1", "value": 1},
        {"source": "2", "target": "3", "value": 1},
        {"source": "3", "target": "4", "value": 1},
        {"source": "3", "target": "2", "value": 1},
        {"source": "3", "target": "2", "value": 1},
        {"source": "4", "target": "1", "value": 1},
        {"source": "4", "target": "3", "value": 1},
        {"source": "4", "target": "2", "value": 1}
    ],
    "nodes": [
        {"id": "1", "label": "abby/template battleground"},
        {"id": "2", "label": "boyd/template battleground"},
        {"id": "3", "label": "adam/template battleground"},
        {"id": "4", "label": "andy/template battleground"}
    ]
}

Other information

Other information about the game can be found as follows:

  • game.iterations: The number of complete turns made so far, where each turn consists of every bot going once
  • game.ticks: The number of game ticks, which is incremented after each bot makes its move
  • game.game_dir: This string uniquely identifies every game

Other information is also available as member variables to the game object. In json format, they might look like:

  ...
  "game_dir": "games/2020-11-10T02:52:25.301462",
  "coin_icons": ["a", "b", "c", "d"],
  "bot_icons": ["A", "B", "C", "D"],
  "port_icons": ["1", "2", "3", "4"],
  "iteration": 14,
  "turn_time": 0.05,
  "tick": 59,
  ...