A small C# helperclass for boardgame-applications: allowable moves

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.


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.

Geef een reactie

Vul je gegevens in of klik op een icoon om in te loggen.

WordPress.com logo

Je reageert onder je WordPress.com account. Log uit /  Bijwerken )


Je reageert onder je Twitter account. Log uit /  Bijwerken )

Facebook foto

Je reageert onder je Facebook account. Log uit /  Bijwerken )

Verbinden met %s

Deze site gebruikt Akismet om spam te bestrijden. Ontdek hoe de data van je reactie verwerkt wordt.

%d bloggers liken dit:
search previous next tag category expand menu location phone mail time cart zoom edit close