Added twentyfortyeight.

This commit is contained in:
Jacob Signorovitch
2025-03-26 13:19:04 -04:00
parent 3fb6413bec
commit 405f5bceaa
4 changed files with 99 additions and 0 deletions

28
twentyfortyeight/.project Normal file
View File

@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>twentyfortyeight</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
<filteredResources>
<filter>
<id>1743009076709</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>

View File

@@ -0,0 +1,2 @@
eclipse.preferences.version=1
encoding/<project>=UTF-8

View File

@@ -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

View File

@@ -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<Integer> 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<Integer> freeCellIdxs() {
List<Integer> 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<Integer>(List.of(0, 1, 2, 3))
);
t.checkExpect(
verySmall.freeCellIdxs(), new ArrayList<Integer>(List.of(0))
);
t.checkExpect(
oblong.freeCellIdxs(), new ArrayList<Integer>(List.of(0, 1, 2))
);
}
}