WHat;
This commit is contained in:
@@ -71,7 +71,10 @@ class Game extends World {
|
|||||||
this.scene = new WorldScene(this.grid.w, this.grid.h);
|
this.scene = new WorldScene(this.grid.w, this.grid.h);
|
||||||
}
|
}
|
||||||
|
|
||||||
void launchGame() { this.bigBang(this.w, this.h, 0.01); }
|
void launchGame() {
|
||||||
|
this.bigBang(this.w, this.h, 0.01);
|
||||||
|
this.grid.set(0, 0, 2).set(0, 2, 2);
|
||||||
|
}
|
||||||
|
|
||||||
public WorldScene makeScene() {
|
public WorldScene makeScene() {
|
||||||
this.scene = this.getEmptyScene();
|
this.scene = this.getEmptyScene();
|
||||||
@@ -84,7 +87,16 @@ class Game extends World {
|
|||||||
else if (key.equals("down") || key.equals("j")) this.grid.mv(1);
|
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("up") || key.equals("k")) this.grid.mv(2);
|
||||||
else if (key.equals("right") || key.equals("l")) this.grid.mv(3);
|
else if (key.equals("right") || key.equals("l")) this.grid.mv(3);
|
||||||
else return; // Don't draw if bad key pressed.
|
else return; // Don't draw or add tiles if bad key pressed.
|
||||||
|
|
||||||
|
this.draw();
|
||||||
|
|
||||||
|
List<Integer> frees = this.grid.freeCellIdxs();
|
||||||
|
// Add tiles
|
||||||
|
this.grid.buf.set(
|
||||||
|
frees.get(0), 2 // this.rand.nextInt() % (frees.size() - 1)), 2
|
||||||
|
);
|
||||||
|
|
||||||
this.draw();
|
this.draw();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -173,13 +185,46 @@ class Grid {
|
|||||||
void combine(int d) {
|
void combine(int d) {
|
||||||
// Assume left.
|
// Assume left.
|
||||||
if (d == 0) {
|
if (d == 0) {
|
||||||
for (int i = 0; i < this.h; i++) {
|
for (int y = 0; y < this.h; y++) {
|
||||||
int prv = this.get(0, i);
|
int prv = this.get(0, y);
|
||||||
for (int j = 1; j < this.w; j++) {
|
for (int x = 1; x < this.w; x++) {
|
||||||
int cur = this.get(j, i);
|
int cur = this.get(x, y);
|
||||||
if (cur == prv) {
|
if (cur == prv) {
|
||||||
this.set(j - 1, i, cur * 2);
|
this.set(x - 1, y, cur * 2);
|
||||||
this.set(j, i, 0);
|
this.set(x, y, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (d == 1) {
|
||||||
|
for (int x = 0; x < this.w; x++) {
|
||||||
|
int prv = this.get(x, this.h - 1);
|
||||||
|
for (int y = this.h - 2; y > 0; y--) {
|
||||||
|
int cur = this.get(x, y);
|
||||||
|
if (cur == prv) {
|
||||||
|
this.set(x, y + 1, cur * 2);
|
||||||
|
this.set(x, y, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (d == 2) {
|
||||||
|
for (int x = 0; x < this.w; x++) {
|
||||||
|
int prv = this.get(x, 0);
|
||||||
|
for (int y = 1; y < this.h; y++) {
|
||||||
|
int cur = this.get(x, y);
|
||||||
|
if (cur == prv) {
|
||||||
|
this.set(x, y - 1, cur * 2);
|
||||||
|
this.set(x, y, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (d == 3) {
|
||||||
|
for (int y = 0; y < this.h; y++) {
|
||||||
|
int prv = this.get(this.w - 1, y);
|
||||||
|
for (int x = this.w - 2; x > 0; x--) {
|
||||||
|
int cur = this.get(x, y);
|
||||||
|
if (cur == prv) {
|
||||||
|
this.set(x + 1, y, cur * 2);
|
||||||
|
this.set(x, y, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -226,6 +271,7 @@ class Grid {
|
|||||||
void mv(int d) {
|
void mv(int d) {
|
||||||
while (this.partMv(d)); // I love side effects :).
|
while (this.partMv(d)); // I love side effects :).
|
||||||
this.combine(d);
|
this.combine(d);
|
||||||
|
// while (this.partMv(d)); // Complete move.
|
||||||
}
|
}
|
||||||
|
|
||||||
// Render the grid.
|
// Render the grid.
|
||||||
@@ -294,7 +340,6 @@ class Examples {
|
|||||||
|
|
||||||
void testGame(Tester t) {
|
void testGame(Tester t) {
|
||||||
game = new Game();
|
game = new Game();
|
||||||
game.grid.set(0, 0, 2).set(2, 0, 2);
|
|
||||||
|
|
||||||
game.launchGame();
|
game.launchGame();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user