From d7efde25761bad8670595537d1c645b0c117b642 Mon Sep 17 00:00:00 2001 From: Jacob Signorovitch Date: Wed, 26 Mar 2025 17:51:16 -0400 Subject: [PATCH] Updated. --- .../src/twentyfortyeight/Main.java | 81 ++++++++++++++++--- 1 file changed, 71 insertions(+), 10 deletions(-) diff --git a/twentyfortyeight/src/twentyfortyeight/Main.java b/twentyfortyeight/src/twentyfortyeight/Main.java index 07176db..19c4f13 100644 --- a/twentyfortyeight/src/twentyfortyeight/Main.java +++ b/twentyfortyeight/src/twentyfortyeight/Main.java @@ -2,8 +2,36 @@ package twentyfortyeight; import java.util.ArrayList; import java.util.List; +import java.util.Random; import tester.Tester; +// A game of 2048. +class Game { + int score; + Grid grid; + Random rand; + + Game(int score, Grid grid, Random rand) { + this.score = score; + this.grid = grid; + this.rand = rand; + } + + // Convenience constructor for init with default settings. + Game() { + this.grid = new Grid(4, 4); + this.score = 0; + this.rand = new Random(); + } + + // Convenience constructor for init with custom settings. + Game(int w, int h, int seed) { + this.grid = new Grid(w, h); + this.score = 0; + this.rand = new Random(seed); + } +} + // The grid in which the game is played. class Grid { int w; // The width of the grid. @@ -15,32 +43,43 @@ class Grid { if (w < 1 || h < 1) throw new IllegalArgumentException("Can't make grid that small."); - this.sz = w * h; + this.w = w; + this.h = h; + this.sz = this.w * this.h; this.buf = new ArrayList<>(this.sz); - for (int i = 0; i < this.sz; i++) this.buf.add(null); + // Fill with zeros. + for (int i = 0; i < this.sz; this.buf.add(i++ * 0)); } - // Get the indexes of all "free" cells -- those whose value in buf is null, - // and are a blank tile. + // Get the indexes of all "free" cells -- those whose value in buf is 0, + // and are represented as blank tile. List freeCellIdxs() { - List nulls = new ArrayList<>(); + List free = new ArrayList<>(); for (int i = 0; i < this.sz; i++) - if (this.buf.get(i) == null && nulls.add(i)) continue; + if (this.buf.get(i) == 0 && free.add(i)) continue; - return nulls; + return free; } + + // Set the tile at the coords. Just for testing. + Grid set(int x, int y, int v) { + this.buf.set(this.w * y + x, v); + return this; + } + + // Get the value at the coords. Just for testing. + int get(int x, int y) { return this.buf.get(this.w * y + x); } } class Examples { - Grid verySmall; - Grid small; - Grid oblong; + Grid verySmall, small, oblong, regular; void init() { verySmall = new Grid(1, 1); small = new Grid(2, 2); oblong = new Grid(1, 3); + regular = new Grid(4, 4); } void testGridFreeCellIdxs(Tester t) { @@ -55,4 +94,26 @@ class Examples { oblong.freeCellIdxs(), new ArrayList(List.of(0, 1, 2)) ); } + + void testGridSet(Tester t) { + init(); + small.set(0, 0, 1); + t.checkExpect(small.freeCellIdxs(), new ArrayList<>(List.of(1, 2, 3))); + small.set(1, 1, 1); + t.checkExpect(small.freeCellIdxs(), new ArrayList<>(List.of(1, 2))); + } + + void testRegular(Tester t) { + init(); + regular.set(1, 0, 16) + .set(2, 0, 16) + .set(3, 0, 32) + .set(0, 1, 4) + .set(1, 1, 4) + .set(4, 1, 2); + + t.checkExpect(regular.get(1, 0), 16); + t.checkExpect(regular.get(0, 0), 0); + t.checkExpect(regular.get(3, 3), 0); + } }