Skip to content

Commit

Permalink
feat(block-creator): add simple and all block queue creator
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablito2020 committed May 5, 2022
1 parent e5f17fd commit fe7068e
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 19 deletions.
27 changes: 27 additions & 0 deletions src/main/kotlin/block_factory/AllBlockQueueCreator.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package block_factory

import blocks.implementation.*
import movements.Position
import java.util.*

class BlockQueueCreator : TemplateQueueCreator(){

internal val allBlockQueue: Queue<BlockWrapper> = LinkedList()

init {
allBlockQueue.addAll(
listOf(
BlockWrapper(IBlock(Position(INITIAL_ROW_BLOCK, INITIAL_COLUMN_BLOCK)), BlockType.I_BLOCK),
BlockWrapper(JBlock(Position(INITIAL_ROW_BLOCK, INITIAL_COLUMN_BLOCK)), BlockType.J_BLOCK),
BlockWrapper(SBlock(Position(INITIAL_ROW_BLOCK, INITIAL_COLUMN_BLOCK)), BlockType.S_BLOCK),
BlockWrapper(ZBlock(Position(INITIAL_ROW_BLOCK, INITIAL_COLUMN_BLOCK)), BlockType.Z_BLOCK),
BlockWrapper(LBlock(Position(INITIAL_ROW_BLOCK, INITIAL_COLUMN_BLOCK)), BlockType.L_BLOCK),
BlockWrapper(SquareBlock(Position(INITIAL_ROW_BLOCK, INITIAL_COLUMN_BLOCK)), BlockType.SQUARE_BLOCK),
BlockWrapper(TBlock(Position(INITIAL_ROW_BLOCK, INITIAL_COLUMN_BLOCK)), BlockType.T_BLOCK)
)
)
}

override fun getQueue(): Queue<BlockWrapper> = allBlockQueue

}
21 changes: 2 additions & 19 deletions src/main/kotlin/block_factory/RandomBlockCreator.kt
Original file line number Diff line number Diff line change
@@ -1,28 +1,11 @@
package block_factory

import blocks.Block
import blocks.implementation.*
import game.normal.GAME_COLUMNS
import movements.Position

data class BlockWrapper(val block: Block, val type: BlockType)
class RandomBlockCreator : TemplateBlockCreator() {

internal const val INITIAL_ROW_BLOCK = 0
internal const val INITIAL_COLUMN_BLOCK = (GAME_COLUMNS / 2) - 2

class RandomBlockCreator : BlockCreator {

private var block: BlockWrapper = getNewBlock()

override fun getBlock(): Block {
val result = block.block
block = getNewBlock()
return result
}

override fun getNextBlockType() = block.type

private fun getNewBlock() = when (val type = BlockType.values().random()) {
override fun getNewBlock() = when (val type = BlockType.values().random()) {
BlockType.I_BLOCK -> BlockWrapper(IBlock(Position(INITIAL_ROW_BLOCK, INITIAL_COLUMN_BLOCK)), type)
BlockType.J_BLOCK -> BlockWrapper(JBlock(Position(INITIAL_ROW_BLOCK, INITIAL_COLUMN_BLOCK)), type)
BlockType.S_BLOCK -> BlockWrapper(SBlock(Position(INITIAL_ROW_BLOCK, INITIAL_COLUMN_BLOCK)), type)
Expand Down
24 changes: 24 additions & 0 deletions src/main/kotlin/block_factory/SimpleBlockQueueCreator.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package block_factory

import blocks.implementation.*
import movements.Position
import java.util.*

class SimpleBlockQueueCreator: TemplateQueueCreator() {

internal val allBlockQueue: Queue<BlockWrapper> = LinkedList()

init {
allBlockQueue.addAll(
listOf(
BlockWrapper(IBlock(Position(INITIAL_ROW_BLOCK, INITIAL_COLUMN_BLOCK)), BlockType.I_BLOCK),
BlockWrapper(JBlock(Position(INITIAL_ROW_BLOCK, INITIAL_COLUMN_BLOCK)), BlockType.J_BLOCK),
BlockWrapper(LBlock(Position(INITIAL_ROW_BLOCK, INITIAL_COLUMN_BLOCK)), BlockType.L_BLOCK),
BlockWrapper(SquareBlock(Position(INITIAL_ROW_BLOCK, INITIAL_COLUMN_BLOCK)), BlockType.SQUARE_BLOCK),
BlockWrapper(TBlock(Position(INITIAL_ROW_BLOCK, INITIAL_COLUMN_BLOCK)), BlockType.T_BLOCK)
)
)
}

override fun getQueue(): Queue<BlockWrapper> = allBlockQueue
}
25 changes: 25 additions & 0 deletions src/main/kotlin/block_factory/TemplateBlockCreator.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package block_factory

import blocks.Block
import game.normal.GAME_COLUMNS

internal data class BlockWrapper(val block: Block, val type: BlockType)

internal const val INITIAL_ROW_BLOCK = 0
internal const val INITIAL_COLUMN_BLOCK = (GAME_COLUMNS / 2) - 2

abstract class TemplateBlockCreator : BlockCreator {

private var block: BlockWrapper = getNewBlock()

override fun getBlock(): Block {
val result = block.block
block = getNewBlock()
return result
}

override fun getNextBlockType() = block.type

internal abstract fun getNewBlock(): BlockWrapper

}
16 changes: 16 additions & 0 deletions src/main/kotlin/block_factory/TemplateQueueCreator.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package block_factory

import java.util.*

abstract class TemplateQueueCreator: TemplateBlockCreator() {

override fun getNewBlock(): BlockWrapper {
val queue = getQueue()
val element = queue.poll()
queue.add(element.copy())
return element
}

internal abstract fun getQueue(): Queue<BlockWrapper>

}

0 comments on commit fe7068e

Please sign in to comment.