Skip to content

Commit efa4593

Browse files
committed
Fixed bugs
New Game button always lighting up Undo button wrongly undoing score
1 parent 75be952 commit efa4593

File tree

3 files changed

+23
-22
lines changed

3 files changed

+23
-22
lines changed

2048/2048/src/main/java/com/tpcstld/twozerogame/Grid.java

+7-9
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,13 @@ public void saveTiles() {
100100
}
101101

102102
public void revertTiles() {
103-
if (canUndo) {
104-
canUndo = false;
105-
for (int xx = 0; xx < undoField.length; xx++) {
106-
for (int yy = 0; yy < undoField[0].length; yy++) {
107-
if (undoField[xx][yy] == null) {
108-
field[xx][yy] = null;
109-
} else {
110-
field[xx][yy] = new Tile(xx, yy, undoField[xx][yy].getValue());
111-
}
103+
canUndo = false;
104+
for (int xx = 0; xx < undoField.length; xx++) {
105+
for (int yy = 0; yy < undoField[0].length; yy++) {
106+
if (undoField[xx][yy] == null) {
107+
field[xx][yy] = null;
108+
} else {
109+
field[xx][yy] = new Tile(xx, yy, undoField[xx][yy].getValue());
112110
}
113111
}
114112
}

2048/2048/src/main/java/com/tpcstld/twozerogame/MainGame.java

+12-9
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,13 @@ public void newGame() {
5252
recordHighScore();
5353
}
5454
score = 0;
55+
undoScore = 0;
5556
won = false;
5657
lose = false;
5758
addStartTiles();
5859
mView.refreshLastTime = true;
5960
mView.resyncTime();
60-
mView.postInvalidate();
61+
mView.invalidate();
6162
}
6263

6364
public void addStartTiles() {
@@ -115,13 +116,15 @@ public void saveUndoState() {
115116
}
116117

117118
public void revertUndoState() {
118-
aGrid.cancelAnimations();
119-
grid.revertTiles();
120-
score = undoScore;
121-
won = false;
122-
lose = false;
123-
mView.refreshLastTime = true;
124-
mView.invalidate();
119+
if (grid.canUndo) {
120+
aGrid.cancelAnimations();
121+
grid.revertTiles();
122+
score = undoScore;
123+
won = false;
124+
lose = false;
125+
mView.refreshLastTime = true;
126+
mView.invalidate();
127+
}
125128
}
126129

127130
public void move (int direction) {
@@ -191,7 +194,7 @@ public void move (int direction) {
191194
checkLose();
192195
}
193196
mView.resyncTime();
194-
mView.postInvalidate();
197+
mView.invalidate();
195198
}
196199

197200
public void checkLose() {

2048/2048/src/main/java/com/tpcstld/twozerogame/MainView.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public void onDraw(Canvas canvas) {
8686
drawScoreText(canvas);
8787

8888
if ((game.won || game.lose) && !game.aGrid.isAnimationActive()) {
89-
drawNewGameButton(canvas);
89+
drawNewGameButton(canvas, true);
9090
}
9191

9292
drawCells(canvas);
@@ -169,8 +169,8 @@ public void drawScoreText(Canvas canvas) {
169169
canvas.drawText("" + game.score, sXScore + textMiddleScore, bodyStartYAll, paint);
170170
}
171171

172-
public void drawNewGameButton(Canvas canvas) {
173-
if ((game.won || game.lose)) {
172+
public void drawNewGameButton(Canvas canvas, boolean lightUp) {
173+
if (lightUp) {
174174
drawDrawable(canvas, lightUpRectangle, sXNewGame, sYIcons, sXNewGame + iconSize, sYIcons + iconSize);
175175
} else {
176176
drawDrawable(canvas, backgroundRectangle, sXNewGame, sYIcons, sXNewGame + iconSize, sYIcons + iconSize);
@@ -334,7 +334,7 @@ public void createBackgroundBitmap(int width, int height) {
334334
background = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
335335
Canvas canvas = new Canvas(background);
336336
drawHeader(canvas);
337-
drawNewGameButton(canvas);
337+
drawNewGameButton(canvas, false);
338338
drawUndoButton(canvas);
339339
drawBackground(canvas);
340340
drawBackgroundGrid(canvas);

0 commit comments

Comments
 (0)