tron

package
v0.0.0-...-9131a41 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 3, 2017 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

This file contains state structures for a Tron game. These tron games are a 2-dimensional grid with arbitrary numbers of players.

Index

Constants

View Source
const (
	DirectionNorth = "north"
	DirectionEast  = "east"
	DirectionSouth = "south"
	DirectionWest  = "west"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type TronCoord

type TronCoord struct {
	X int `json:"x"`
	Y int `json:"y"`
}

type TronState

type TronState struct {
	// A map of x, y coordinates to player indices, indicating which cells are
	// part of which player's "trail". The keys are strings due to those being
	// the only supported type by the JSON library.
	Cells map[string]map[string]int `json:"cells"`
	// The players slice is simply a lookup of a player's position, where each
	// player is tracked by its index in this list. If the player's position is
	// (-1, -1) then the player is dead.
	Players []TronCoord `json:"players"`
	// track the current direction the players are traveling in
	Directions []string
	// The width and height of the game grid.
	Width  int `json:"w"`
	Height int `json:"h"`
}

The state of the tron world, holds lists of coordinates for an arbitrary number of players. The top left cell is coordinate (0,0) like screen coords.

func NewTwoPlayerTron

func NewTwoPlayerTron(w, h int) *TronState

Build a blank tron world with 2 players.

func (*TronState) Actions

func (s *TronState) Actions(p int) interface{}

Returns the possible actions an agent can make given the current state. Each action is a unit tuple of directions that the player can travel in.

func (*TronState) Do

func (s *TronState) Do(p int, a string)

Commit an action for a player. The direction will be sent as a generic interface, and casted into the expected string

func (*TronState) Finished

func (s *TronState) Finished() bool

Check if the game is over. This happens if all but one player is dead. TODO: Currently this assumes only a 2 player game

func (*TronState) Kill

func (s *TronState) Kill(p int)

Kill a player.

func (*TronState) Result

func (s *TronState) Result() []int

Collect the result of the game TODO: Currently this assumes a 2-player game.

func (*TronState) Validate

func (s *TronState) Validate(p int, a string) bool

Check if an action is valid.

func (*TronState) View

func (s *TronState) View(p int) interface{}

Tron is a perfect-information game, so return the state regardless of player.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL