-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
452 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 0 additions & 140 deletions
140
app/src/main/java/org/secuso/privacyfriendly2048/activities/helper/GameStatistics.java
This file was deleted.
Oops, something went wrong.
56 changes: 56 additions & 0 deletions
56
app/src/main/java/org/secuso/privacyfriendly2048/model/Game2048.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package org.secuso.privacyfriendly2048.model | ||
|
||
typealias Board = Array<Array<Int>> | ||
|
||
class Game2048(val config: GameConfig, boardState: Array<Array<Int>>? = null) { | ||
data class SingleGameState(val data: Array<Array<Int>>, val points: Int) | ||
|
||
data class GameConfig( | ||
val boardSize: Int, | ||
val cellsToFill: Int, | ||
val distribution: GameBoard.SpawnProbabilityDistribution, | ||
val wonOnValue: Int | ||
) | ||
|
||
enum class GameState { | ||
RUNNING, | ||
LOST, | ||
WON; | ||
} | ||
|
||
private var board = GameBoard(config.boardSize, boardState) | ||
private var points = 0 | ||
private val boardHistory: MutableList<SingleGameState> = mutableListOf() | ||
private var state = determineGameState() | ||
|
||
init { | ||
board.onMergeListener = GameBoard.OnMergeListener { _, _, mergedValue -> points += mergedValue } | ||
} | ||
|
||
private fun determineGameState(): GameState { | ||
return if (board.data.flatten().contains(config.wonOnValue)) { | ||
GameState.WON | ||
} else if (board.freeCells.isEmpty() && !board.anyMergePossible()) { | ||
GameState.LOST | ||
} else { | ||
GameState.RUNNING | ||
} | ||
} | ||
|
||
fun undo() { | ||
boardHistory.lastOrNull()?.let { (board, points) -> | ||
this.board.replaceBoard(board) | ||
this.points = points | ||
boardHistory.removeAt(boardHistory.lastIndex) | ||
} | ||
} | ||
|
||
fun move(direction: GameBoard.Direction) { | ||
if (state != GameState.RUNNING) { | ||
return | ||
} | ||
board.move(direction) | ||
board.fillRandomCell(config.cellsToFill, config.distribution) | ||
boardHistory.add(SingleGameState(board.data, points)) | ||
} | ||
} |
Oops, something went wrong.