From e9f15ebd03181a7ffc45dbc6c233e44f71e34206 Mon Sep 17 00:00:00 2001 From: Jacob Date: Wed, 9 Apr 2025 20:22:03 -0400 Subject: [PATCH] Movement kinda works. --- .../src/twentyfortyeight/Main.java | 115 ++++++++++-------- 1 file changed, 62 insertions(+), 53 deletions(-) diff --git a/twentyfortyeight/src/twentyfortyeight/Main.java b/twentyfortyeight/src/twentyfortyeight/Main.java index 0fcfe9a..87bef79 100644 --- a/twentyfortyeight/src/twentyfortyeight/Main.java +++ b/twentyfortyeight/src/twentyfortyeight/Main.java @@ -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 mvblDir(int d) { + List mvbl = new ArrayList(); + + 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 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 unfrees = this.unfreeCellIdxs(); - List 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(); }