why.
This commit is contained in:
@@ -10,25 +10,67 @@ import javalib.impworld.*;
|
|||||||
import javalib.worldimages.*;
|
import javalib.worldimages.*;
|
||||||
import tester.Tester;
|
import tester.Tester;
|
||||||
|
|
||||||
|
class Examples {
|
||||||
|
void testRun(Tester t) {
|
||||||
|
Game game = new Game();
|
||||||
|
game.board.board.put(new Coord(0, 0), new Tile(2));
|
||||||
|
game.board.board.put(new Coord(0, 1), new Tile(20));
|
||||||
|
game.run();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Config constants.
|
// Config constants.
|
||||||
class Util {
|
class Util {
|
||||||
// Game scale.
|
// Game scale.
|
||||||
static int scale = 24;
|
static int scale = 70;
|
||||||
|
|
||||||
|
// Default 2048 game board width.
|
||||||
|
static int defaultWidth = 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
// A game of 2048.
|
||||||
|
class Game extends World {
|
||||||
|
int score; // The current score.
|
||||||
|
int sz; // The size of the grid.
|
||||||
|
int width; // The width of the image in pixels.
|
||||||
|
Board board; // The game board.
|
||||||
|
|
||||||
|
Game(int score, int sz, int width, Board board) {
|
||||||
|
this.score = score;
|
||||||
|
this.sz = sz;
|
||||||
|
this.width = this.sz * Util.scale;
|
||||||
|
this.board = board;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convenince constructor with default parameters.
|
||||||
|
Game() {
|
||||||
|
this.score = 0;
|
||||||
|
this.sz = Util.defaultWidth;
|
||||||
|
this.width = this.sz * Util.scale;
|
||||||
|
this.board = new Board(this.sz);
|
||||||
|
}
|
||||||
|
|
||||||
|
public WorldScene makeScene() {
|
||||||
|
WorldScene scene = new WorldScene(this.width, this.width);
|
||||||
|
scene.placeImageXY(this.board.draw(), this.width / 2, this.width / 2);
|
||||||
|
return scene;
|
||||||
|
}
|
||||||
|
|
||||||
|
void run() { this.bigBang(this.width, this.width); }
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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.
|
||||||
Map<Coord, Tile> board;
|
Map<Coord, Cell> board;
|
||||||
|
|
||||||
Board(int sz) {
|
Board(int sz) {
|
||||||
this.sz = sz;
|
this.sz = sz;
|
||||||
this.board = new HashMap<Coord, Tile>();
|
this.board = new HashMap<Coord, Cell>();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw the board.
|
// Draw the board.
|
||||||
WorldImage draw() {
|
WorldImage draw() {
|
||||||
WorldImage img;
|
|
||||||
ArrayList<WorldImage> rows = new ArrayList<>();
|
ArrayList<WorldImage> rows = new ArrayList<>();
|
||||||
|
|
||||||
// For each row.
|
// For each row.
|
||||||
@@ -36,7 +78,7 @@ class Board {
|
|||||||
ArrayList<WorldImage> row = new ArrayList<>();
|
ArrayList<WorldImage> row = new ArrayList<>();
|
||||||
// For each element in the row.
|
// For each element in the row.
|
||||||
for (int x = 0; x < this.sz; x++)
|
for (int x = 0; x < this.sz; x++)
|
||||||
row.add(this.board.get(new Coord(x, y)).draw());
|
row.add(this.get(new Coord(x, y)).draw());
|
||||||
|
|
||||||
// Add row to the list.
|
// Add row to the list.
|
||||||
rows.add(row.stream().reduce(
|
rows.add(row.stream().reduce(
|
||||||
@@ -47,34 +89,64 @@ class Board {
|
|||||||
|
|
||||||
// Collapse rows into single image.
|
// Collapse rows into single image.
|
||||||
return rows.stream().reduce(
|
return rows.stream().reduce(
|
||||||
new EmptyImage(),
|
new EmptyImage(), (row1, row2) -> new AboveImage(row1, row2)
|
||||||
(row1, row2) -> new BesideAlignImage(AlignModeY.BOTTOM, row1, row2)
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// A moveable tile on the board.
|
Cell get(Coord coord) {
|
||||||
class Tile {
|
Cell gotten = this.board.get(coord);
|
||||||
int n;
|
|
||||||
|
|
||||||
Tile(int n) { this.n = n; }
|
// Check if there's something there, otherwise return empty space.
|
||||||
|
if (gotten == null) return new Space();
|
||||||
// Generate the color of the tile.
|
else return gotten;
|
||||||
Color col() {
|
|
||||||
int r =
|
|
||||||
(int)Math.min(255, 20 * (int)(Math.log(this.n) / Math.log(2)));
|
|
||||||
return new Color(r, r / 2 + 80, r / 3 + 20);
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Draw the tile.
|
// A cell on the board.
|
||||||
|
abstract class Cell {
|
||||||
|
// Generate the color of the tile.
|
||||||
|
Color col() { return Color.PINK; }
|
||||||
|
|
||||||
|
// Draw the cell.
|
||||||
WorldImage draw() {
|
WorldImage draw() {
|
||||||
return new RectangleImage(
|
return new RectangleImage(
|
||||||
Util.scale, Util.scale, OutlineMode.SOLID, this.col()
|
Util.scale, Util.scale, OutlineMode.SOLID, this.col()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// A moveable tile on the board.
|
||||||
|
class Tile extends Cell {
|
||||||
|
int n;
|
||||||
|
|
||||||
|
Tile(int n) { this.n = n; }
|
||||||
|
|
||||||
|
Color col() {
|
||||||
|
// int r = (int)Math.min(255, 20 * (int)(Math.log(this.n) /
|
||||||
|
// Math.log(2))); return new Color(r, r / 2 + 80, r / 3 + 20);
|
||||||
|
return Color.RED;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Board coordinates.
|
WorldImage draw() {
|
||||||
class Coord {
|
return new OverlayImage(
|
||||||
|
new TextImage(
|
||||||
|
String.valueOf(this.n),
|
||||||
|
(Util.scale / Math.ceil((Math.log(this.n) / Math.log(10)))) *
|
||||||
|
0.7,
|
||||||
|
Color.BLACK
|
||||||
|
),
|
||||||
|
super.draw()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// An empty space on the board.
|
||||||
|
class Space extends Cell {
|
||||||
|
Color col() { return Color.BLUE; }
|
||||||
|
}
|
||||||
|
|
||||||
|
// Board coordinates.
|
||||||
|
class Coord {
|
||||||
int x, y;
|
int x, y;
|
||||||
|
|
||||||
Coord(int x, int y) {
|
Coord(int x, int y) {
|
||||||
@@ -88,4 +160,4 @@ class Board {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public int hashCode() { return 31 * Integer.hashCode(this.x + this.y); }
|
public int hashCode() { return 31 * Integer.hashCode(this.x + this.y); }
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user