Skip to content

Commit

Permalink
Add ability for a debug start.
Browse files Browse the repository at this point in the history
  • Loading branch information
tpcstld committed Apr 1, 2020
1 parent ece209b commit 713cc93
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
56 changes: 56 additions & 0 deletions 2048/base/src/main/java/com/tpcstld/twozerogame/DebugTools.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.tpcstld.twozerogame;

import android.support.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

/**
* Tools and hooks for debugging.
*
* Note to self: when making a custom build, set the versionCode in
* installed/build.gradle to 1.
*/
class DebugTools {
private static final boolean DEBUG_ENABLED = false;

private static final int[][] PREMADE_MAP = {
{128, 256, 32768, 131072},
{8, 16, 0, 2},
{0, 0, 0, 2},
{0, 0, 0, 0},
};
private static final long STARTING_SCORE = 2529244L;

@Nullable
static List<Tile> generatePremadeMap() {
if (!DEBUG_ENABLED) {
return null;
}

if (PREMADE_MAP == null) {
return null;
}

List<Tile> result = new ArrayList<>();
for (int yy = 0; yy < PREMADE_MAP.length; yy++) {
for (int xx = 0; xx < PREMADE_MAP[0].length; xx++) {
if (PREMADE_MAP[yy][xx] == 0) {
continue;
}

result.add(new Tile(xx, yy, PREMADE_MAP[yy][xx]));
}
}

return result;
}

static long getStartingScore() {
if (!DEBUG_ENABLED) {
return 0;
}

return STARTING_SCORE;
}
}
11 changes: 10 additions & 1 deletion 2048/base/src/main/java/com/tpcstld/twozerogame/MainGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Debug;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;

Expand Down Expand Up @@ -70,7 +71,7 @@ void newGame() {
highScore = score;
recordHighScore();
}
score = 0;
score = DebugTools.getStartingScore();
gameState = GAME_NORMAL;
addStartTiles();
mView.showHelp = firstRun();
Expand All @@ -80,6 +81,14 @@ void newGame() {
}

private void addStartTiles() {
List<Tile> debugTiles = DebugTools.generatePremadeMap();
if (debugTiles != null) {
for (Tile tile : debugTiles) {
this.spawnTile(tile);
}
return;
}

int startTiles = 2;
for (int xx = 0; xx < startTiles; xx++) {
this.addRandomTile();
Expand Down

0 comments on commit 713cc93

Please sign in to comment.