From f66bfd660cd411df8a62c2e3f892e55ae57e20f5 Mon Sep 17 00:00:00 2001 From: Jacob Signorovitch Date: Mon, 7 Apr 2025 09:09:06 -0400 Subject: [PATCH] NOTHING WORKS AAAAAAAAAAAAAAAAAHHHHHHHHHHHHHHH --- .../src/twentyfortyeight/Main.java | 76 ++++++++++++++++--- 1 file changed, 65 insertions(+), 11 deletions(-) diff --git a/twentyfortyeight/src/twentyfortyeight/Main.java b/twentyfortyeight/src/twentyfortyeight/Main.java index 1d765c2..0221cab 100644 --- a/twentyfortyeight/src/twentyfortyeight/Main.java +++ b/twentyfortyeight/src/twentyfortyeight/Main.java @@ -1,33 +1,52 @@ package twentyfortyeight; +import java.awt.Color; import java.util.ArrayList; import java.util.List; import java.util.Random; import javalib.impworld.*; +import javalib.worldimages.*; 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. class Game extends World { + static int SCALE = 100; + int score; Grid grid; Random rand; WorldScene scene; - - Game(int score, Grid grid, Random rand, WorldScene scene) { - this.score = score; - this.grid = grid; - this.rand = rand; - this.scene = scene; - } + int w, h; // The pixel widths and heights. // Convenience constructor for init with default settings. Game() { this.grid = new Grid(4, 4); this.score = 0; this.rand = new Random(); - this.scene = new WorldScene( - this.grid.w, this.grid.h - ); // TODO: Add proper scaling parameter. + this.w = this.grid.w * SCALE; + this.h = this.grid.h * SCALE; + this.scene = new WorldScene(this.w, this.h); } // 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); } - 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. @@ -87,11 +123,22 @@ class Grid { // Assume right. 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 { Grid verySmall, small, oblong, regular; + Game game; + void init() { verySmall = new Grid(1, 1); small = new Grid(2, 2); @@ -134,6 +181,7 @@ class Examples { t.checkExpect(regular.get(3, 3), 0); } + /* void testMoveLeft(Tester t) { init(); regular.set(1, 0, 16) @@ -151,4 +199,10 @@ class Examples { t.checkExpect(regular.get(2, 1), 8); t.checkExpect(regular.get(3, 1), 2); } + */ + + void testGame(Tester t) { + game = new Game(); + game.launchGame(); + } }