From 8ac2d02555e95ad3ea8e2be390d2b30b9e7ba0b6 Mon Sep 17 00:00:00 2001 From: Katie Date: Sat, 15 Mar 2025 18:36:14 -0400 Subject: [PATCH 1/3] added click-3x3 feature --- .../command/MinesweeperCommand.java | 61 ++++++++++--------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/src/main/java/net/earthcomputer/clientcommands/command/MinesweeperCommand.java b/src/main/java/net/earthcomputer/clientcommands/command/MinesweeperCommand.java index c9cd1659..8feda9c5 100644 --- a/src/main/java/net/earthcomputer/clientcommands/command/MinesweeperCommand.java +++ b/src/main/java/net/earthcomputer/clientcommands/command/MinesweeperCommand.java @@ -5,6 +5,7 @@ import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; +import it.unimi.dsi.fastutil.ints.IntArrayList; import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; import net.minecraft.client.gui.GuiGraphics; import net.minecraft.client.gui.screens.Screen; @@ -19,6 +20,7 @@ import org.joml.Vector2i; import java.util.Random; +import java.util.stream.Stream; import static com.mojang.brigadier.arguments.IntegerArgumentType.*; import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.*; @@ -219,13 +221,18 @@ public boolean mouseReleased(double mouseX, double mouseY, int button) { int tileY = Mth.floorDiv((int) (mouseY - topLeftY - 12), 16); if (isWithinBounds(tileX, tileY) && gameActive()) { + byte tile = getTile(tileX, tileY); if (button == InputConstants.MOUSE_BUTTON_LEFT) { if (ticksPlaying == 0) { generateMines(tileX, tileY); ticksPlaying = 1; } - click(tileX, tileY); + if (isCovered(tile)) { + click(tileX, tileY); + } else { + click3x3(tileX, tileY); + } assert minecraft != null && minecraft.player != null; if (emptyTilesRemaining <= 0) { @@ -290,6 +297,14 @@ private boolean isWithinBounds(int x, int y) { return 0 <= x && x < boardWidth && 0 <= y && y < boardHeight; } + private Stream getNeighbours(int x, int y) { + return Stream.of( + new Vector2i(x - 1, y - 1), new Vector2i(x, y - 1), new Vector2i(x + 1, y - 1), + new Vector2i(x - 1, y), new Vector2i(x + 1, y), + new Vector2i(x - 1, y + 1), new Vector2i(x, y + 1), new Vector2i(x + 1, y + 1) + ).filter(pos -> isWithinBounds(pos.x, pos.y)); + } + private void click(int x, int y) { byte tile = getTile(x, y); if (!isCovered(tile) || isFlagged(tile)) { @@ -307,41 +322,31 @@ private void click(int x, int y) { uncover(x, y); emptyTilesRemaining--; // we need to leave room for the current tile in the queue - int[] queue = new int[emptyTilesRemaining + 1]; - int queueIdx = 0; - queue[0] = y * boardWidth + x; - while (queueIdx >= 0) { - int idx = queue[queueIdx--]; + IntArrayList queue = new IntArrayList(emptyTilesRemaining + 1); + queue.add(y * boardWidth + x); + while (!queue.isEmpty()) { + int idx = queue.popInt(); int xPart = idx % boardWidth; int yPart = idx / boardWidth; - for (Vector2i possibleNeighbour : new Vector2i[]{ - new Vector2i(xPart - 1, yPart - 1), - new Vector2i(xPart, yPart - 1), - new Vector2i(xPart + 1, yPart - 1), - - new Vector2i(xPart - 1, yPart), - new Vector2i(xPart + 1, yPart), - - new Vector2i(xPart - 1, yPart + 1), - new Vector2i(xPart, yPart + 1), - new Vector2i(xPart + 1, yPart + 1), - }) { - if (isWithinBounds(possibleNeighbour.x, possibleNeighbour.y)) { - byte value = getTile(possibleNeighbour.x, possibleNeighbour.y); - uncover(possibleNeighbour.x, possibleNeighbour.y); - if (isCovered(value)) { - emptyTilesRemaining--; - // if it's an empty tile, we put it in the queue to go activate all its neighbours - if (tileType(value) == EMPTY_TILE_TYPE) { - queue[++queueIdx] = possibleNeighbour.y * boardWidth + possibleNeighbour.x; - } + getNeighbours(xPart, yPart).forEach(neighbour -> { + byte value = getTile(neighbour.x, neighbour.y); + uncover(neighbour.x, neighbour.y); + if (isCovered(value)) { + emptyTilesRemaining--; + // if it's an empty tile, we put it in the queue to go activate all its neighbours + if (tileType(value) == EMPTY_TILE_TYPE) { + queue.add(neighbour.y * boardWidth + neighbour.x); } } - } + }); } } } + private void click3x3(int x, int y) { + getNeighbours(x, y).forEach(neighbour -> click(neighbour.x, neighbour.y)); + } + private void flag(int x, int y) { if (!isCovered(getTile(x, y))) { return; From a9aee76b68f372c5e1c4e2860b5aea6a3ad2843c Mon Sep 17 00:00:00 2001 From: Katie <72476111+RealRTTV@users.noreply.github.com> Date: Thu, 20 Mar 2025 15:35:45 -0400 Subject: [PATCH 2/3] Fixed MinesweeperCommand.java Spacing --- .../clientcommands/command/MinesweeperCommand.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/earthcomputer/clientcommands/command/MinesweeperCommand.java b/src/main/java/net/earthcomputer/clientcommands/command/MinesweeperCommand.java index 8feda9c5..b4b235bd 100644 --- a/src/main/java/net/earthcomputer/clientcommands/command/MinesweeperCommand.java +++ b/src/main/java/net/earthcomputer/clientcommands/command/MinesweeperCommand.java @@ -299,9 +299,9 @@ private boolean isWithinBounds(int x, int y) { private Stream getNeighbours(int x, int y) { return Stream.of( - new Vector2i(x - 1, y - 1), new Vector2i(x, y - 1), new Vector2i(x + 1, y - 1), - new Vector2i(x - 1, y), new Vector2i(x + 1, y), - new Vector2i(x - 1, y + 1), new Vector2i(x, y + 1), new Vector2i(x + 1, y + 1) + new Vector2i(x - 1, y - 1), new Vector2i(x , y - 1), new Vector2i(x + 1, y - 1), + new Vector2i(x - 1, y), new Vector2i(x + 1, y), + new Vector2i(x - 1, y + 1), new Vector2i(x , y + 1), new Vector2i(x + 1, y + 1) ).filter(pos -> isWithinBounds(pos.x, pos.y)); } From 045d92fa13e00c9f135c29528ffb8cd1c2e92b42 Mon Sep 17 00:00:00 2001 From: Katie <72476111+RealRTTV@users.noreply.github.com> Date: Thu, 20 Mar 2025 15:37:28 -0400 Subject: [PATCH 3/3] Update MinesweeperCommand.java --- .../clientcommands/command/MinesweeperCommand.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/earthcomputer/clientcommands/command/MinesweeperCommand.java b/src/main/java/net/earthcomputer/clientcommands/command/MinesweeperCommand.java index b4b235bd..dc1a2494 100644 --- a/src/main/java/net/earthcomputer/clientcommands/command/MinesweeperCommand.java +++ b/src/main/java/net/earthcomputer/clientcommands/command/MinesweeperCommand.java @@ -299,9 +299,9 @@ private boolean isWithinBounds(int x, int y) { private Stream getNeighbours(int x, int y) { return Stream.of( - new Vector2i(x - 1, y - 1), new Vector2i(x , y - 1), new Vector2i(x + 1, y - 1), - new Vector2i(x - 1, y), new Vector2i(x + 1, y), - new Vector2i(x - 1, y + 1), new Vector2i(x , y + 1), new Vector2i(x + 1, y + 1) + new Vector2i(x - 1, y - 1), new Vector2i(x, y - 1), new Vector2i(x + 1, y - 1), + new Vector2i(x - 1, y), new Vector2i(x + 1, y), + new Vector2i(x - 1, y + 1), new Vector2i(x, y + 1), new Vector2i(x + 1, y + 1) ).filter(pos -> isWithinBounds(pos.x, pos.y)); }