Skip to content

Instantly share code, notes, and snippets.

@iffy
Created September 11, 2011 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iffy/1209792 to your computer and use it in GitHub Desktop.
Save iffy/1209792 to your computer and use it in GitHub Desktop.

Hexagon Dots

depth

4

Challenge

Your challenge is to write a bot to play the Hexagon dots game. Your bot will play the game using HTTP requests with JSON-encoded payloads. The bot who beats the most opponents wins.

Gameplay and Board Layout

A game is played on a hexagonal board between two players (bots). On a player's turn, they draw a new line between two dots on the board. If the new line completes a hexagon, the player captures the hexagon and has another turn. The player with the most captured hexagons at the end of the game wins.

The game ends when all possible lines have been drawn.

An fresh 3 row x 5 column board:

o   o   o   o

o o o o o

o o o o

Note that rows alternate between short and long rows. The top row is always short and the bottom row is always short -- all boards have an odd number of rows.

Coordinates

Points on the board are indexed using a hexagonal coordinate system. The top row is row 0, the next row down is row 1 and so on. The left-most point in each row is column 0. Given this 3 row x 3 column board:

A   B

C D E

F G

The points would be addressed as follows:

Point Row Col
A 0 0
B 0 1
C 1 0
D 1 1
E 1 2
F 2 0
G 2 1

Disqualifying Moves

If a player (bot) attempts a disqualifying move, they lose the game immediately. Following are disqualifying moves:

Redrawing a line

It is illegal to draw a line on a line that has already been drawn.

Drawing in a captured hexagon

If the board is currently in this state:

A   B---C   D
   /     \    
E F G H I

/

J K---L M

It would be illegal to draw a line using G as an endpoint.

Taking too long

Once notified that it is your turn, you have 5 seconds to submit your move. Failing to do so results in immediate loss.

Drawing outside the board

It is illegal to draw a line outside the board.

Server Protocol

The game server uses HTTP with JSON payloads for both responses and request. Following is a typical game request/response example and the list of available commands and their responses.

Example interaction

Client                      Server

POST /               
{rows: 5, cols: 3}      ->

                            201 Created
                        <-  Location: /foobar

POST /foobar/players
{name: "Bot1"}          ->  

                            200 OK
                            X-Turn-Token: gorilla
                            {rows: 5,
                             cols: 3,
                             lines: [],
                             hexagons: [],
                             players: [...],
                        <-   state: 'in play'}

POST /foobar/moves
X-Turn-Token: gorilla
[{row: 0, col: 0},
 {row:0, col:1}]        ->

POST / (new game)

Payload:

{rows: <integer>,
 cols: <integer>}

Creates a new game of the given dimensions.

201 Created (response)

Response Headers:

Location: The <game_url> for the create game

If the game is succesfully created. The created game will be in the initiating state. The Location HTTP header is the url for the created game.

4XX (response)

If there is an error creating the game.

POST <game_url>/players (join game)

Payload:

{name: <string>}

Tells the server you'd like to join the game at <game_url> with the player name given.

200 OK (response)

Response Headers:

X-Turn-Token: A token you need to pass back on your next move

Payload:

<Game> object

This indicates that it's your turn to play. The game is now in the in play state. You must use the received X-Turn-Token when submitting the next move.

410 Gone (response)

Indicates that the game is full. Find another game.

POST <game_url>/moves (make move)

Headers:

X-Turn-Token: <Previous-X-Turn-Token>

Payload:

[<Coordinate>, <Coordinate>]

The payload indicates the line you'd like to draw.

200 OK (response)

Response Headers:

X-Turn-Token: A token you need to pass back on your next move

Payload:

<Game Delta>

This indicates that it's your turn to play again. You must use this new X-Turn-Token in your next <game_url>/moves request.

403 Forbidden` (response) ------------------------------------------------------------------------------- Payload:: An error describing your violation. You lose. Response Data Types =============================================================================== ------------------------------------------------------------------------------- Player ------------------------------------------------------------------------------- :: {name: <string>, score: <integer> or 'disqualified'} ------------------------------------------------------------------------------- Game ------------------------------------------------------------------------------- :: {rows: <integer>, cols: <integer>, lines: [<Line>*], hexagons: [<Hexagon>*], players: [<Player>*], state: <Game State>} ------------------------------------------------------------------------------- Game State ------------------------------------------------------------------------------- :: 'initiating', 'in play', or 'completed' ------------------------------------------------------------------------------- Line ------------------------------------------------------------------------------- :: {endpoints: [<Coordinate>, <Coordinate>] owner: <Player>} Coordinates in responses will always be sorted in row-major order (Coordinates are sorted by row, then Coordinates with the same row are sorted by col).owneris the player that placed the line. ------------------------------------------------------------------------------- Coordinate ------------------------------------------------------------------------------- :: {row: <integer>, col: <integer>} ------------------------------------------------------------------------------- Hexagon ------------------------------------------------------------------------------- :: {center: <Coordinate>, owner: <Player>} Represents a claimed Hexagon on the board. The six triangles surrounding the centerCoordinate`` are assigned to the owner.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment