5,427,813 members and growing! (15,298 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Game Development » Games     Intermediate

Hexomania - A Game for Hexagons

By Bishoy Labib

A strategy game that you can play versus the computer or another player.
C#, Windows, .NET 1.1, .NETVisual Studio, VS.NET2003, Dev

Posted: 13 Apr 2004
Updated: 13 Apr 2004
Views: 31,289
Bookmarked: 16 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
18 votes for this Article.
Popularity: 4.22 Rating: 3.36 out of 5
5 votes, 27.8%
1
3 votes, 16.7%
2
0 votes, 0.0%
3
1 vote, 5.6%
4
9 votes, 50.0%
5

Sample Image - hexomania.jpg

Introduction

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 number of players.

I Hope you enjoy the Game.

The game classes consists of

  • Board: which saves information about almost every thing (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 is initially contains zeros. Then searching around every target of the player and adding 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 another 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

License

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


Technical Lead
Link Development
Egypt

http://bishoylabib.blogspot.com
Occupation: Team Leader
Location: Egypt Egypt

Other popular Game Development articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 8 of 8 (Total in Forum: 8) (Refresh)FirstPrevNext
Subject  Author Date 
General[Message Deleted]memberDanny Rodriguez10:12 27 Jan '08  
GeneralGreat workmemberRiku-Juhani Niemlä21:43 3 Jul '04  
GeneralGood.memberacontoli22:34 16 Jun '04  
GeneralGreat Game But..memberklexys14:30 12 May '04  
GeneralGood workmemberChristian Graus16:22 15 Apr '04  
GeneralRe: Good workstaffNishant S22:57 27 Apr '04  
GeneralNo code at ALL ?memberChristian Graus16:05 14 Apr '04  
GeneralGame cool, article notmemberDave Loeser15:27 14 Apr '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 13 Apr 2004
Editor: Nishant Sivakumar
Copyright 2004 by Bishoy Labib
Everything else Copyright © CodeProject, 1999-2008
Web10 | Advertise on the Code Project