i dunno how git works tbh.
This commit is contained in:
@@ -75,37 +75,6 @@ class Game extends World {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void onKeyEvent(String key) {
|
public void onKeyEvent(String key) {
|
||||||
<<<<<<< HEAD
|
|
||||||
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();
|
|
||||||
// Add tiles
|
|
||||||
this.grid.buf.set(frees.get(this.rand.nextInt(frees.size())), 2);
|
|
||||||
|
|
||||||
this.draw();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Draw current game state.
|
|
||||||
void draw() {
|
|
||||||
this.scene = this.grid.render();
|
|
||||||
|
|
||||||
this.scene.placeImageXY(
|
|
||||||
new TextImage(
|
|
||||||
"Score: " + String.valueOf(this.score), 25, Color.red
|
|
||||||
),
|
|
||||||
100, 20
|
|
||||||
);
|
|
||||||
}
|
|
||||||
=======
|
|
||||||
if (key.equals("left") || key.equals("h")) this.board.move(Move.LEFT);
|
if (key.equals("left") || key.equals("h")) this.board.move(Move.LEFT);
|
||||||
else if (key.equals("down") || key.equals("j"))
|
else if (key.equals("down") || key.equals("j"))
|
||||||
this.board.move(Move.DOWN);
|
this.board.move(Move.DOWN);
|
||||||
@@ -116,7 +85,6 @@ class Game extends World {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void run() { this.bigBang(this.width, this.width); }
|
void run() { this.bigBang(this.width, this.width); }
|
||||||
>>>>>>> origin/main
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// The possible movements.
|
// The possible movements.
|
||||||
@@ -143,44 +111,12 @@ class Board {
|
|||||||
for (int x = 0; x < this.sz; x++)
|
for (int x = 0; x < this.sz; x++)
|
||||||
row.add(this.get(new Coord(x, y)).draw());
|
row.add(this.get(new Coord(x, y)).draw());
|
||||||
|
|
||||||
<<<<<<< HEAD
|
|
||||||
// Set the tile at the coords.
|
|
||||||
Grid set(int x, int y, int v) {
|
|
||||||
this.buf.set(this.where(x, y), v);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the index for the coord.
|
|
||||||
int where(int x, int y) { return this.w * y + x; }
|
|
||||||
|
|
||||||
// Get the value at the coords.
|
|
||||||
int get(int x, int y) { return this.buf.get(this.w * y + x); }
|
|
||||||
|
|
||||||
// Get the x coord for an index.
|
|
||||||
int ix(int i) { return i % this.h; }
|
|
||||||
|
|
||||||
// Get the y coord for an index.
|
|
||||||
int iy(int i) { return i % this.w; }
|
|
||||||
|
|
||||||
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));
|
|
||||||
add += this.squish(i, d);
|
|
||||||
while (this.lnMv(i, d));
|
|
||||||
}
|
|
||||||
|
|
||||||
return add;
|
|
||||||
}
|
|
||||||
=======
|
|
||||||
// Add row to the list.
|
// Add row to the list.
|
||||||
rows.add(row.stream().reduce(
|
rows.add(row.stream().reduce(
|
||||||
new EmptyImage(),
|
new EmptyImage(),
|
||||||
(cell1, cell2) -> new BesideImage(cell1, cell2)
|
(cell1, cell2) -> new BesideImage(cell1, cell2)
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
>>>>>>> origin/main
|
|
||||||
|
|
||||||
// Collapse rows into single image.
|
// Collapse rows into single image.
|
||||||
return rows.stream().reduce(
|
return rows.stream().reduce(
|
||||||
@@ -197,56 +133,14 @@ class Board {
|
|||||||
else return gotten;
|
else return gotten;
|
||||||
}
|
}
|
||||||
|
|
||||||
<<<<<<< HEAD
|
|
||||||
// Get the line.
|
|
||||||
List<Integer> getLn(int i, int d) {
|
|
||||||
return d == 0 ? this.getRow(i)
|
|
||||||
: d == 1 ? this.getCol(i).reversed()
|
|
||||||
: d == 2 ? this.getCol(i)
|
|
||||||
: this.getRow(i).reversed();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Move the specified line in the specified direction once. If there is a
|
|
||||||
// combination, return false. If there are no more moves possible, return
|
|
||||||
// false.
|
|
||||||
boolean lnMv(int i, int d) {
|
|
||||||
List<Integer> ln = getLn(i, d);
|
|
||||||
|
|
||||||
int c = 0; // Moves made in line.
|
|
||||||
for (int j = 1; j < ln.size(); j++) {
|
|
||||||
if (ln.get(j - 1) == 0 && ln.get(j) != 0) {
|
|
||||||
ln.set(j - 1, ln.get(j));
|
|
||||||
ln.set(j, 0);
|
|
||||||
} else c++;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If nothing has been done, give up.
|
|
||||||
if (c == ln.size() - 1) return false;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Squish like tiles together.
|
|
||||||
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);
|
|
||||||
=======
|
|
||||||
// Move in the given direction.
|
// Move in the given direction.
|
||||||
void move(Move move) {
|
void move(Move move) {
|
||||||
for (int x = 0; x < this.sz; x++) {
|
for (int x = 0; x < this.sz; x++) {
|
||||||
for (int y = 0; y < this.sz; y++) {
|
for (int y = 0; y < this.sz; y++) {
|
||||||
// Copy logic from old code here.
|
// Copy logic from old code here.
|
||||||
Cell cell = this.get(new Coord(x, y));
|
Cell cell = this.get(new Coord(x, y));
|
||||||
>>>>>>> origin/main
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return add;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -281,42 +175,6 @@ class Tile extends Cell {
|
|||||||
),
|
),
|
||||||
super.draw()
|
super.draw()
|
||||||
);
|
);
|
||||||
<<<<<<< HEAD
|
|
||||||
t.checkExpect(
|
|
||||||
verySmall.freeCellIdxs(), new ArrayList<Integer>(List.of(0))
|
|
||||||
);
|
|
||||||
t.checkExpect(
|
|
||||||
oblong.freeCellIdxs(), new ArrayList<Integer>(List.of(0, 1, 2))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
void testGridSet(Tester t) {
|
|
||||||
init();
|
|
||||||
small.set(0, 0, 1);
|
|
||||||
t.checkExpect(small.freeCellIdxs(), new ArrayList<>(List.of(1, 2, 3)));
|
|
||||||
small.set(1, 1, 1);
|
|
||||||
t.checkExpect(small.freeCellIdxs(), new ArrayList<>(List.of(1, 2)));
|
|
||||||
}
|
|
||||||
|
|
||||||
void testRegular(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);
|
|
||||||
|
|
||||||
t.checkExpect(regular.get(1, 0), 16);
|
|
||||||
t.checkExpect(regular.get(0, 0), 0);
|
|
||||||
t.checkExpect(regular.get(3, 3), 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
void testGame(Tester t) {
|
|
||||||
game = new Game(4, 4);
|
|
||||||
game.launchGame();
|
|
||||||
=======
|
|
||||||
>>>>>>> origin/main
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user