Skip to content

Commit

Permalink
feat(game): add move down next check
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablito2020 committed Apr 17, 2022
1 parent 48dea91 commit a1244be
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/main/kotlin/game/Game.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import board.Board
import board.Cell
import game.exceptions.EmptyCurrentBlockException
import movements.Direction
import movements.Position
import score.ScoreCalculator

internal const val GAME_COLUMNS = 10
Expand Down Expand Up @@ -46,5 +47,13 @@ class Game(private val creator: BlockCreator, val scoreCalculator: ScoreCalculat
block!!.move(direction)
}

fun blockCanMoveDownNext(): Boolean {
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))) }
block!!.move(Direction.UP)
return canMoveDownMore
}

}
3 changes: 2 additions & 1 deletion src/main/kotlin/movements/Direction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ package movements
enum class Direction {
LEFT,
RIGHT,
DOWN
DOWN,
UP
}
1 change: 1 addition & 0 deletions src/main/kotlin/movements/Position.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ data class Position(val row: Int, val column: Int) {
Direction.DOWN -> Position(row + 1, column)
Direction.LEFT -> Position(row, column - 1)
Direction.RIGHT -> Position(row, column + 1)
Direction.UP -> Position(row - 1, column)
}
}

Expand Down
47 changes: 47 additions & 0 deletions src/test/kotlin/game/GameMoveDownNextTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package game

import block_factory.BlockCreator
import blocks.implementation.IBlock
import movements.Direction
import movements.Position
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import score.Points
import score.ScoreCalculator
import kotlin.test.assertTrue

class GameMoveDownNextTest {

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)
})
game.getNextBlock()
}

@Test
fun `first move always can move down`() {
assertTrue(game.blockCanMoveDownNext())
}

@Test
fun `down the board less 1 can move down`() {
for (i in 1 until GAME_ROWS - 2)
game.moveBlock(Direction.DOWN)
assertTrue(game.blockCanMoveDownNext())
}

@Test
fun `down the board can't move more`() {
for (i in 1 until GAME_ROWS - 1)
game.moveBlock(Direction.DOWN)
assertFalse(game.blockCanMoveDownNext())
}

}

0 comments on commit a1244be

Please sign in to comment.