too late.

This commit is contained in:
2025-05-13 00:22:18 -04:00
parent 3edaf673c8
commit 8319e00bae

View File

@@ -15,6 +15,8 @@ class Examples {
Game game = new Game(); Game game = new Game();
game.board.board.put(new Coord(0, 0), new Tile(2)); game.board.board.put(new Coord(0, 0), new Tile(2));
game.board.board.put(new Coord(0, 1), new Tile(20)); game.board.board.put(new Coord(0, 1), new Tile(20));
game.board.board.put(new Coord(0, 2), new Tile(200));
game.board.board.put(new Coord(0, 3), new Tile(2000));
game.run(); game.run();
} }
} }
@@ -22,10 +24,25 @@ class Examples {
// Config constants. // Config constants.
class Util { class Util {
// Game scale. // Game scale.
static int scale = 70; static int scale = 256;
// Default 2048 game board width. // Default 2048 game board width.
static int defaultWidth = 4; static int defaultWidth = 4;
// Calculate an appropriate font size for a number.
static double fontSize(int n) {
// Start scaling numbers down once they pass 2 digits.
double x = 0.73 * Math.pow(Math.ceil(Math.log(n) / Math.log(100)), -1);
return Util.scale * x;
}
// Convert a movement to a displacement vector.
Coord moveDisp(Move move) {
if (move.equals(Move.LEFT)) return new Coord(-1, 0);
else if (move.equals(Move.DOWN)) return new Coord(0, -1);
else if (move.equals(Move.UP)) return new Coord(0, 1);
else return new Coord(0, 1);
}
} }
// A game of 2048. // A game of 2048.
@@ -56,9 +73,22 @@ class Game extends World {
return scene; return scene;
} }
public void onKeyEvent(String key) {
if (key.equals("left") || key.equals("h")) this.board.move(Move.LEFT);
else if (key.equals("down") || key.equals("j"))
this.board.move(Move.DOWN);
else if (key.equals("up") || key.equals("k")) this.board.move(Move.UP);
else if (key.equals("right") || key.equals("l"))
this.board.move(Move.RIGHT);
else return;
}
void run() { this.bigBang(this.width, this.width); } void run() { this.bigBang(this.width, this.width); }
} }
// The possible movements.
enum Move { UP, LEFT, RIGHT, DOWN }
// The board on which the game is played. // The board on which the game is played.
class Board { class Board {
int sz; // The side length of the board grid square. int sz; // The side length of the board grid square.
@@ -93,6 +123,7 @@ class Board {
); );
} }
// Get the cell at the coords.
Cell get(Coord coord) { Cell get(Coord coord) {
Cell gotten = this.board.get(coord); Cell gotten = this.board.get(coord);
@@ -100,6 +131,16 @@ class Board {
if (gotten == null) return new Space(); if (gotten == null) return new Space();
else return gotten; else return gotten;
} }
// Move in the given direction.
void move(Move move) {
for (int x = 0; x < this.sz; x++) {
for (int y = 0; y < this.sz; y++) {
// Copy logic from old code here.
Cell cell = this.get(new Coord(x, y));
}
}
}
} }
// A cell on the board. // A cell on the board.
@@ -122,18 +163,14 @@ class Tile extends Cell {
Tile(int n) { this.n = n; } Tile(int n) { this.n = n; }
Color col() { Color col() {
// int r = (int)Math.min(255, 20 * (int)(Math.log(this.n) / int r = (int)Math.min(255, 20 * (int)(Math.log(this.n) / Math.log(2)));
// Math.log(2))); return new Color(r, r / 2 + 80, r / 3 + 20); return new Color(r, r / 2 + 80, r / 3 + 20);
return Color.RED;
} }
WorldImage draw() { WorldImage draw() {
return new OverlayImage( return new OverlayImage(
new TextImage( new TextImage(
String.valueOf(this.n), String.valueOf(this.n), Util.fontSize(this.n), Color.BLACK
(Util.scale / Math.ceil((Math.log(this.n) / Math.log(10)))) *
0.7,
Color.BLACK
), ),
super.draw() super.draw()
); );
@@ -142,7 +179,7 @@ class Tile extends Cell {
// An empty space on the board. // An empty space on the board.
class Space extends Cell { class Space extends Cell {
Color col() { return Color.BLUE; } Color col() { return Color.LIGHT_GRAY; }
} }
// Board coordinates. // Board coordinates.
@@ -154,6 +191,10 @@ class Coord {
this.y = y; this.y = y;
} }
Coord add(Coord that) { return that.addHelper(this.x, this.y); }
Coord addHelper(int x, int y) { return new Coord(this.x + x, this.y + y); }
public boolean equals(Object that) { public boolean equals(Object that) {
if (!(that instanceof Coord)) return false; if (!(that instanceof Coord)) return false;
else return ((Coord)that).x == this.x && ((Coord)that).y == this.y; else return ((Coord)that).x == this.x && ((Coord)that).y == this.y;