Skip to content

Commit

Permalink
[refactor] rewrites game logic.
Browse files Browse the repository at this point in the history
prepares separation of logic and ui.
  • Loading branch information
coderPaddyS committed Nov 8, 2024
1 parent 4008d73 commit c16da67
Show file tree
Hide file tree
Showing 7 changed files with 452 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import org.secuso.privacyfriendly2048.R;
import org.secuso.privacyfriendly2048.activities.helper.BaseActivityWithoutNavBar;
import org.secuso.privacyfriendly2048.activities.helper.GameState;
import org.secuso.privacyfriendly2048.activities.helper.GameStatistics;
import org.secuso.privacyfriendly2048.model.GameStatistics;
import org.secuso.privacyfriendly2048.activities.helper.Gestures;

import java.io.File;
Expand Down Expand Up @@ -328,7 +328,7 @@ public void initialize() {

}
gameStatistics = readStatisticsFromFile();
record = gameStatistics.getRecord();
record = gameStatistics.record;
last_points = gameState.last_points;
createNewGame = false;
DisplayMetrics metrics = new DisplayMetrics();
Expand Down Expand Up @@ -991,7 +991,7 @@ public void addNumber() {

if (points > record) {
record = points;
gameStatistics.setRecord(record);
gameStatistics.record = record;
textFieldRecord.setText("" + record);
}
if (moved) {
Expand Down Expand Up @@ -1040,7 +1040,7 @@ record = points;
}

public void gameOver() {
Log.i("record", "" + record + ", " + gameStatistics.getRecord());
Log.i("record", "" + record + ", " + gameStatistics.record);
saveStatisticsToFile(gameStatistics);
new AlertDialog.Builder(this)
.setTitle((this.getResources().getString(R.string.Titel_L_Message, points)))
Expand Down Expand Up @@ -1184,7 +1184,7 @@ public GameStatistics readStatisticsFromFile() {

public void saveStatisticsToFile(GameStatistics gS) {
try {
File file = new File(getFilesDir(), gS.getFilename());
File file = new File(getFilesDir(), gS.filename);
FileOutputStream fileOut = new FileOutputStream(file);
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(gS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

import org.secuso.privacyfriendly2048.R;
import org.secuso.privacyfriendly2048.activities.helper.BaseActivity;
import org.secuso.privacyfriendly2048.activities.helper.GameStatistics;
import org.secuso.privacyfriendly2048.model.GameStatistics;

import java.io.File;
import java.io.FileInputStream;
Expand Down Expand Up @@ -273,7 +273,7 @@ public Object instantiateItem(ViewGroup container, int position) {
tpm.setText("" + formatSmallMillis(gameStatistics.getTimePlayed() / gameStatistics.getMoves()));
else
tpm.setText("0");
rekord.setText("" + gameStatistics.getRecord());
rekord.setText("" + gameStatistics.record);


return view;
Expand Down

This file was deleted.

56 changes: 56 additions & 0 deletions app/src/main/java/org/secuso/privacyfriendly2048/model/Game2048.kt
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))
}
}
Loading

0 comments on commit c16da67

Please sign in to comment.