Skip to content

Commit

Permalink
feat(lblock): implement double rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablito2020 committed Apr 16, 2022
1 parent da9dd32 commit 8cc4ea3
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/main/kotlin/blocks/implementation/LBlock.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,34 @@ import movements.Position

class LBlock(private var initialPosition: Position): Block {

var positions = listOf(Position(0, 0), Position(1, 0), Position(2, 0), Position(2,1))
val defaultPositions = listOf(Position(0, 2), Position(1, 0), Position(1, 1), Position(1,2))
val rightPositions = listOf(Position(0, 1), Position(1, 1), Position(2, 1), Position(2,2))
val leftPositions = listOf(Position(0, 0), Position(0, 1), Position(1, 1), Position(2,1))
val middlePosition = listOf(Position(1, 0), Position(1, 1), Position(1,2), Position(2, 0))
var currentPosition = defaultPositions

override fun getNeededPositions(): Collection<Position> {
return positions.map({pos -> pos.addAxes(initialPosition)})
return currentPosition.map({pos -> pos.addAxes(initialPosition)})
}

override fun rotate(degree: Rotation) {
positions = when(degree) {
Rotation.RIGHT_90_DEGREE -> rightPositions
Rotation.LEFT_90_DEGREE -> leftPositions
currentPosition = when(degree) {
Rotation.RIGHT_90_DEGREE -> {
if (currentPosition == leftPositions)
defaultPositions
else if (currentPosition == rightPositions)
middlePosition
else
rightPositions
}
Rotation.LEFT_90_DEGREE -> {
if (currentPosition == rightPositions)
defaultPositions
else if (currentPosition == leftPositions)
middlePosition
else
leftPositions
}
}
}

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

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

class LBlockDoubleRotationTest : DoubleRotation {

private lateinit var lBlock: Block

@BeforeEach
fun setUp() {
lBlock = LBlock(Position(0, 0))
}

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

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

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

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

}

0 comments on commit 8cc4ea3

Please sign in to comment.