Skip to content

Commit

Permalink
refactor(iblock): use delegation, no inheritance
Browse files Browse the repository at this point in the history
With this, we can encapsulate the BlockImplementation type.
  • Loading branch information
Pablito2020 committed Apr 16, 2022
1 parent 3277f60 commit 0b3b8d4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import blocks.Rotation
import movements.Direction
import movements.Position

abstract class BlockImplementation(private val FORMATS_OF_BLOCK: List<List<Position>>, private var initialPosition: Position):
Block {
internal class BlockLogic(
private val FORMATS_OF_BLOCK: List<List<Position>>,
private var initialPosition: Position
) : Block {

private var currentFormatIndex = 0

Expand Down
18 changes: 15 additions & 3 deletions src/main/kotlin/blocks/implementation/IBlock.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
package blocks.implementation

import blocks.Block
import blocks.Rotation
import movements.Direction
import movements.Position

class IBlock(initialPosition: Position) : BlockImplementation(TYPES, initialPosition)

private val TYPES = listOf(
internal val TYPES = listOf(
listOf(Position(1, 0), Position(1, 1), Position(1, 2), Position(1, 3)),
listOf(Position(0, 2), Position(1, 2), Position(2, 2), Position(3, 2)),
listOf(Position(2, 0), Position(2, 1), Position(2, 2), Position(2, 3)),
listOf(Position(0, 1), Position(1, 1), Position(2, 1), Position(3, 1)),
)

class IBlock(initialPosition: Position) : Block {

private val blockImplementation = BlockLogic(TYPES, initialPosition)

override fun getNeededPositions(): Collection<Position> = blockImplementation.getNeededPositions()

override fun rotate(degree: Rotation) = blockImplementation.rotate(degree)

override fun move(direction: Direction) = blockImplementation.move(direction)

}

0 comments on commit 0b3b8d4

Please sign in to comment.