Movement kinda works.
This commit is contained in:
@@ -80,12 +80,11 @@ class Game extends World {
|
||||
}
|
||||
|
||||
public void onKeyEvent(String key) {
|
||||
System.out.println("YES");
|
||||
if (key.equals("left") || key.equals("h")) this.grid.move(0);
|
||||
else if (key.equals("down") || key.equals("j")) this.grid.move(1);
|
||||
else if (key.equals("up") || key.equals("k")) this.grid.move(2);
|
||||
else if (key.equals("right") || key.equals("l")) this.grid.move(3);
|
||||
else return;
|
||||
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);
|
||||
else return; // Don't draw if bad key pressed.
|
||||
this.draw();
|
||||
}
|
||||
|
||||
@@ -148,20 +147,65 @@ class Grid {
|
||||
// Get the y coord for an index.
|
||||
int iy(int i) { return i % this.w; }
|
||||
|
||||
// Given an index and a direction, is there a free there?
|
||||
boolean freeDir(int i, int d) {
|
||||
if (d == 0) { // Left.
|
||||
if ((i - 1) % this.w < 0) return false; // At an edge.
|
||||
return this.buf.get(i - 1) == 0;
|
||||
} else if (d == 1) { // Down.
|
||||
if ((i + this.w) > this.sz) return false; // At an edge
|
||||
return this.buf.get(i + this.w) == 0;
|
||||
} else if (d == 2) { // Up.
|
||||
if ((i - this.w) < 0) return false;
|
||||
return this.buf.get(i - this.w) == 0;
|
||||
} else if (d == 3) { // Right.
|
||||
if ((i + 1) % this.w == 0) return false; // At an edge.
|
||||
return this.buf.get(i + 1) ==
|
||||
0; // Check if next space over is free.
|
||||
}
|
||||
|
||||
else
|
||||
throw new IllegalArgumentException("Bad direction code given.");
|
||||
}
|
||||
|
||||
// Get the indexes of all moveable tiles in a given direction -- those
|
||||
// that are abutted by a free cell in the specified direction.
|
||||
List<Integer> mvblDir(int d) {
|
||||
List<Integer> mvbl = new ArrayList<Integer>();
|
||||
|
||||
for (int i : this.unfreeCellIdxs())
|
||||
if (this.freeDir(i, d)) mvbl.add(i);
|
||||
|
||||
return mvbl;
|
||||
}
|
||||
|
||||
// Move the tile at the index 1 space in the specified direction.
|
||||
void mvTile(int i, int d) {
|
||||
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 == 2) this.buf.set(i - this.w, this.buf.get(i));
|
||||
else if (d == 3) this.buf.set(i + 1, this.buf.get(i));
|
||||
else throw new IllegalArgumentException("Bad direction code given.");
|
||||
|
||||
this.buf.set(i, 0); // Reset original.
|
||||
}
|
||||
|
||||
// Partial move -- apply movement rules once. There still may be tiles
|
||||
// which can move after this is done. Return true if there are still
|
||||
// more moves possible.
|
||||
boolean partMv(int d) {
|
||||
// 1. Find the tiles that can move.
|
||||
List<Integer> mvbl = this.mvblDir(d);
|
||||
if (mvbl.isEmpty()) return false;
|
||||
// 2. Move each tile to its new location.
|
||||
for (int i : mvbl) { this.mvTile(i, d); }
|
||||
return true;
|
||||
}
|
||||
|
||||
// Move all tiles in that direction until they can't move or combine any
|
||||
// more.
|
||||
void move(int d) {
|
||||
// TODO: Don't assume right.
|
||||
// Assume right.
|
||||
List<Integer> unfrees = this.unfreeCellIdxs();
|
||||
List<Integer> frees = this.freeCellIdxs();
|
||||
for (int unfree : unfrees) {
|
||||
/*
|
||||
if (frees.contains(this.ix(unfree) + 1)) {
|
||||
this.buf.set(unfree, 0))
|
||||
}
|
||||
*/
|
||||
}
|
||||
void mv(int d) {
|
||||
while (this.partMv(d)); // I love side effects :).
|
||||
}
|
||||
|
||||
// Render the grid.
|
||||
@@ -231,44 +275,9 @@ class Examples {
|
||||
t.checkExpect(regular.get(3, 3), 0);
|
||||
}
|
||||
|
||||
/*
|
||||
void testMoveRight(Tester t) {
|
||||
init();
|
||||
regular.set(1, 0, 16)
|
||||
.set(2, 0, 16)
|
||||
.set(3, 0, 32)
|
||||
.set(0, 1, 4)
|
||||
.set(1, 1, 4)
|
||||
.set(4, 1, 2);
|
||||
|
||||
// Move right.
|
||||
regular.move(2);
|
||||
|
||||
t.checkExpect(regular.get(2, 0), 32);
|
||||
t.checkExpect(regular.get(3, 0), 32);
|
||||
t.checkExpect(regular.get(2, 1), 8);
|
||||
t.checkExpect(regular.get(3, 1), 2);
|
||||
}
|
||||
*/
|
||||
|
||||
void testGame(Tester t) {
|
||||
game = new Game();
|
||||
game.grid.set(0, 0, 2);
|
||||
game.grid.set(0, 1, 4);
|
||||
// Random free space.
|
||||
game.grid.set(0, 3, 8);
|
||||
game.grid.set(1, 0, 16);
|
||||
game.grid.set(1, 1, 32);
|
||||
game.grid.set(1, 2, 64);
|
||||
game.grid.set(1, 3, 128);
|
||||
game.grid.set(2, 0, 256);
|
||||
game.grid.set(2, 1, 512);
|
||||
game.grid.set(2, 2, 1024);
|
||||
game.grid.set(2, 3, 2048);
|
||||
game.grid.set(3, 0, 4096);
|
||||
// Random free space.
|
||||
game.grid.set(3, 2, 8192);
|
||||
game.grid.set(3, 3, 16384);
|
||||
|
||||
game.launchGame();
|
||||
}
|
||||
|
Reference in New Issue
Block a user