Hex

A fairly challenging game that will test your ability to write clever bots

The rules behind this game are simple, but there is a surprising amount of strategy that is involved at high levels of play.

Objective

The objective of the game is to make an unbroken path from one side of the board to the other. Every hexagon in this path has to be of only your colour and has to match the appropriate sides. In every game there’s exactly one winner (there’s no way to reach a completely coloured board without completing at least one of the paths)!

Players make alternating moves to fill the empty squares on the board with their colour. No hexagons can be re-coloured or removed, so the board will gradually fill up until there is a path for one of the players.

Strategy

There is much to consider when implementing an advanced AI for this game. One important thing to note is that the offence and the defence in this game are equivalent - a connection between your sides will prevent connections for the opponent. Here are a few examples of some of the basic things you could further look at:

Centralised play

The centre of the board is the most important area, since there are many different paths that can be made in all directions. Consider this when you make a move and try to give yourself the most opportunities to make the winning path in the future!

Opponent's paths

It is important to somehow keep track of how the opponent is doing. The best moves will not only get you closer to joining the two sides of the board, but also prevent the other player in doing so, by getting in the way of their threatening path. This will give a better chance of not getting blocked by the path that is providing the threat in the first place, so will put you in a better position.

The two-bridge

This set-up is almost as strong as having the two pieces connected, since the only way for the opponent to separate these is to place their colour in both the hexagons in the middle, which you can counter as soon as they fill one. This will allow you to move through the board at double the speed

Programmer's Reference

Your Main Function: calculateMove()

The calculateMove() function is the equivalent of your main function. This is where you need to make your changes and from where you will control the game. The calculateMove() function:

  1. Receives information about the game in a Python dictionary called gameState().

  2. Returns a game move.

Understanding the Game State

The gameState is where all of the game information is held. It is a Python dictionary. Examples of accessing data in the gameState would be:

gameState["Board"][2][4]
gameState["Role"]

Your calculateMove() function will be passed the current state of the game, the 'gamestate'. The gamestate is a python dictionary containing the following keys:

Board - A 2-dimensional array of integers representing the state of the board. (See Understanding The Board)

IsMover - A Boolean value indicating whether it is your turn to move. It will always be true when in the calculateMove() function. (Delve deeper into the code if you want to do some processing during your opponent's turn)

ResponseDeadline - The epoch time, in milliseconds, that a successful move has to be sent and received by to prevent you from timing out.

Role - An integer representing weather you are blue or red. (0-blue, 1-red)

GameStatus - A string that will have value "RUNNING" if the game is in progress or a reason the game has ended otherwise.

InvalidMove - always returns 'None' if a valid move has been made, otherwise returns the move that caused this error.

OpponentId - A string containing the name of your opponent.

Here is an example of a valid gamestate that could be returned:

{
'Board':
[[-1, 1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, 1, 1],
[-1, -1, -1, 0, 0, 1, -1],
[-1, 0, -1, -1, 0, 0, 0],
[-1, -1, -1, -1, 0, -1, -1],
[-1, 1, -1, -1, -1, 1, -1],
[-1, -1, -1, -1, 1, -1, -1]],
'IsMover': True,
'ResponseDeadline': 1563869183634,
'Role': 0,
'GameStatus': 'RUNNING',
'InvalidMove': None,
'OpponentId': 'housebot-practise'
}

Understanding the Board

A board element in the gamestate can contain the following values:

Value

Description

-1

An empty hexagon

0

A blue hexagon

1

A red hexagon

For example the value gameState["Board"][2][4] represents row 2 along the blue edge and column 4 along the red edge

Making a Valid Move

Your move should be a dictionary with the key value pair ‘Pos’ which takes a two integer list that represents the row and column of your new hexagon. It must be in the range of the board and cannot override an existing hexagon.

Here’s an example of a move:

{ “Pos”: [2,4] }

Jargon

Epoch time - The number of seconds (or in our case milliseconds) that have elapsed since January 1 1970 00:00 UTC