This project is read-only.

Project: BomberMan


The first version of BomberMan game for WindowsForms, the idea is to make it for SL and WP7, now done in TDD.

BomberMan.png

The GameEnvironment class:
public class GameEnvironment
{
	private readonly Action<Action> temporizedAction;

	private int currentX;
	private int currentY;

	public GameEnvironment(
		Map map,
		Action<Action> temporizedAction)
	{
		this.temporizedAction = temporizedAction;
		Map = map;
	}

	public Map Map { get; private set; }

	public void Action(ActionType action)
	{
		var nextX = currentX;
		var nextY = currentY;

		var position = Move(action, nextY, nextX);

		nextY = position.Y;
		nextX = position.X;

		if (action == ActionType.PlaceBomb)
		{
			PlaceBomb();
			return;
		}

		if (!CanMove(nextY, nextX))
		{
			return;
		}

		AdjustMap(nextY, nextX);
	}

	private void PlaceBomb()
	{
		Map[currentX, currentY] = ItemType.Bomb;

		var clearX = currentX;
		var clearY = currentY;

		temporizedAction(() => Map[clearX, clearY] = ItemType.Wall);
	}

	private bool CanMove(int x, int y)
	{
		if ((y < 0) || (y >= Map.Size.Width))
		{
			return false;
		}

		if ((x < 0) || (x >= Map.Size.Height))
		{
			return false;
		}

		if (Map[y, x] != ItemType.Wall)
		{
			return false;
		}

		return true;
	}

	private void AdjustMap(int nextY, int nextX)
	{
		if (Map[currentX, currentY] != ItemType.Bomb)
		{
			Map[currentX, currentY] = ItemType.Wall;
		}

		currentX = nextX;
		currentY = nextY;
		Map[currentX, currentY] = ItemType.Man;
	}

	private Point Move(ActionType action, int nextY, int nextX)
	{
		switch (action)
		{
			case ActionType.Up:
				nextY--;
				break;

			case ActionType.Left:
				nextX--;
				break;

			case ActionType.Right:
				nextX++;
				break;

			case ActionType.Down:
				nextY++;
				break;
		}

		return new Point(nextX, nextY);
	}
}


The unit test of the game environment.
[TestClass]
public class GameEnvironmentTest
{
	private readonly GameEnvironment gameEnvironment;

	public GameEnvironmentTest()
	{
		var mapLines = KnownMaps.Correct();
		var map = new Map(mapLines);

		gameEnvironment =
			new GameEnvironment(map);
	}

	[TestMethod]
	public void CanMovePlayerOnWall()
	{
		Assert.IsFalse(gameEnvironment.Action(ActionType.Right));
	}

	[TestMethod]
	public void CantMovePlayerOnRock()
	{
		Assert.IsFalse(gameEnvironment.Action(ActionType.Left));
	}
}

Last edited Oct 27, 2011 at 3:25 AM by teoarch, version 10

Comments

No comments yet.