Skip to content

Commit

Permalink
feat(game): add write block
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablito2020 committed Apr 17, 2022
1 parent a1244be commit 46b4c93
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/main/kotlin/game/Game.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
)
)
)
}
}

}
3 changes: 3 additions & 0 deletions src/main/kotlin/game/exceptions/BlockCanMoveDownException.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package game.exceptions

class BlockCanMoveDownException(message: String): Throwable(message)
66 changes: 66 additions & 0 deletions src/test/kotlin/game/GameWriteBlockTest.kt
Original file line number Diff line number Diff line change
@@ -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())
}

}

0 comments on commit 46b4c93

Please sign in to comment.