From 405f5bceaa16be2fc4f99df6eab10967be6da4b8 Mon Sep 17 00:00:00 2001 From: Jacob Signorovitch Date: Wed, 26 Mar 2025 13:19:04 -0400 Subject: [PATCH] Added twentyfortyeight. --- twentyfortyeight/.project | 28 +++++++++ .../org.eclipse.core.resources.prefs | 2 + .../.settings/org.eclipse.jdt.core.prefs | 11 ++++ .../src/twentyfortyeight/Main.java | 58 +++++++++++++++++++ 4 files changed, 99 insertions(+) create mode 100644 twentyfortyeight/.project create mode 100644 twentyfortyeight/.settings/org.eclipse.core.resources.prefs create mode 100644 twentyfortyeight/.settings/org.eclipse.jdt.core.prefs create mode 100644 twentyfortyeight/src/twentyfortyeight/Main.java diff --git a/twentyfortyeight/.project b/twentyfortyeight/.project new file mode 100644 index 0000000..861e561 --- /dev/null +++ b/twentyfortyeight/.project @@ -0,0 +1,28 @@ + + + twentyfortyeight + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + + + 1743009076709 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + + diff --git a/twentyfortyeight/.settings/org.eclipse.core.resources.prefs b/twentyfortyeight/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..99f26c0 --- /dev/null +++ b/twentyfortyeight/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/twentyfortyeight/.settings/org.eclipse.jdt.core.prefs b/twentyfortyeight/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..9a7984b --- /dev/null +++ b/twentyfortyeight/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=21 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=21 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=21 diff --git a/twentyfortyeight/src/twentyfortyeight/Main.java b/twentyfortyeight/src/twentyfortyeight/Main.java new file mode 100644 index 0000000..07176db --- /dev/null +++ b/twentyfortyeight/src/twentyfortyeight/Main.java @@ -0,0 +1,58 @@ +package twentyfortyeight; + +import java.util.ArrayList; +import java.util.List; +import tester.Tester; + +// The grid in which the game is played. +class Grid { + int w; // The width of the grid. + int h; // The height of the grid. + ArrayList buf; // The buffer containing the tiles. + int sz; // The size of the buffer -- always w * h. + + Grid(int w, int h) { + if (w < 1 || h < 1) + throw new IllegalArgumentException("Can't make grid that small."); + + this.sz = w * h; + this.buf = new ArrayList<>(this.sz); + + for (int i = 0; i < this.sz; i++) this.buf.add(null); + } + + // Get the indexes of all "free" cells -- those whose value in buf is null, + // and are a blank tile. + List freeCellIdxs() { + List nulls = new ArrayList<>(); + for (int i = 0; i < this.sz; i++) + if (this.buf.get(i) == null && nulls.add(i)) continue; + + return nulls; + } +} + +class Examples { + Grid verySmall; + Grid small; + Grid oblong; + + void init() { + verySmall = new Grid(1, 1); + small = new Grid(2, 2); + oblong = new Grid(1, 3); + } + + void testGridFreeCellIdxs(Tester t) { + init(); + t.checkExpect( + small.freeCellIdxs(), new ArrayList(List.of(0, 1, 2, 3)) + ); + t.checkExpect( + verySmall.freeCellIdxs(), new ArrayList(List.of(0)) + ); + t.checkExpect( + oblong.freeCellIdxs(), new ArrayList(List.of(0, 1, 2)) + ); + } +}