These
This commit is contained in:
@@ -4,6 +4,7 @@ import tester.Tester;
|
|||||||
|
|
||||||
class Examples {
|
class Examples {
|
||||||
void testItAll(Tester t) { new Game().run(); }
|
void testItAll(Tester t) { new Game().run(); }
|
||||||
|
|
||||||
void testCoord(Tester t) {
|
void testCoord(Tester t) {
|
||||||
Coord coord = new Coord(1, 1);
|
Coord coord = new Coord(1, 1);
|
||||||
Coord cod = new Coord(1, 1);
|
Coord cod = new Coord(1, 1);
|
||||||
|
@@ -37,9 +37,10 @@ class Util {
|
|||||||
// Convert a movement to a displacement vector.
|
// Convert a movement to a displacement vector.
|
||||||
static 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);
|
||||||
else return new Coord(0, 1);
|
else if (move.equals(Move.RIGHT)) return new Coord(1, 0);
|
||||||
|
else return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,9 +86,6 @@ class Game extends World {
|
|||||||
void run() { this.bigBang(this.width, this.width, 0.01); }
|
void run() { this.bigBang(this.width, this.width, 0.01); }
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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.
|
||||||
@@ -150,13 +148,12 @@ class Board {
|
|||||||
// Move cell as much as possible.
|
// Move cell as much as possible.
|
||||||
void fullCellMove(Coord coord, Move move) {
|
void fullCellMove(Coord coord, Move move) {
|
||||||
Cell cell = this.get(coord);
|
Cell cell = this.get(coord);
|
||||||
Coord current = coord;
|
Coord disp = Util.moveDisp(move);
|
||||||
|
|
||||||
if (!cell.isMoveable()) return;
|
if (!cell.isMoveable()) return;
|
||||||
|
|
||||||
while (this.get(current.add(Util.moveDisp(move))).isReplaceable()) {
|
for () {
|
||||||
this.tryCellMove(current, current.add(Util.moveDisp(move)));
|
this.tryCellMove(coord, coord.add(disp));
|
||||||
current = current.add(Util.moveDisp(move));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -177,8 +174,8 @@ class Board {
|
|||||||
}
|
}
|
||||||
|
|
||||||
else if (move.equals(Move.RIGHT)) {
|
else if (move.equals(Move.RIGHT)) {
|
||||||
for (int x = 0; x < this.sz - 1; x++) {
|
for (int x = this.sz - 2; x >= 0; x--) {
|
||||||
this.tryCellMove(new Coord(x, i), new Coord(x + 1, i));
|
this.fullCellMove(new Coord(x, i), move);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -190,6 +187,9 @@ class Board {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The possible movements.
|
||||||
|
enum Move { UP, LEFT, RIGHT, DOWN }
|
||||||
|
|
||||||
// A cell on the board.
|
// A cell on the board.
|
||||||
abstract class Cell {
|
abstract class Cell {
|
||||||
// Generate the color of the tile.
|
// Generate the color of the tile.
|
||||||
@@ -204,7 +204,7 @@ abstract class Cell {
|
|||||||
|
|
||||||
// Is this moveable?
|
// Is this moveable?
|
||||||
boolean isMoveable() { return false; }
|
boolean isMoveable() { return false; }
|
||||||
boolean isReplaceable() { return true;}
|
boolean isReplaceable() { return false;}
|
||||||
}
|
}
|
||||||
|
|
||||||
// A moveable tile on the board.
|
// A moveable tile on the board.
|
||||||
@@ -234,6 +234,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.LIGHT_GRAY; }
|
Color col() { return Color.LIGHT_GRAY; }
|
||||||
|
boolean isReplaceable() { return true; }
|
||||||
}
|
}
|
||||||
|
|
||||||
// Board coordinates.
|
// Board coordinates.
|
||||||
|
Reference in New Issue
Block a user