LOOK HERE IT IS.

This commit is contained in:
Jacob Signorovitch
2025-03-27 15:54:58 -04:00
parent d7efde2576
commit b4ff4b23c0

View File

@@ -3,18 +3,21 @@ package twentyfortyeight;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
import javalib.impworld.*;
import tester.Tester; import tester.Tester;
// A game of 2048. // A game of 2048.
class Game { class Game extends World {
int score; int score;
Grid grid; Grid grid;
Random rand; Random rand;
WorldScene scene;
Game(int score, Grid grid, Random rand) { Game(int score, Grid grid, Random rand, WorldScene scene) {
this.score = score; this.score = score;
this.grid = grid; this.grid = grid;
this.rand = rand; this.rand = rand;
this.scene = scene;
} }
// Convenience constructor for init with default settings. // Convenience constructor for init with default settings.
@@ -22,6 +25,9 @@ class Game {
this.grid = new Grid(4, 4); this.grid = new Grid(4, 4);
this.score = 0; this.score = 0;
this.rand = new Random(); this.rand = new Random();
this.scene = new WorldScene(
this.grid.w, this.grid.h
); // TODO: Add proper scaling parameter.
} }
// Convenience constructor for init with custom settings. // Convenience constructor for init with custom settings.
@@ -29,7 +35,10 @@ class Game {
this.grid = new Grid(w, h); this.grid = new Grid(w, h);
this.score = 0; this.score = 0;
this.rand = new Random(seed); this.rand = new Random(seed);
this.scene = new WorldScene(this.grid.w, this.grid.h);
} }
public WorldScene makeScene() { return null; }
} }
// The grid in which the game is played. // The grid in which the game is played.
@@ -70,6 +79,14 @@ class Grid {
// Get the value at the coords. Just for testing. // Get the value at the coords. Just for testing.
int get(int x, int y) { return this.buf.get(this.w * y + x); } int get(int x, int y) { return this.buf.get(this.w * y + x); }
// 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.
for (int i = 0; i < this.h; i++) {}
}
} }
class Examples { class Examples {
@@ -116,4 +133,22 @@ class Examples {
t.checkExpect(regular.get(0, 0), 0); t.checkExpect(regular.get(0, 0), 0);
t.checkExpect(regular.get(3, 3), 0); t.checkExpect(regular.get(3, 3), 0);
} }
void testMoveLeft(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);
}
} }