Skip to content

Commit

Permalink
Přidány testy pro metody modelu Board
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominik Tulak committed Nov 7, 2023
1 parent 443c0d6 commit 2db960c
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 15 deletions.
2 changes: 1 addition & 1 deletion SnakeGame/Interfaces/IBoard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public interface IBoard
/// <returns>Pokud tick proběhl bez chyb, vrátí nulu (0). Pokud hra skončila (had narazil), vrátí aktuální score (velikost hada)</returns>
public int Tick();
/// <summary>
/// Vrací všechny instance třídy Tile (políčka), které herní deska obsahuje.
/// Vrací všechny instance třídy Tile (políčka), které herní deska obsahuje. Nejdříve všechna políčka hada, potom všechna políčka jídla.
/// </summary>
/// <returns></returns>
public List<Tile> DumpBoard();
Expand Down
115 changes: 101 additions & 14 deletions SnakeGameTests/Models/BoardTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@ public void SnakeMovementTest()

//Kolize hada s hadem
board = PrepareBoard(Snake.Direction.Right, food: new List<Food> { new Food(6, 5), new Food(7, 5) });
var status = board.Tick();
Assert.IsTrue(status);
status = board.Tick();
Assert.IsTrue(status);
var score = board.Tick();
Assert.AreEqual(score, 0);
score = board.Tick();
Assert.AreEqual(score, 0);
board.KeyPress(Snake.Direction.Left);
status = board.Tick();
Assert.IsFalse(status);
score = board.Tick();
Assert.AreNotEqual(score, 0);
}
[TestMethod()]
public void SnakeFoodTest()
{
var board = PrepareBoard(Snake.Direction.Right, food: new List<Food> { new Food(6, 5), new Food(7, 5) });
var status = board.Tick();
Assert.IsTrue(status);
status = board.Tick();
Assert.IsTrue(status);
var score = board.Tick();
Assert.AreEqual(score, 0);
score = board.Tick();
Assert.AreEqual(score, 0);

var tiles = board.DumpBoard();
var snakeCounter = 0;
Expand All @@ -70,13 +70,100 @@ public void SnakeFoodTest()
Assert.AreEqual(foodCounter, 2);
}

[TestMethod()]
public void Board_TickTest()
{
var board = PrepareBoard(Snake.Direction.Right, 17, 5, new List<Food> { new Food(19,5) });

//Po prvním kroku bude existovat pouze had a jídlo, hra pokračuje
var score = board.Tick();
Assert.AreEqual(score, 0);
var tiles = board.DumpBoard();
Assert.IsInstanceOfType(tiles[0], typeof(Tile));
Assert.IsInstanceOfType(tiles[1], typeof(Food));

//Po druhém roku bude had najedený, existují tedy dvě políčka hada, jídlo a hra pokračuje
score = board.Tick();
Assert.AreEqual(score, 0);
tiles = board.DumpBoard();
var qe = tiles[0].Equals(new Tile(18, 5, Tile.TileType.Snake));
var rov = tiles[0] == new Tile(18, 5, Tile.TileType.Snake);
Assert.IsInstanceOfType(tiles[0], typeof(Tile));
Assert.IsInstanceOfType(tiles[1], typeof(Tile));
Assert.IsInstanceOfType(tiles[2], typeof(Food));

//Po třetím kroku had narazí do zdi. Stav je tedy stejný jako minule, ale hra nepokračuje
score = board.Tick();
Assert.AreEqual(score, 2);
var tilesAfterCollision = board.DumpBoard();
Assert.IsInstanceOfType(tilesAfterCollision[0], typeof(Tile));
Assert.IsInstanceOfType(tilesAfterCollision[1], typeof(Tile));
Assert.IsInstanceOfType(tilesAfterCollision[2], typeof(Food));
}

[TestMethod()]
public void Board_KeyPressTest()
{
var snake = new Snake(3, 3);

//Změna směru vpravo
snake.ChangeDirection(Snake.Direction.Right);
var nextTile = snake.NextMove();
Assert.AreEqual(4, nextTile.x);
Assert.AreEqual(3, nextTile.y);

//Změna směru dolů
snake.ChangeDirection(Snake.Direction.Down);
nextTile = snake.NextMove();
Assert.AreEqual(3, nextTile.x);
Assert.AreEqual(4, nextTile.y);

//Změna směru vlevo
snake.ChangeDirection(Snake.Direction.Left);
nextTile = snake.NextMove();
Assert.AreEqual(2, nextTile.x);
Assert.AreEqual(3, nextTile.y);

//Změna směru nahoru
snake.ChangeDirection(Snake.Direction.Up);
nextTile = snake.NextMove();
Assert.AreEqual(3, nextTile.x);
Assert.AreEqual(2, nextTile.y);
}

[TestMethod()]
public void Board_InitBoard_Test()
{
var board = new Board(20, 20);
board.InitBoard();

//Hra běží
Assert.IsTrue(board.Playing);

var Tiles = board.DumpBoard();
var snakeCounter = 0;
var foodCounter = 0;
foreach(Tile t in Tiles)
{
if(t.type == Tile.TileType.Food)
foodCounter++;
else if(t.type == Tile.TileType.Snake)
snakeCounter++;
}
//Na herním poli se nachází had
Assert.AreNotEqual(snakeCounter, 0);

//Na herním poli se nachází jídlo
Assert.AreNotEqual(foodCounter, 0);
}

#region Private methods
private void TestSnakeMovement(bool isPlaying, List<Tile> tiles, int snakeX, int snakeY, bool shouldBePlaying)
private void TestSnakeMovement(int score, List<Tile> tiles, int snakeX, int snakeY, bool shouldBePlaying)
{
if(shouldBePlaying)
Assert.IsTrue(isPlaying);
if (shouldBePlaying)
Assert.AreEqual(score, 0);
else
Assert.IsFalse(isPlaying);
Assert.AreNotEqual(score, 0);
CoordinatesAreEqual(tiles.First(), snakeX, snakeY);
}

Expand Down

0 comments on commit 2db960c

Please sign in to comment.