Our students are creating classic boardgames using Silverlight. A problem that kept occurring was the following: “A player throws a certain number with the dice. How do I know, given his current location on the board, to which squares on the board the player is allowed to move to”.
I’ve written a small utility class that can be (re)used for simple applications where you have 2-dimensional map with certain squares on which the player can stand, and other not.
You can download the project here. Feel free to reuse/adapt. I’ve included to demo-usages.
Usage
Using the BoardMove class is, I think, fairly simple.
First you have to create a map on which you note what squares are allowable for a player to stand on. A ‘1’ denotes that this square is allowed to be moved to:
List<List<int>> map= new List<List<int>> { new List<int>(){1,1,1,1,1,1}, new List<int>(){1,0,1,0,0,1}, new List<int>(){1,1,1,1,0,0}, new List<int>(){0,0,0,1,1,0}, new List<int>(){1,1,1,1,0,0}, new List<int>(){1,0,1,0,0,1}, new List<int>(){1,0,1,1,1,1} };
Next we feed this map to our BoardMove object:
BoardMover bm = new BoardMover(map);
If we now wish to know the allowable moves, giving a begin location and the number of moves, we do the following:
List<Point> posiblities = new List<Point>(); posiblities = bm.GetPossibleMoves(speler, moves);
We now have a list of Points, i.e. (x,y) coordinates on the map, to which the player can move to.
That’s all.
Important note: I am by no means an AI or Path-finding coder, so what I’ve written is workable, but by no means a most optimal solutions. Feel free to comment.