Creating a Hivemind

a Python Implementation and Reinforcement Learning Adventure

Hive

A while back, my girlfriend and I went on a road trip in her father’s camper van. As a board game enthusiast, I couldn’t resist bringing along the highly portable game of Hive to try out. Hive is a fascinating abstract game where players aim to surround their opponent’s bee with various insects. Although I haven’t had the chance to play it extensively, I’ve always been curious about the ideal opening strategy and whether the game is truly balanced. Inspired by my longstanding desire to apply machine learning to a “real” problem, I embarked on this project to unravel the mysteries of Hive.

Getting Started with the Python Implementation

The first order of business was to construct the game’s core mechanics and logic in Python. The implementation is divided into two primary components: hive.py (the game implementation) and a separate GUI using Tkinter for visualization and interaction.

The hive.py file encompasses the game’s implementation, including the board, pieces, and game logic. Each piece is represented by a class that inherits from a base class called Piece. The base class takes care of managing moves and verifying if a move adheres to the game’s rules. Each piece (Bee, Beetle, Ant, Grasshopper, and Spider) then extends this class to implement their specific movement rules.

Let’s take a deeper look at how a piece is initialized in our Hive game implementation. The __init__ method of the Piece class sets up the foundation for each piece in the game, allowing them to interact with the game board and follow the game’s rules. Here’s the method and the main attributes of a Hive piece:

class Piece:
    # six hexagonal neighbors
    adjacent = [(1,1,0), (0,1,1), (-1,0,1), (-1,-1,0), (0,-1,-1), (1,0,-1)]

    def __init__(self, player, game):
        self.position = None
        self.edge = None
        self.player = player
        self.mounted = False
        self.game = game

Here are the main attributes of a Hive piece:

  • position: Represents the piece’s current position on the board. Initialized as None since the piece has not yet been placed.
  • edge: Represents the six neighboring positions of the piece on the hexagonal board. Initialized as None since the piece has no neighbors initially.
  • player: Stores the player that owns the piece (either player 1 or player 2).
  • mounted: A boolean value that indicates if the a beetle has climbed on top of the piece
  • game: A reference to the game object, which contains the overall game state and board.

As the game progresses, the other methods within the Piece class will interact with and modify these attributes, ensuring that each piece follows the game rules, such as updating its position on the board, maintaining connections to neighboring pieces, and handling special moves like beetle mounting.

Implementing the GUI

Once the game logic was in place, I created a straightforward GUI using Tkinter to visualize the game board and facilitate interaction. This enabled me to play the game and gain a deeper understanding of its mechanics.

Delving into the Game and Reinforcement Learning

With the Python implementation up and running, I’m excited to take the next step in this project by experimenting with deep reinforcement learning algorithms. Drawing on what I learned from the “Probabilistic Artificial Intelligence” lecture at ETH, I plan to implement an Actor-Critic algorithm to teach an AI to play Hive. This approach will hopefully provide insights into whether the starting player has a clear advantage and reveal any interesting emerging strategies.

Final Thoughts

This project has been a fantastic learning experience and a great opportunity to dive deeper into the captivating world of Hive. Implementing the game in Python has not only given me a better understanding of the game mechanics but also helped me sharpen my Python development skills.

As the next step, I will be working on integrating deep reinforcement learning algorithms into the game, which I’m excited to explore. This will allow me to investigate if the starting player holds a clear advantage and observe any emerging strategies.

Stay tuned for more updates on this project and other exciting adventures in the world of board games and programming! Remember to check back for the reinforcement learning implementation and the insights it may bring to the game of Hive.