Combinations.
This commit is contained in:
@@ -150,10 +150,10 @@ class Grid {
|
|||||||
// Given an index and a direction, is there a free there?
|
// Given an index and a direction, is there a free there?
|
||||||
boolean freeDir(int i, int d) {
|
boolean freeDir(int i, int d) {
|
||||||
if (d == 0) { // Left.
|
if (d == 0) { // Left.
|
||||||
if ((i - 1) % this.w < 0) return false; // At an edge.
|
if (i % this.w == 0) return false; // At an edge.
|
||||||
return this.buf.get(i - 1) == 0;
|
return this.buf.get(i - 1) == 0;
|
||||||
} else if (d == 1) { // Down.
|
} else if (d == 1) { // Down.
|
||||||
if ((i + this.w) > this.sz) return false; // At an edge
|
if ((i + 1 + this.w) > this.sz) return false; // At an edge
|
||||||
return this.buf.get(i + this.w) == 0;
|
return this.buf.get(i + this.w) == 0;
|
||||||
} else if (d == 2) { // Up.
|
} else if (d == 2) { // Up.
|
||||||
if ((i - this.w) < 0) return false;
|
if ((i - this.w) < 0) return false;
|
||||||
@@ -168,6 +168,24 @@ class Grid {
|
|||||||
throw new IllegalArgumentException("Bad direction code given.");
|
throw new IllegalArgumentException("Bad direction code given.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Combine numbers that are the same into their sum. Start checking for
|
||||||
|
// combinations from the side the direction is to for each row/column.
|
||||||
|
void combine(int d) {
|
||||||
|
// Assume left.
|
||||||
|
if (d == 0) {
|
||||||
|
for (int i = 0; i < this.h; i++) {
|
||||||
|
int prv = this.get(0, i);
|
||||||
|
for (int j = 1; j < this.w; j++) {
|
||||||
|
int cur = this.get(j, i);
|
||||||
|
if (cur == prv) {
|
||||||
|
this.set(j - 1, i, cur * 2);
|
||||||
|
this.set(j, i, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Get the indexes of all moveable tiles in a given direction -- those
|
// Get the indexes of all moveable tiles in a given direction -- those
|
||||||
// that are abutted by a free cell in the specified direction.
|
// that are abutted by a free cell in the specified direction.
|
||||||
List<Integer> mvblDir(int d) {
|
List<Integer> mvblDir(int d) {
|
||||||
@@ -181,6 +199,7 @@ class Grid {
|
|||||||
|
|
||||||
// Move the tile at the index 1 space in the specified direction.
|
// Move the tile at the index 1 space in the specified direction.
|
||||||
void mvTile(int i, int d) {
|
void mvTile(int i, int d) {
|
||||||
|
System.out.println("attempting to move");
|
||||||
if (d == 0) this.buf.set(i - 1, this.buf.get(i));
|
if (d == 0) this.buf.set(i - 1, this.buf.get(i));
|
||||||
else if (d == 1) this.buf.set(i + this.w, this.buf.get(i));
|
else if (d == 1) this.buf.set(i + this.w, this.buf.get(i));
|
||||||
else if (d == 2) this.buf.set(i - this.w, this.buf.get(i));
|
else if (d == 2) this.buf.set(i - this.w, this.buf.get(i));
|
||||||
@@ -206,24 +225,22 @@ class Grid {
|
|||||||
// more.
|
// more.
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Render the grid.
|
// Render the grid.
|
||||||
WorldScene render() {
|
WorldScene render() {
|
||||||
WorldScene bg =
|
WorldScene bg =
|
||||||
new WorldScene(Game.SCALE * this.w, Game.SCALE * this.h);
|
new WorldScene(Game.SCALE * this.w, Game.SCALE * this.h);
|
||||||
for (int i = 0; i < this.h; i++) {
|
for (int i = 0; i < this.h; i++)
|
||||||
for (int j = 0; j < this.w; j++) {
|
for (int j = 0; j < this.w; j++) {
|
||||||
int n = this.get(i, j);
|
int n = this.get(i, j);
|
||||||
int coordx = i * Game.SCALE + Game.SCALE / 2;
|
int coordx = i * Game.SCALE + Game.SCALE / 2;
|
||||||
int coordy = j * Game.SCALE + Game.SCALE / 2;
|
int coordy = j * Game.SCALE + Game.SCALE / 2;
|
||||||
if (n == 0) {
|
if (n == 0) bg.placeImageXY(ImgUtil.mkfree(), coordx, coordy);
|
||||||
bg.placeImageXY(ImgUtil.mkfree(), coordx, coordy);
|
else bg.placeImageXY(ImgUtil.mktile(n), coordx, coordy);
|
||||||
} else {
|
|
||||||
bg.placeImageXY(ImgUtil.mktile(n), coordx, coordy);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return bg;
|
return bg;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -277,7 +294,7 @@ class Examples {
|
|||||||
|
|
||||||
void testGame(Tester t) {
|
void testGame(Tester t) {
|
||||||
game = new Game();
|
game = new Game();
|
||||||
game.grid.set(0, 0, 2);
|
game.grid.set(0, 0, 2).set(2, 0, 2);
|
||||||
|
|
||||||
game.launchGame();
|
game.launchGame();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user