why.
This commit is contained in:
@@ -10,25 +10,67 @@ import javalib.impworld.*;
|
||||
import javalib.worldimages.*;
|
||||
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.
|
||||
class Util {
|
||||
// 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.
|
||||
class Board {
|
||||
int sz; // The side length of the board grid square.
|
||||
Map<Coord, Tile> board;
|
||||
Map<Coord, Cell> board;
|
||||
|
||||
Board(int sz) {
|
||||
this.sz = sz;
|
||||
this.board = new HashMap<Coord, Tile>();
|
||||
this.board = new HashMap<Coord, Cell>();
|
||||
}
|
||||
|
||||
// Draw the board.
|
||||
WorldImage draw() {
|
||||
WorldImage img;
|
||||
ArrayList<WorldImage> rows = new ArrayList<>();
|
||||
|
||||
// For each row.
|
||||
@@ -36,7 +78,7 @@ class Board {
|
||||
ArrayList<WorldImage> row = new ArrayList<>();
|
||||
// For each element in the row.
|
||||
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.
|
||||
rows.add(row.stream().reduce(
|
||||
@@ -47,45 +89,75 @@ class Board {
|
||||
|
||||
// Collapse rows into single image.
|
||||
return rows.stream().reduce(
|
||||
new EmptyImage(),
|
||||
(row1, row2) -> new BesideAlignImage(AlignModeY.BOTTOM, row1, row2)
|
||||
new EmptyImage(), (row1, row2) -> new AboveImage(row1, row2)
|
||||
);
|
||||
}
|
||||
|
||||
// A moveable tile on the board.
|
||||
class Tile {
|
||||
int n;
|
||||
Cell get(Coord coord) {
|
||||
Cell gotten = this.board.get(coord);
|
||||
|
||||
Tile(int n) { this.n = n; }
|
||||
// Check if there's something there, otherwise return empty space.
|
||||
if (gotten == null) return new Space();
|
||||
else return gotten;
|
||||
}
|
||||
}
|
||||
|
||||
// Generate the color of the tile.
|
||||
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);
|
||||
}
|
||||
// A cell on the board.
|
||||
abstract class Cell {
|
||||
// Generate the color of the tile.
|
||||
Color col() { return Color.PINK; }
|
||||
|
||||
// Draw the tile.
|
||||
WorldImage draw() {
|
||||
return new RectangleImage(
|
||||
Util.scale, Util.scale, OutlineMode.SOLID, this.col()
|
||||
);
|
||||
}
|
||||
// Draw the cell.
|
||||
WorldImage draw() {
|
||||
return new RectangleImage(
|
||||
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.
|
||||
class Coord {
|
||||
int x, y;
|
||||
|
||||
Coord(int x, int y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public boolean equals(Object that) {
|
||||
if (!(that instanceof Coord)) return false;
|
||||
else return ((Coord)that).x == this.x && ((Coord)that).y == this.y;
|
||||
}
|
||||
|
||||
public int hashCode() { return 31 * Integer.hashCode(this.x + this.y); }
|
||||
WorldImage draw() {
|
||||
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;
|
||||
|
||||
Coord(int x, int y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public boolean equals(Object that) {
|
||||
if (!(that instanceof Coord)) return false;
|
||||
else return ((Coord)that).x == this.x && ((Coord)that).y == this.y;
|
||||
}
|
||||
|
||||
public int hashCode() { return 31 * Integer.hashCode(this.x + this.y); }
|
||||
}
|
||||
|
Reference in New Issue
Block a user