From 713cc937218fd1f0e4aacaea601dd1a8121a216e Mon Sep 17 00:00:00 2001 From: Jerry Jiang Date: Tue, 31 Mar 2020 23:10:47 -0700 Subject: [PATCH] Add ability for a debug start. --- .../com/tpcstld/twozerogame/DebugTools.java | 56 +++++++++++++++++++ .../com/tpcstld/twozerogame/MainGame.java | 11 +++- 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 2048/base/src/main/java/com/tpcstld/twozerogame/DebugTools.java diff --git a/2048/base/src/main/java/com/tpcstld/twozerogame/DebugTools.java b/2048/base/src/main/java/com/tpcstld/twozerogame/DebugTools.java new file mode 100644 index 0000000..1f7a594 --- /dev/null +++ b/2048/base/src/main/java/com/tpcstld/twozerogame/DebugTools.java @@ -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 generatePremadeMap() { + if (!DEBUG_ENABLED) { + return null; + } + + if (PREMADE_MAP == null) { + return null; + } + + List 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; + } +} diff --git a/2048/base/src/main/java/com/tpcstld/twozerogame/MainGame.java b/2048/base/src/main/java/com/tpcstld/twozerogame/MainGame.java index 32ac437..949e52d 100644 --- a/2048/base/src/main/java/com/tpcstld/twozerogame/MainGame.java +++ b/2048/base/src/main/java/com/tpcstld/twozerogame/MainGame.java @@ -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; @@ -70,7 +71,7 @@ void newGame() { highScore = score; recordHighScore(); } - score = 0; + score = DebugTools.getStartingScore(); gameState = GAME_NORMAL; addStartTiles(); mView.showHelp = firstRun(); @@ -80,6 +81,14 @@ void newGame() { } private void addStartTiles() { + List 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();