Click here to Skip to main content
15,347,383 members
Articles / Programming Languages / C#
Posted 13 Apr 2004


29 bookmarked

Hexomania - A Game for Hexagons

Rate me:
Please Sign up or sign in to vote.
3.50/5 (22 votes)
13 Apr 20043 min read
A strategy game that you can play versus the computer or another player

Sample Image - hexomania.jpg


Hexomania is a strategy game that I've made and wanted to share with you. It contains a good AI for the computer play. I think this is a good example of what you can do with C#. It took 2 days for programming.

How to Play

Every player has a lot of pieces and some targets (circles) with the same color. The game is played in turns. Targets move randomly one step, every turn. You win if all his targets are imprisoned (can't move). You play by moving any of your pieces to an empty hex that is adjacent to another one of your pieces.

Click a piece to move. Click an empty hex to put the piece. You can change the board size (and number of pieces and targets of course). You can change the number of players.

I hope you enjoy the game.

The Game Classes Consists Of

  • Board: which saves information about almost everything (hex cells, ArrayList of teams), and also handles the drawing of the board. Contains some functions to help AI cost calculations per team (Form1 contains the actual AI that uses this function).
  • Team: which saves information about player name, control (human, CPU), and saves the targets positions of this player. (Note that the players pieces are saved in the board array.)
  • Form1: The main form of the game and handles mouse clicks for the users and some of the computer playing turn (AI)

The Computer AI

The AI is simple enough. It consists of the function Board.Cost(team) which calculates the cost for the current position of the board for a certain player. Based on the sum of free spaces around every target of the player. This means that the player wins when the Cost returns 0.

The Cost function works by creating a tmp array with the same size of the board, that initially contains zeros. Then search around every target of the player and add for every free space a certain amount of cost in the tmp array. This amount is big when the position is near the target, and small when far from it because we want the computer to fill the free space around the target. Then the Cost function returns the sum of all the tmp array.

For the computer to play its turn, it searches for the piece that can be removed producing the minimum cost. Then searches for the best position to put that piece in, based on (again) the minimum cost produced.

The search is done on all the available pieces to remove, then on all the free spaces that are adjacent to other pieces, to select a place to put on.

How to Save a Hex Grid in an Array

It is done by a normal 2 dimensional array. But the y positions are spaced by a half grid below, every Xindex mod 2 = 1, to create the hex shape. The next shape shows how we calculate the x,y index of the array:

Sample screenshot


  • 14th April, 2004: Initial version


This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below. A list of licenses authors might use can be found here.


About the Author

Bishoy Labib
Team Leader Link Development
Egypt Egypt

Comments and Discussions

GeneralMy vote of 5 Pin
conmajia4-Jul-12 16:33
Memberconmajia4-Jul-12 16:33 
GeneralMy vote of 5 Pin
Nsk-realty7-Feb-11 0:32
MemberNsk-realty7-Feb-11 0:32 
Generalthank's for code and demo Pin
Dan_4ugust12-Jun-09 22:55
MemberDan_4ugust12-Jun-09 22:55 
General[Message Deleted] Pin
Danny Rodriguez27-Jan-08 9:12
MemberDanny Rodriguez27-Jan-08 9:12 
GeneralGreat work Pin
Member 12154543-Jul-04 20:43
MemberMember 12154543-Jul-04 20:43 
GeneralGood. Pin
andrea contoli16-Jun-04 21:34
Memberandrea contoli16-Jun-04 21:34 
GeneralGreat Game But.. Pin
audiere12-May-04 13:30
Memberaudiere12-May-04 13:30 
GeneralGood work Pin
Christian Graus15-Apr-04 15:22
mveChristian Graus15-Apr-04 15:22 
GeneralRe: Good work Pin
Nish Nishant27-Apr-04 21:57
sitebuilderNish Nishant27-Apr-04 21:57 
QuestionNo code at ALL ? Pin
Christian Graus14-Apr-04 15:05
mveChristian Graus14-Apr-04 15:05 
GeneralGame cool, article not Pin
Dave Loeser14-Apr-04 14:27
MemberDave Loeser14-Apr-04 14:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.