Finished twentyfortyeight.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package twentyfortyeight;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.time.format.TextStyle;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
@@ -77,12 +78,15 @@ class Game extends World {
|
||||
}
|
||||
|
||||
public void onKeyEvent(String key) {
|
||||
if (key.equals("left") || key.equals("h")) this.grid.mv(0);
|
||||
else if (key.equals("down") || key.equals("j")) this.grid.mv(1);
|
||||
else if (key.equals("up") || key.equals("k")) this.grid.mv(2);
|
||||
else if (key.equals("right") || key.equals("l")) this.grid.mv(3);
|
||||
int mv;
|
||||
if (key.equals("left") || key.equals("h")) mv = 0;
|
||||
else if (key.equals("down") || key.equals("j")) mv = 1;
|
||||
else if (key.equals("up") || key.equals("k")) mv = 2;
|
||||
else if (key.equals("right") || key.equals("l")) mv = 3;
|
||||
else return; // Don't draw or add tiles if bad key pressed.
|
||||
|
||||
this.score += this.grid.mv(mv);
|
||||
|
||||
this.draw();
|
||||
|
||||
List<Integer> frees = this.grid.freeCellIdxs();
|
||||
@@ -93,7 +97,16 @@ class Game extends World {
|
||||
}
|
||||
|
||||
// Draw current game state.
|
||||
void draw() { this.scene = this.grid.render(); }
|
||||
void draw() {
|
||||
this.scene = this.grid.render();
|
||||
|
||||
this.scene.placeImageXY(
|
||||
new TextImage(
|
||||
"Score: " + String.valueOf(this.score), 25, Color.red
|
||||
),
|
||||
100, 20
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// The grid in which the game is played.
|
||||
@@ -126,18 +139,6 @@ class Grid {
|
||||
return free;
|
||||
}
|
||||
|
||||
// Get the indexes of all "unfree" cells -- those whose value in buf is not
|
||||
// 0, and represent a used tile.
|
||||
/*
|
||||
List<Integer> unfreeCellIdxs() {
|
||||
List<Integer> unfree = new ArrayList<>();
|
||||
for (int i = 0; i < this.sz; i++)
|
||||
if (this.buf.get(i) != 0 && unfree.add(i)) continue;
|
||||
|
||||
return unfree;
|
||||
}
|
||||
*/
|
||||
|
||||
// Set the tile at the coords.
|
||||
Grid set(int x, int y, int v) {
|
||||
this.buf.set(this.where(x, y), v);
|
||||
@@ -156,13 +157,16 @@ class Grid {
|
||||
// Get the y coord for an index.
|
||||
int iy(int i) { return i % this.w; }
|
||||
|
||||
void mv(int d) {
|
||||
int mv(int d) {
|
||||
int add = 0;
|
||||
// For each row / col.
|
||||
for (int i = 0; i < (d % 3 == 0 ? this.h : this.w); i++) {
|
||||
while (this.lnMv(i, d));
|
||||
this.squish(i, d);
|
||||
add += this.squish(i, d);
|
||||
while (this.lnMv(i, d));
|
||||
}
|
||||
|
||||
return add;
|
||||
}
|
||||
|
||||
// Get the ith row.
|
||||
@@ -222,14 +226,18 @@ class Grid {
|
||||
}
|
||||
|
||||
// Squish like tiles together.
|
||||
void squish(int i, int d) {
|
||||
int squish(int i, int d) {
|
||||
int add = 0;
|
||||
List<Integer> ln = getLn(i, d);
|
||||
for (int j = 0; j < ln.size() - 1; j++) {
|
||||
if (ln.get(j) != 0 && ln.get(j) == ln.get(j + 1)) {
|
||||
ln.set(j, 2 * ln.get(j));
|
||||
ln.set(j + 1, 0);
|
||||
add += ln.get(j);
|
||||
}
|
||||
}
|
||||
|
||||
return add;
|
||||
}
|
||||
|
||||
// Render the grid.
|
||||
@@ -297,7 +305,7 @@ class Examples {
|
||||
}
|
||||
|
||||
void testGame(Tester t) {
|
||||
game = new Game(2, 2);
|
||||
game = new Game(4, 4);
|
||||
game.launchGame();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user