Skip to content

Commit

Permalink
feat(jblock): implement double rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablito2020 committed Apr 16, 2022
1 parent 27431df commit 082597c
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/main/kotlin/blocks/implementation/JBlock.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,31 @@ class JBlock(private var initialPosition: Position) : Block {
private val initialState = listOf(Position(0, 0), Position(1, 0), Position(1, 1), Position(1, 2))
private val rightState = listOf(Position(0, 1), Position(0, 2), Position(1, 1), Position(2, 1))
private val leftState = listOf(Position(0, 1), Position(1, 1), Position(2, 0), Position(2, 1))
private val middleState = listOf(Position(1, 0), Position(1, 1), Position(1, 2), Position(2, 2))
private var state = initialState

override fun getNeededPositions(): Collection<Position> {
return state.map { position -> position.addAxes(initialPosition) }
}

override fun rotate(degree: Rotation) {
state = when(degree) {
Rotation.RIGHT_90_DEGREE -> rightState
Rotation.LEFT_90_DEGREE -> leftState
state = when (degree) {
Rotation.RIGHT_90_DEGREE -> {
if (state == leftState)
initialState
else if (state == rightState)
middleState
else
rightState
}
Rotation.LEFT_90_DEGREE -> {
if (state == rightState)
initialState
else if (state == leftState)
middleState
else
leftState
}
}
}

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

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

class JBlockDoubleRotationTest : DoubleRotation {

private lateinit var jBlock: JBlock

@BeforeEach
fun setUp() {
jBlock = JBlock(Position(0, 0))
}

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

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

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

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

}

0 comments on commit 082597c

Please sign in to comment.