Refactoring
This commit is contained in:
+37
-37
@@ -5,20 +5,20 @@ import sdl.*
|
|||||||
typealias Field = Array<ByteArray>
|
typealias Field = Array<ByteArray>
|
||||||
|
|
||||||
enum class Move {
|
enum class Move {
|
||||||
Left,
|
LEFT,
|
||||||
Right,
|
RIGHT,
|
||||||
Down,
|
DOWN,
|
||||||
Rotate
|
ROTATE
|
||||||
}
|
}
|
||||||
|
|
||||||
enum class PlacementResult(val linesCleared: Int = 0, val bonus: Int = 0) {
|
enum class PlacementResult(val linesCleared: Int = 0, val bonus: Int = 0) {
|
||||||
Nothing,
|
NOTHING,
|
||||||
GameOver,
|
GAMEOVER,
|
||||||
// For values of bonuses see https://tetris.wiki/Scoring
|
// For values of bonuses see https://tetris.wiki/Scoring
|
||||||
Single(1, 40),
|
SINGLE(1, 40),
|
||||||
Double(2, 100),
|
DOUBLE(2, 100),
|
||||||
Triple(3, 300),
|
TRIPLE(3, 300),
|
||||||
Tetris(4, 1200)
|
TETRIS(4, 1200)
|
||||||
}
|
}
|
||||||
|
|
||||||
val EMPTY: Byte = 0
|
val EMPTY: Byte = 0
|
||||||
@@ -47,19 +47,19 @@ class PiecePosition(piece: Piece, private val origin: Point) {
|
|||||||
|
|
||||||
fun makeMove(move: Move) {
|
fun makeMove(move: Move) {
|
||||||
when (move) {
|
when (move) {
|
||||||
Move.Left -> --p.y
|
Move.LEFT -> --p.y
|
||||||
Move.Right -> ++p.y
|
Move.RIGHT -> ++p.y
|
||||||
Move.Down -> ++p.x
|
Move.DOWN -> ++p.x
|
||||||
Move.Rotate -> state = (state + 1) % numberOfStates
|
Move.ROTATE -> state = (state + 1) % numberOfStates
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun unMakeMove(move: Move) {
|
fun unMakeMove(move: Move) {
|
||||||
when (move) {
|
when (move) {
|
||||||
Move.Left -> ++p.y
|
Move.LEFT -> ++p.y
|
||||||
Move.Right -> --p.y
|
Move.RIGHT -> --p.y
|
||||||
Move.Down -> --p.x
|
Move.DOWN -> --p.x
|
||||||
Move.Rotate -> state = (state + numberOfStates - 1) % numberOfStates
|
Move.ROTATE -> state = (state + numberOfStates - 1) % numberOfStates
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -215,7 +215,7 @@ enum class UserCommand {
|
|||||||
LEFT,
|
LEFT,
|
||||||
RIGHT,
|
RIGHT,
|
||||||
DOWN,
|
DOWN,
|
||||||
RELEASE,
|
DROP,
|
||||||
ROTATE,
|
ROTATE,
|
||||||
EXIT
|
EXIT
|
||||||
}
|
}
|
||||||
@@ -296,16 +296,16 @@ class GameField(val width: Int, val height: Int, val visualizer: GameFieldVisual
|
|||||||
fun place(): PlacementResult {
|
fun place(): PlacementResult {
|
||||||
currentPiece.place(field, currentPosition)
|
currentPiece.place(field, currentPosition)
|
||||||
val linesCleared = clearLines()
|
val linesCleared = clearLines()
|
||||||
if (isOutOfBorders()) return PlacementResult.GameOver
|
if (isOutOfBorders()) return PlacementResult.GAMEOVER
|
||||||
switchCurrentPiece()
|
switchCurrentPiece()
|
||||||
if (!currentPiece.canBePlaced(field, currentPosition))
|
if (!currentPiece.canBePlaced(field, currentPosition))
|
||||||
return PlacementResult.GameOver
|
return PlacementResult.GAMEOVER
|
||||||
when (linesCleared) {
|
when (linesCleared) {
|
||||||
1 -> return PlacementResult.Single
|
1 -> return PlacementResult.SINGLE
|
||||||
2 -> return PlacementResult.Double
|
2 -> return PlacementResult.DOUBLE
|
||||||
3 -> return PlacementResult.Triple
|
3 -> return PlacementResult.TRIPLE
|
||||||
4 -> return PlacementResult.Tetris
|
4 -> return PlacementResult.TETRIS
|
||||||
else -> return PlacementResult.Nothing
|
else -> return PlacementResult.NOTHING
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -415,8 +415,8 @@ class Game(width: Int, height: Int, val visualizer: GameFieldVisualizer, val use
|
|||||||
ticks = 0
|
ticks = 0
|
||||||
println(placementResult.toString())
|
println(placementResult.toString())
|
||||||
when (placementResult) {
|
when (placementResult) {
|
||||||
PlacementResult.Nothing -> return
|
PlacementResult.NOTHING -> return
|
||||||
PlacementResult.GameOver -> {
|
PlacementResult.GAMEOVER -> {
|
||||||
gameOver = true
|
gameOver = true
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -424,7 +424,7 @@ class Game(width: Int, height: Int, val visualizer: GameFieldVisualizer, val use
|
|||||||
linesCleared += placementResult.linesCleared
|
linesCleared += placementResult.linesCleared
|
||||||
linesClearedAtCurrentLevel += placementResult.linesCleared
|
linesClearedAtCurrentLevel += placementResult.linesCleared
|
||||||
score += placementResult.bonus * (level + 1)
|
score += placementResult.bonus * (level + 1)
|
||||||
if (placementResult == PlacementResult.Tetris)
|
if (placementResult == PlacementResult.TETRIS)
|
||||||
++tetrises
|
++tetrises
|
||||||
val levelUpThreshold = levelUpThreshold
|
val levelUpThreshold = levelUpThreshold
|
||||||
if (linesClearedAtCurrentLevel >= levelUpThreshold) {
|
if (linesClearedAtCurrentLevel >= levelUpThreshold) {
|
||||||
@@ -456,15 +456,15 @@ class Game(width: Int, height: Int, val visualizer: GameFieldVisualizer, val use
|
|||||||
val success: Boolean
|
val success: Boolean
|
||||||
when (cmd) {
|
when (cmd) {
|
||||||
UserCommand.EXIT -> return
|
UserCommand.EXIT -> return
|
||||||
UserCommand.LEFT -> success = field.makeMove(Move.Left)
|
UserCommand.LEFT -> success = field.makeMove(Move.LEFT)
|
||||||
UserCommand.RIGHT -> success = field.makeMove(Move.Right)
|
UserCommand.RIGHT -> success = field.makeMove(Move.RIGHT)
|
||||||
UserCommand.ROTATE -> success = field.makeMove(Move.Rotate)
|
UserCommand.ROTATE -> success = field.makeMove(Move.ROTATE)
|
||||||
UserCommand.DOWN -> {
|
UserCommand.DOWN -> {
|
||||||
success = field.makeMove(Move.Down)
|
success = field.makeMove(Move.DOWN)
|
||||||
if (!success) placePiece()
|
if (!success) placePiece()
|
||||||
}
|
}
|
||||||
UserCommand.RELEASE -> {
|
UserCommand.DROP -> {
|
||||||
while (field.makeMove(Move.Down)) {
|
while (field.makeMove(Move.DOWN)) {
|
||||||
}
|
}
|
||||||
success = true
|
success = true
|
||||||
placePiece()
|
placePiece()
|
||||||
@@ -477,7 +477,7 @@ class Game(width: Int, height: Int, val visualizer: GameFieldVisualizer, val use
|
|||||||
}
|
}
|
||||||
++ticks
|
++ticks
|
||||||
if (ticks < speed) continue
|
if (ticks < speed) continue
|
||||||
if (!field.makeMove(Move.Down)) {
|
if (!field.makeMove(Move.DOWN)) {
|
||||||
if (++attemptsToLock >= LOCK_DELAY) {
|
if (++attemptsToLock >= LOCK_DELAY) {
|
||||||
placePiece()
|
placePiece()
|
||||||
attemptsToLock = 0
|
attemptsToLock = 0
|
||||||
@@ -785,7 +785,7 @@ class SDL_Visualizer(val width: Int, val height: Int): GameFieldVisualizer, User
|
|||||||
SDL_SCANCODE_RIGHT -> commands.add(UserCommand.RIGHT)
|
SDL_SCANCODE_RIGHT -> commands.add(UserCommand.RIGHT)
|
||||||
SDL_SCANCODE_DOWN -> commands.add(UserCommand.DOWN)
|
SDL_SCANCODE_DOWN -> commands.add(UserCommand.DOWN)
|
||||||
SDL_SCANCODE_Z -> commands.add(UserCommand.ROTATE)
|
SDL_SCANCODE_Z -> commands.add(UserCommand.ROTATE)
|
||||||
SDL_SCANCODE_UP -> commands.add(UserCommand.RELEASE)
|
SDL_SCANCODE_UP -> commands.add(UserCommand.DROP)
|
||||||
SDL_SCANCODE_ESCAPE -> commands.add(UserCommand.EXIT)
|
SDL_SCANCODE_ESCAPE -> commands.add(UserCommand.EXIT)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user