NOTHING WORKS AAAAAAAAAAAAAAAAAHHHHHHHHHHHHHHH
This commit is contained in:
@@ -1,33 +1,52 @@
|
|||||||
package twentyfortyeight;
|
package twentyfortyeight;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import javalib.impworld.*;
|
import javalib.impworld.*;
|
||||||
|
import javalib.worldimages.*;
|
||||||
import tester.Tester;
|
import tester.Tester;
|
||||||
|
|
||||||
|
class ImgUtil {
|
||||||
|
static Color color(int n) {
|
||||||
|
int hash = Integer.hashCode(n);
|
||||||
|
int r, g, b;
|
||||||
|
|
||||||
|
// Shift the hash so the least significant byte is the part you want,
|
||||||
|
// and mask to isolate that byte.
|
||||||
|
r = (hash >> 16) & 0xff;
|
||||||
|
g = (hash >> 8) & 0xff;
|
||||||
|
b = (hash) & 0xff;
|
||||||
|
|
||||||
|
return new Color(r, g, b);
|
||||||
|
}
|
||||||
|
|
||||||
|
static WorldImage mktile(int n) {
|
||||||
|
return new RectangleImage(
|
||||||
|
Game.SCALE, Game.SCALE, OutlineMode.SOLID, ImgUtil.color(n)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// A game of 2048.
|
// A game of 2048.
|
||||||
class Game extends World {
|
class Game extends World {
|
||||||
|
static int SCALE = 100;
|
||||||
|
|
||||||
int score;
|
int score;
|
||||||
Grid grid;
|
Grid grid;
|
||||||
Random rand;
|
Random rand;
|
||||||
WorldScene scene;
|
WorldScene scene;
|
||||||
|
int w, h; // The pixel widths and heights.
|
||||||
Game(int score, Grid grid, Random rand, WorldScene scene) {
|
|
||||||
this.score = score;
|
|
||||||
this.grid = grid;
|
|
||||||
this.rand = rand;
|
|
||||||
this.scene = scene;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Convenience constructor for init with default settings.
|
// Convenience constructor for init with default settings.
|
||||||
Game() {
|
Game() {
|
||||||
this.grid = new Grid(4, 4);
|
this.grid = new Grid(4, 4);
|
||||||
this.score = 0;
|
this.score = 0;
|
||||||
this.rand = new Random();
|
this.rand = new Random();
|
||||||
this.scene = new WorldScene(
|
this.w = this.grid.w * SCALE;
|
||||||
this.grid.w, this.grid.h
|
this.h = this.grid.h * SCALE;
|
||||||
); // TODO: Add proper scaling parameter.
|
this.scene = new WorldScene(this.w, this.h);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convenience constructor for init with custom settings.
|
// Convenience constructor for init with custom settings.
|
||||||
@@ -38,7 +57,24 @@ class Game extends World {
|
|||||||
this.scene = new WorldScene(this.grid.w, this.grid.h);
|
this.scene = new WorldScene(this.grid.w, this.grid.h);
|
||||||
}
|
}
|
||||||
|
|
||||||
public WorldScene makeScene() { return null; }
|
void launchGame() { this.bigBang(this.w, this.h, 0.01); }
|
||||||
|
|
||||||
|
public WorldScene makeScene() {
|
||||||
|
this.scene = this.getEmptyScene();
|
||||||
|
this.draw();
|
||||||
|
return this.scene;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onKeyEvent(String key) {
|
||||||
|
System.out.println("YES");
|
||||||
|
if (key == "h") { this.grid.set(0, 0, 8); }
|
||||||
|
this.draw();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw current game state.
|
||||||
|
void draw() {
|
||||||
|
this.scene.placeImageXY(this.grid.render(), this.w / 2, this.h / 2);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// The grid in which the game is played.
|
// The grid in which the game is played.
|
||||||
@@ -87,11 +123,22 @@ class Grid {
|
|||||||
// Assume right.
|
// Assume right.
|
||||||
for (int i = 0; i < this.h; i++) {}
|
for (int i = 0; i < this.h; i++) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Render the grid.
|
||||||
|
WorldImage render() {
|
||||||
|
WorldImage bg = ImgUtil.mktile(this.buf.get(0));
|
||||||
|
for (int i = 0; i < this.h) {
|
||||||
|
for (int j = 0; j < this.w) {}
|
||||||
|
}
|
||||||
|
return bg;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Examples {
|
class Examples {
|
||||||
Grid verySmall, small, oblong, regular;
|
Grid verySmall, small, oblong, regular;
|
||||||
|
|
||||||
|
Game game;
|
||||||
|
|
||||||
void init() {
|
void init() {
|
||||||
verySmall = new Grid(1, 1);
|
verySmall = new Grid(1, 1);
|
||||||
small = new Grid(2, 2);
|
small = new Grid(2, 2);
|
||||||
@@ -134,6 +181,7 @@ class Examples {
|
|||||||
t.checkExpect(regular.get(3, 3), 0);
|
t.checkExpect(regular.get(3, 3), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
void testMoveLeft(Tester t) {
|
void testMoveLeft(Tester t) {
|
||||||
init();
|
init();
|
||||||
regular.set(1, 0, 16)
|
regular.set(1, 0, 16)
|
||||||
@@ -151,4 +199,10 @@ class Examples {
|
|||||||
t.checkExpect(regular.get(2, 1), 8);
|
t.checkExpect(regular.get(2, 1), 8);
|
||||||
t.checkExpect(regular.get(3, 1), 2);
|
t.checkExpect(regular.get(3, 1), 2);
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
void testGame(Tester t) {
|
||||||
|
game = new Game();
|
||||||
|
game.launchGame();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user