doesn't work.
This commit is contained in:
@@ -1,12 +1,9 @@
|
|||||||
package twentyfortyeight;
|
package twentyfortyeight;
|
||||||
|
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
import java.time.format.TextStyle;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Random;
|
|
||||||
import javalib.impworld.*;
|
import javalib.impworld.*;
|
||||||
import javalib.worldimages.*;
|
import javalib.worldimages.*;
|
||||||
import tester.Tester;
|
import tester.Tester;
|
||||||
@@ -15,9 +12,9 @@ class Examples {
|
|||||||
void testRun(Tester t) {
|
void testRun(Tester t) {
|
||||||
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(3, 1), new Tile(20));
|
||||||
game.board.board.put(new Coord(0, 2), new Tile(200));
|
game.board.board.put(new Coord(3, 2), new Tile(200));
|
||||||
game.board.board.put(new Coord(0, 3), new Tile(2000));
|
game.board.board.put(new Coord(2, 3), new Tile(2000));
|
||||||
game.run();
|
game.run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -25,7 +22,7 @@ class Examples {
|
|||||||
// Config constants.
|
// Config constants.
|
||||||
class Util {
|
class Util {
|
||||||
// Game scale.
|
// Game scale.
|
||||||
static int scale = 256;
|
static int scale = 128;
|
||||||
|
|
||||||
// Default 2048 game board width.
|
// Default 2048 game board width.
|
||||||
static int defaultWidth = 4;
|
static int defaultWidth = 4;
|
||||||
@@ -38,7 +35,7 @@ class Util {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Convert a movement to a displacement vector.
|
// Convert a movement to a displacement vector.
|
||||||
Coord moveDisp(Move move) {
|
static Coord moveDisp(Move move) {
|
||||||
if (move.equals(Move.LEFT)) return new Coord(-1, 0);
|
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.DOWN)) return new Coord(0, -1);
|
||||||
else if (move.equals(Move.UP)) return new Coord(0, 1);
|
else if (move.equals(Move.UP)) return new Coord(0, 1);
|
||||||
@@ -75,6 +72,7 @@ class Game extends World {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void onKeyEvent(String key) {
|
public void onKeyEvent(String key) {
|
||||||
|
System.out.println("got key" + key);
|
||||||
if (key.equals("left") || key.equals("h")) this.board.move(Move.LEFT);
|
if (key.equals("left") || key.equals("h")) this.board.move(Move.LEFT);
|
||||||
else if (key.equals("down") || key.equals("j"))
|
else if (key.equals("down") || key.equals("j"))
|
||||||
this.board.move(Move.DOWN);
|
this.board.move(Move.DOWN);
|
||||||
@@ -84,7 +82,7 @@ class Game extends World {
|
|||||||
else return;
|
else return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void run() { this.bigBang(this.width, this.width); }
|
void run() { this.bigBang(this.width, this.width, 0.01); }
|
||||||
}
|
}
|
||||||
|
|
||||||
// The possible movements.
|
// The possible movements.
|
||||||
@@ -133,12 +131,60 @@ class Board {
|
|||||||
else return gotten;
|
else return gotten;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set the cell at the coords.
|
||||||
|
void set(Coord coord, Cell cell) {
|
||||||
|
this.board.put(coord, cell);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try to move the cell at the coords to the coords.
|
||||||
|
void tryCellMove(Coord coord, Coord dest) {
|
||||||
|
Cell cell = this.get(coord);
|
||||||
|
Cell destCell = this.get(dest);
|
||||||
|
|
||||||
|
if (cell.isMoveable() && destCell.isReplaceable()) {
|
||||||
|
this.set(dest, cell);
|
||||||
|
this.set(coord, new Space());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Move cell as much as possible.
|
||||||
|
void fullCellMove(Coord coord, Move move) {
|
||||||
|
Cell cell = this.get(coord);
|
||||||
|
Coord current = coord;
|
||||||
|
|
||||||
|
if (!cell.isMoveable()) return;
|
||||||
|
|
||||||
|
while (this.get(current.add(Util.moveDisp(move))).isReplaceable()) {
|
||||||
|
this.tryCellMove(current, current.add(Util.moveDisp(move)));
|
||||||
|
current = current.add(Util.moveDisp(move));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Move in the given direction.
|
// Move in the given direction.
|
||||||
void move(Move move) {
|
void move(Move move) {
|
||||||
for (int x = 0; x < this.sz; x++) {
|
System.out.println("moving");
|
||||||
for (int y = 0; y < this.sz; y++) {
|
for (int i = 0; i < this.sz; i ++) {
|
||||||
// Copy logic from old code here.
|
this.moveLine(i, move);
|
||||||
Cell cell = this.get(new Coord(x, y));
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Move a line of tiles.
|
||||||
|
void moveLine(int i, Move move) {
|
||||||
|
if (move.equals(Move.LEFT)) {
|
||||||
|
for (int x = 1; x < this.sz; x++) {
|
||||||
|
this.fullCellMove(new Coord(x, i), move);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (move.equals(Move.RIGHT)) {
|
||||||
|
for (int x = 0; x < this.sz - 1; x++) {
|
||||||
|
this.tryCellMove(new Coord(x, i), new Coord(x + 1, i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (move.equals(Move.DOWN)) {
|
||||||
|
for (int y = this.sz - 2; y > 0; y--) {
|
||||||
|
this.tryCellMove(new Coord(i, y), new Coord(i , y + 1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -155,6 +201,10 @@ abstract class Cell {
|
|||||||
Util.scale, Util.scale, OutlineMode.SOLID, this.col()
|
Util.scale, Util.scale, OutlineMode.SOLID, this.col()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Is this moveable?
|
||||||
|
boolean isMoveable() { return false; }
|
||||||
|
boolean isReplaceable() { return true;}
|
||||||
}
|
}
|
||||||
|
|
||||||
// A moveable tile on the board.
|
// A moveable tile on the board.
|
||||||
@@ -176,6 +226,9 @@ class Tile extends Cell {
|
|||||||
super.draw()
|
super.draw()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
boolean isMoveable() { return true;}
|
||||||
|
boolean isReplaceable() {return false;}
|
||||||
}
|
}
|
||||||
|
|
||||||
// An empty space on the board.
|
// An empty space on the board.
|
||||||
|
|||||||
Reference in New Issue
Block a user