diff --git a/src/main/kotlin/game/Game.kt b/src/main/kotlin/game/Game.kt index 3a758ef..1ff0124 100644 --- a/src/main/kotlin/game/Game.kt +++ b/src/main/kotlin/game/Game.kt @@ -4,6 +4,7 @@ import block_factory.BlockCreator import blocks.Block import board.Board import board.Cell +import game.exceptions.BlockCanMoveDownException import game.exceptions.EmptyCurrentBlockException import movements.Direction import movements.Position @@ -51,9 +52,34 @@ class Game(private val creator: BlockCreator, val scoreCalculator: ScoreCalculat if (block == null) throw EmptyCurrentBlockException("Did you initialize a block with getNextBlock()?") block!!.move(Direction.DOWN) - val canMoveDownMore = block!!.getNeededPositions().all { board.isInside(it.addAxes(Position(GAME_CELL_BUFFER, 0))) && board.isEmpty(it.addAxes(Position(GAME_CELL_BUFFER, 0))) } + val canMoveDownMore = block!!.getNeededPositions().all { + board.isInside(it.addAxes(Position(GAME_CELL_BUFFER, 0))) && board.isEmpty( + it.addAxes( + Position( + GAME_CELL_BUFFER, + 0 + ) + ) + ) + } block!!.move(Direction.UP) return canMoveDownMore } + fun writeBlockToBoard() { + if (block == null) + throw EmptyCurrentBlockException("Did you initialize a block with getNextBlock()?") + if (blockCanMoveDownNext()) + throw BlockCanMoveDownException("Block can move down next, so it's impossible to write it to the board") + block!!.getNeededPositions().forEach { + board.writePosition( + block!!.getCell(), it.addAxes( + Position( + GAME_CELL_BUFFER, 0 + ) + ) + ) + } + } + } \ No newline at end of file diff --git a/src/main/kotlin/game/exceptions/BlockCanMoveDownException.kt b/src/main/kotlin/game/exceptions/BlockCanMoveDownException.kt new file mode 100644 index 0000000..db50658 --- /dev/null +++ b/src/main/kotlin/game/exceptions/BlockCanMoveDownException.kt @@ -0,0 +1,3 @@ +package game.exceptions + +class BlockCanMoveDownException(message: String): Throwable(message) \ No newline at end of file diff --git a/src/test/kotlin/game/GameWriteBlockTest.kt b/src/test/kotlin/game/GameWriteBlockTest.kt new file mode 100644 index 0000000..f926877 --- /dev/null +++ b/src/test/kotlin/game/GameWriteBlockTest.kt @@ -0,0 +1,66 @@ +package game + +import block_factory.BlockCreator +import blocks.implementation.IBlock +import board.Cell +import game.exceptions.BlockCanMoveDownException +import game.exceptions.EmptyCurrentBlockException +import movements.Direction +import movements.Position +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import score.Points +import score.ScoreCalculator + +class GameWriteBlockTest { + + private lateinit var game: Game + + @BeforeEach + fun setUp() { + game = Game(object : BlockCreator { + override fun getBlock() = IBlock(Position(0, (GAME_COLUMNS / 2) - 2)) + }, object : ScoreCalculator { + override fun getScore(cleanedRows: Int) = Points(0) + }) + } + + @Test + fun `non initialized block should not be written`() { + Assertions.assertThrows(EmptyCurrentBlockException::class.java) { + game.writeBlockToBoard() + } + } + + @Test + fun `new block that can move next should not be written`() { + game.getNextBlock() + Assertions.assertThrows(BlockCanMoveDownException::class.java) { + game.writeBlockToBoard() + } + } + + @Test + fun `block that can be written and it writes appears as a new cell`() { + // Create first block, move it down, write it, and then get a new one + game.getNextBlock() + for( i in 1 until GAME_ROWS - 1) + game.moveBlock(Direction.DOWN) + game.writeBlockToBoard() + game.getNextBlock() + // Expected: block saved at the bottom, and a new (current) block at the top + val expectedGrid = MutableList(GAME_ROWS) { MutableList(GAME_COLUMNS) { GameCell(Cell.EMPTY, false) } } + expectedGrid[GAME_ROWS - 1][(GAME_COLUMNS / 2) - 2] = GameCell(Cell.I_BLOCK, false) + expectedGrid[GAME_ROWS - 1][(GAME_COLUMNS / 2) - 1] = GameCell(Cell.I_BLOCK, false) + expectedGrid[GAME_ROWS - 1][(GAME_COLUMNS / 2)] = GameCell(Cell.I_BLOCK, false) + expectedGrid[GAME_ROWS - 1][(GAME_COLUMNS / 2) + 1] = GameCell(Cell.I_BLOCK, false) + expectedGrid[1][(GAME_COLUMNS / 2) - 2] = GameCell(Cell.I_BLOCK, true) + expectedGrid[1][(GAME_COLUMNS / 2) - 1] = GameCell(Cell.I_BLOCK, true) + expectedGrid[1][(GAME_COLUMNS / 2)] = GameCell(Cell.I_BLOCK, true) + expectedGrid[1][(GAME_COLUMNS / 2) + 1] = GameCell(Cell.I_BLOCK, true) + assertEquals(expectedGrid, game.getGrid()) + } + +} \ No newline at end of file