Skip to content

Commit

Permalink
feat(sblock): implement Double Rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablito2020 committed Apr 16, 2022
1 parent e370a69 commit 44e6797
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/main/kotlin/blocks/implementation/SBlock.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@ import movements.Position

class SBlock(private var position: Position) : Block {

private val positions = listOf(Position(0, 1), Position(0, 2), Position(1, 0), Position(1, 1))
private val rightDegreePositions = listOf(Position(0, 1), Position(1, 1), Position(1, 2), Position(2, 2))
private val leftDegreePositions = listOf(Position(0, 0), Position(1, 0), Position(1, 1), Position(2, 1))
private var currentPosition: List<Position> = positions
private val POSITIONS = listOf(
listOf(Position(0, 1), Position(0, 2), Position(1, 0), Position(1, 1)),
listOf(Position(0, 0), Position(1, 0), Position(1, 1), Position(2, 1)),
listOf(Position(1, 1), Position(1, 2), Position(2, 0), Position(2, 1)),
listOf(Position(0, 1), Position(1, 1), Position(1, 2), Position(2, 2))
)
private var positionIndex = 0

override fun getNeededPositions(): Collection<Position> = currentPosition.map { p ->
override fun getNeededPositions(): Collection<Position> = POSITIONS[positionIndex].map { p ->
Position(p.row + position.row, p.column + position.column)
}

override fun rotate(degree: Rotation) {
currentPosition = when(degree) {
Rotation.RIGHT_90_DEGREE -> rightDegreePositions
Rotation.LEFT_90_DEGREE -> leftDegreePositions
positionIndex = when (degree) {
Rotation.RIGHT_90_DEGREE -> if (positionIndex - 1 < 0) POSITIONS.size - 1 else positionIndex - 1
Rotation.LEFT_90_DEGREE -> (positionIndex + 1) % POSITIONS.size
}
}

Expand Down
61 changes: 61 additions & 0 deletions src/test/kotlin/blocks/SBlock/SBlockDoubleRotationTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package blocks.SBlock

import blocks.Block
import blocks.DoubleRotation
import blocks.Rotation
import blocks.implementation.SBlock
import movements.Position
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals

class SBlockDoubleRotationTest : DoubleRotation {

lateinit var sBlock: Block

@BeforeEach
fun setUp() {
sBlock = SBlock(Position(2, 2))
}

@Test
override fun `Rotate block 90 degrees to right and then to left stays on same position`() {
sBlock.rotate(Rotation.RIGHT_90_DEGREE)
sBlock.rotate(Rotation.LEFT_90_DEGREE)
assertEquals(
listOf(Position(2, 3), Position(2, 4), Position(3, 2), Position(3, 3)),
sBlock.getNeededPositions()
)
}

@Test
override fun `Rotate block 90 degrees to left and then to right stays on same position`() {
sBlock.rotate(Rotation.LEFT_90_DEGREE)
sBlock.rotate(Rotation.RIGHT_90_DEGREE)
assertEquals(
listOf(Position(2, 3), Position(2, 4), Position(3, 2), Position(3, 3)),
sBlock.getNeededPositions()
)
}

@Test
override fun `Rotate block 180 degrees to left`() {
sBlock.rotate(Rotation.LEFT_90_DEGREE)
sBlock.rotate(Rotation.LEFT_90_DEGREE)
assertEquals(
listOf(Position(3, 3), Position(3, 4), Position(4, 2), Position(4, 3)),
sBlock.getNeededPositions()
)
}

@Test
override fun `Rotate block 180 degrees to right`() {
sBlock.rotate(Rotation.RIGHT_90_DEGREE)
sBlock.rotate(Rotation.RIGHT_90_DEGREE)
assertEquals(
listOf(Position(3, 3), Position(3, 4), Position(4, 2), Position(4, 3)),
sBlock.getNeededPositions()
)
}

}

0 comments on commit 44e6797

Please sign in to comment.