Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tic Tac Toe Refactor #504

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class CreeperBeams extends DungeonPuzzle {

private static final int FLOOR_Y = 68;
private static final int BASE_Y = 74;
@SuppressWarnings("unused")
private static final CreeperBeams INSTANCE = new CreeperBeams();

private static ArrayList<Beam> beams = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class DungeonBlaze extends DungeonPuzzle {
private static final Logger LOGGER = LoggerFactory.getLogger(DungeonBlaze.class.getName());
private static final float[] GREEN_COLOR_COMPONENTS = {0.0F, 1.0F, 0.0F};
private static final float[] WHITE_COLOR_COMPONENTS = {1.0f, 1.0f, 1.0f};
@SuppressWarnings("unused")
private static final DungeonBlaze INSTANCE = new DungeonBlaze();

private static ArmorStandEntity highestBlaze = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package de.hysky.skyblocker.skyblock.dungeon.puzzle;

import de.hysky.skyblocker.config.SkyblockerConfigManager;
import de.hysky.skyblocker.skyblock.dungeon.secrets.DungeonManager;
import de.hysky.skyblocker.utils.Utils;
import de.hysky.skyblocker.utils.render.RenderHelper;
import de.hysky.skyblocker.utils.tictactoe.TicTacToeUtils;
import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderContext;
import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.client.MinecraftClient;
import net.minecraft.entity.decoration.ItemFrameEntity;
import net.minecraft.item.FilledMapItem;
import net.minecraft.item.map.MapState;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.Direction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -24,7 +22,8 @@
*/
public class TicTacToe extends DungeonPuzzle {
private static final Logger LOGGER = LoggerFactory.getLogger(TicTacToe.class);
private static final float[] RED_COLOR_COMPONENTS = {1.0F, 0.0F, 0.0F};
private static final float[] RED_COLOR_COMPONENTS = { 1.0F, 0.0F, 0.0F };
@SuppressWarnings("unused")
private static final TicTacToe INSTANCE = new TicTacToe();
private static Box nextBestMoveToMake = null;

Expand All @@ -50,56 +49,39 @@ public void tick(MinecraftClient client) {
List<ItemFrameEntity> itemFramesThatHoldMaps = client.world.getEntitiesByClass(ItemFrameEntity.class, searchBox, ItemFrameEntity::containsMap);

try {
//Only attempt to solve if its the player's turn
//Only attempt to solve if the puzzle wasn't just completed and if its the player's turn
if (itemFramesThatHoldMaps.size() != 9 && itemFramesThatHoldMaps.size() % 2 == 1) {
char[][] board = new char[3][3];
BlockPos leftmostRow = null;
int sign = 1;
char facing = 'X';

for (ItemFrameEntity itemFrame : itemFramesThatHoldMaps) {
MapState mapState = client.world.getMapState(FilledMapItem.getMapName(itemFrame.getMapId().getAsInt()));

if (mapState == null) continue;

int column = 0, row;
sign = 1;
//Surely if we pass shouldSolve then the room should be matched right
BlockPos relative = DungeonManager.getCurrentRoom().actualToRelative(itemFrame.getBlockPos());

//Find position of the item frame relative to where it is on the tic tac toe board
if (itemFrame.getHorizontalFacing() == Direction.SOUTH || itemFrame.getHorizontalFacing() == Direction.WEST) sign = -1;
BlockPos itemFramePos = BlockPos.ofFloored(itemFrame.getX(), itemFrame.getY(), itemFrame.getZ());
//Determine the row -- 72 = top, 71 = middle, 70 = bottom
int y = relative.getY();
int row = switch (y) {
case 72 -> 0;
case 71 -> 1;
case 70 -> 2;

for (int i = 2; i >= 0; i--) {
int realI = i * sign;
BlockPos blockPos = itemFramePos;
default -> -1;
};

if (itemFrame.getX() % 0.5 == 0) {
blockPos = itemFramePos.add(realI, 0, 0);
} else if (itemFrame.getZ() % 0.5 == 0) {
blockPos = itemFramePos.add(0, 0, realI);
facing = 'Z';
}
//Determine the column - 17 = first, 16 = second, 15 = third
int z = relative.getZ();
int column = switch (z) {
case 17 -> 0;
case 16 -> 1;
case 15 -> 2;

Block block = client.world.getBlockState(blockPos).getBlock();
if (block == Blocks.AIR || block == Blocks.STONE_BUTTON) {
leftmostRow = blockPos;
column = i;

break;
}
}

//Determine the row of the item frame
if (itemFrame.getY() == 72.5) {
row = 0;
} else if (itemFrame.getY() == 71.5) {
row = 1;
} else if (itemFrame.getY() == 70.5) {
row = 2;
} else {
continue;
}
default -> -1;
};

if (row == -1 || column == -1) continue;

//Get the color of the middle pixel of the map which determines whether its X or O
int middleColor = mapState.colors[8256] & 255;
Expand All @@ -109,17 +91,16 @@ public void tick(MinecraftClient client) {
} else if (middleColor == 33) {
board[row][column] = 'O';
}
}

int bestMove = TicTacToeUtils.getBestMove(board) - 1;
int bestMove = TicTacToeUtils.getBestMove(board) - 1;

if (leftmostRow != null) {
double drawX = facing == 'X' ? leftmostRow.getX() - sign * (bestMove % 3) : leftmostRow.getX();
double drawY = 72 - (double) (bestMove / 3);
double drawZ = facing == 'Z' ? leftmostRow.getZ() - sign * (bestMove % 3) : leftmostRow.getZ();
double nextX = 8;
double nextY = 72 - (double) (bestMove / 3);
double nextZ = 17 - (bestMove % 3);

nextBestMoveToMake = new Box(drawX, drawY, drawZ, drawX + 1, drawY + 1, drawZ + 1);
}
}
BlockPos nextPos = DungeonManager.getCurrentRoom().relativeToActual(BlockPos.ofFloored(nextX, nextY, nextZ));
nextBestMoveToMake = new Box(nextPos);
}
} catch (Exception e) {
LOGGER.error("[Skyblocker Tic Tac Toe] Encountered an exception while determining a tic tac toe solution!", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static int getBestMove(char[][] board) {
return Collections.max(moves.entrySet(), Map.Entry.comparingByValue()).getKey();
}

public static boolean hasMovesLeft(char[][] board) {
private static boolean hasMovesLeft(char[][] board) {
for (char[] rows : board) {
for (char col : rows) {
if (col == '\0') return true;
Expand All @@ -29,7 +29,7 @@ public static boolean hasMovesLeft(char[][] board) {
return false;
}

public static int getBoardRanking(char[][] board) {
private static int getBoardScore(char[][] board) {
for (int row = 0; row < 3; row++) {
if (board[row][0] == board[row][1] && board[row][0] == board[row][2]) {
if (board[row][0] == 'X') {
Expand Down Expand Up @@ -66,8 +66,9 @@ public static int getBoardRanking(char[][] board) {

return 0;
}
public static int alphabeta(char[][] board, int alpha, int beta, boolean max, int depth) {
int score = getBoardRanking(board);

private static int alphabeta(char[][] board, int alpha, int beta, boolean max, int depth) {
int score = getBoardScore(board);
if (score == 10 || score == -10) return score;
if (!hasMovesLeft(board)) return 0;

Expand Down