A slightly more powerful starter package for Halite in C# based on the original community package.
To use, download the Halite executable form https://halite.io/downloads.php and execute runGame.bat
.
The package contains a basic random bot as the player and a random bot as the opponent (RandomBot.cs).
This is the main entry point for the app. Data is read from Console.in and translated into a map of sites corresponding to the playing grid.
Networking.GetFrame(map);
updates the sites in the map to take into account moves from the previous turn. Place your logic for movement after this line and build up a list of moves
for your sites here. This list is then passed onto the Halite app using Networking.SendMoves(moves);
This is the map corresponding to the playing grid. It consists of an x by y array of Site
s. The Site
s can be accessed in MyBot.cs
using map[x,y]
to get the site at the desired co-ordinate. The x
and y
arguments can be greater than the width/height of the map and will automatically be wrapped to the correct block.
A useful method exposed by the map
is map.GetMySites()
which returns a list of sites of which you are the owner.
Another useful method is GetSites(<func>)
. This allows you to retrieve all sites which match a particular filter.
Example usage:
To get all enemy sites use: map.GetSites(site=>site.IsEnemy())
This corresponds to a single block on the map grid. It has the following properties:
- Owner [int]
- Strength [int]
- Production [int]
- X [int]
- Y [int]
- Top [Site]
- Bottom [Site]
- Left [Site]
- Right [Site]
Site
also exposes the following methods:
- GetDirectionToNeighbour(Site neighbour) [Direction]
- IsMine() [bool]
- IsEnemy() [bool]
- IsEmpty() [bool]
This is a singleton which exposes the following properties:
- PlayerTag [int] - your player tag assigned by the Halite runner
- MapWidth [int] - the width of the playing grid
- MapHeight [int] - the height of the playing grid
- Turn [int] - the current turn
To access these properties anywhere in the app type Config.Get().<propertyName>
If you would like to use the powerful, built in VS debugger to debug your code then this is one of the many solutions but it's the one I prefer and use most.
- Add a reference
using System.Diagnostics;
in MyBot.cs - At the beginning of the main method in MyBot.cs, add the following line:
while(!Debugger.IsAttached);
- At the end of the first line of the batch file which builds your bot, add
/debug:full
so that it builds with the debug constant defined. - Add a
-t
flag at the end of the execution of the simulation in runGame.bat to disable the timeout. - Run the simulation. At this point the simulation should freeze on the initialisation of the bot.
- Ensure that you have set a break point somewhere in your code.
- In Visual Studio, in the toolbar click on
Debug>Attach to Process
and select MyBot.exe from the list - Debug to your heart's desire.