aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/me/Danker/utils/BoulderUtils.java
blob: 2d437d02aa6d3d88f0a69527d8113f258bc85a9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package me.Danker.utils;

import me.Danker.features.puzzlesolvers.BoulderSolver;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.BlockPos;
import net.minecraft.util.Vec3;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

public class BoulderUtils {

    public static HashSet<String> seenBoardStates = new HashSet<>();
    public static long iterations = 0;
    public static int fastestSolution = 10;

    /*
    CC BY-SA 4.0
    Unmodified
    Question: https://stackoverflow.com/questions/5617016/
    Answer: https://stackoverflow.com/a/53397359
    Author: https://stackoverflow.com/users/3647002/gayan-weerakutti
    */
    public static char[][] copy(char[][] array) {
        return Arrays.stream(array).map(char[]::clone).toArray(char[][]::new);
    }

    public static char[][] flipVertically(char[][] board) {
        char[][] newBoard = new char[7][7];

        for (int row = 0; row < 7; row++) {
            System.arraycopy(board[6 - row], 0, newBoard[row], 0, 7);
        }

        return newBoard;
    }

    public static char[][] flipHorizontally(char[][] board) {
        char[][] newBoard = new char[7][7];

        for (int row = 0; row < 7; row++) {
            for (int column = 0; column < 7; column++) {
                newBoard[row][column] = board[row][6 - column];
            }
        }

        return newBoard;
    }

    public static char[][] rotateClockwise(char[][] board) {
        char[][] newBoard = new char[7][7];

        for (int row = 0; row < 7; row++) {
            for (int column = 0; column < 7; column++) {
                newBoard[column][6 - row] = board[row][column];
            }
        }

        return newBoard;
    }

    public static char[][] removeFirstRow(char[][] board) {
        List<char[]> list = new ArrayList<>(Arrays.asList(board));
        list.remove(0);
        return list.toArray(new char[][]{});
    }

    public static String toString(char[][] board) {
        StringBuilder sb = new StringBuilder();
        for (char[] row : board) {
            for (char column : row) {
                sb.append(column);
            }
        }
        return sb.toString();
    }

    public static int findSolution(char[][] board, int depth, List<int[]> route) {
        if (depth > 9) return 10;
        String boardString = toString(board);
        if (seenBoardStates.contains(boardString)) return 10; // this line turns 600 million iterations to 700 thousand
        seenBoardStates.add(boardString);
        if (hasOpenPath(board)) return depth;

        char[][] floodFilledBoard = floodFillBottom(board);
        List<int[]> newRoute = new ArrayList<>(route);
        int solutionLength = 10;
        int bestRow = -1;
        int bestColumn = -1;
        int bestDirection = -1;
        for (int row = 0; row < 6; row++) {
            for (int column = 0; column < 7; column++) {
                iterations++;
                if (floodFilledBoard[row][column] == 'X' && isTouchingOpenSpace(floodFilledBoard, row, column)) {
                    if (canBePushed(floodFilledBoard, row, column, 'u')) {
                        int solution = findSolution(push(board, row, column, 'u'), depth + 1, add(newRoute, new int[]{row, column, 1}));
                        if (solution < solutionLength) {
                            solutionLength = solution;
                            bestRow = row;
                            bestColumn = column;
                            bestDirection = 1;
                        }
                    }
                    if (canBePushed(floodFilledBoard, row, column, 'd')) {
                        int solution = findSolution(push(board, row, column, 'd'), depth + 1, add(newRoute, new int[]{row, column, 2}));
                        if (solution < solutionLength) {
                            solutionLength = solution;
                            bestRow = row;
                            bestColumn = column;
                            bestDirection = 2;
                        }
                    }
                    if (canBePushed(floodFilledBoard, row, column, 'l')) {
                        int solution = findSolution(push(board, row, column, 'l'), depth + 1, add(newRoute, new int[]{row, column, 3}));
                        if (solution < solutionLength) {
                            solutionLength = solution;
                            bestRow = row;
                            bestColumn = column;
                            bestDirection = 3;
                        }
                    }
                    if (canBePushed(floodFilledBoard, row, column, 'r')) {
                        int solution = findSolution(push(board, row, column, 'r'), depth + 1, add(newRoute, new int[]{row, column, 4}));
                        if (solution < solutionLength) {
                            solutionLength = solution;
                            bestRow = row;
                            bestColumn = column;
                            bestDirection = 4;
                        }
                    }
                }
            }
        }

        if (bestRow != -1) {
            newRoute.add(0, new int[]{bestRow, bestColumn, bestDirection});
        }
        if (solutionLength < fastestSolution) {
            fastestSolution = solutionLength;
            newRoute.add(newRoute.remove(0)); // dont know why the last one goes first but this fixes it
            BoulderSolver.route = new ArrayList<>(newRoute);
        }
        return solutionLength;
    }

    public static boolean canBePushed(char[][] floodFilledBoard, int row, int column, char direction) {
        switch (direction) {
            case 'u':
                if (row > 0 && row < 5 && floodFilledBoard[row - 1][column] != 'X' && floodFilledBoard[row + 1][column] == 'O') return true;
                break;
            case 'd':
                if (row > 0 && row < 5 && floodFilledBoard[row - 1][column] == 'O' && floodFilledBoard[row + 1][column] != 'X') return true;
                break;
            case 'l':
                if (column > 0 && column < 6 && floodFilledBoard[row][column - 1] != 'X' && floodFilledBoard[row][column + 1] == 'O') return true;
                break;
            case 'r':
                if (column > 0 && column < 6 && floodFilledBoard[row][column - 1] == 'O' && floodFilledBoard[row][column + 1] != 'X') return true;
                break;
        }
        return false;
    }

    public static char[][] push(char[][] board, int row, int column, char direction) {
        char[][] newBoard = copy(board);
        switch (direction) {
            case 'u':
                newBoard[row - 1][column] = 'X';
                break;
            case 'd':
                newBoard[row + 1][column] = 'X';
                break;
            case 'l':
                newBoard[row][column - 1] = 'X';
                break;
            case 'r':
                newBoard[row][column + 1] = 'X';
        }
        newBoard[row][column] = '\0';
        return newBoard;
    }

    public static boolean isTouchingOpenSpace(char[][] floodFilledBoard, int row, int column) {
        return (row > 0 && floodFilledBoard[row - 1][column] == 'O') ||
                (row < 5 && floodFilledBoard[row + 1][column] == 'O') ||
                (column > 0 && floodFilledBoard[row][column - 1] == 'O') ||
                (column < 6 && floodFilledBoard[row][column + 1] == 'O');
    }

    public static boolean hasOpenPath(char[][] board) {
        char[][] newBoard = floodFillBottom(copy(board));
        // Check if flood fill reached top
        for (int column = 0; column < 7; column++) {
            if (newBoard[0][column] == 'O') return true;
        }
        return false;
    }

    public static char[][] floodFillBottom(char[][] board) {
        char[][] newBoard = copy(board);
        for (int column = 0; column < 7; column++) {
            if (newBoard[5][column] == '\0') {
                newBoard = floodFill(newBoard, 5, column);
            }
        }
        return newBoard;
    }

    public static char[][] floodFill(char[][] board, int row, int column) {
        if (row < 0 || row > 5 || column < 0 || column > 6) return board;
        if (board[row][column] == '\0') {
            board[row][column] = 'O';
            floodFill(board, row - 1, column);
            floodFill(board, row + 1, column);
            floodFill(board, row, column - 1);
            floodFill(board, row, column + 1);
            return board;
        }
        return board;
    }

    public static List<int[]> add(List<int[]> list, int[] arrayToAdd) {
        List<int[]> newList = new ArrayList<>(list);
        newList.add(arrayToAdd);
        return newList;
    }

    public static AxisAlignedBB getBoulder(int row, int column, BlockPos chestLocation, String boulderRoomDirection) {
        BlockPos boulderPosition;
        switch (boulderRoomDirection) {
            case "north":
                boulderPosition = chestLocation.add(3 * column - 10, -2, 3 * row + 4);
                break;
            case "east":
                boulderPosition = chestLocation.add(-3 * row - 6, -2, 3 * column - 10);
                break;
            case "south":
                boulderPosition = chestLocation.add(-3 * column + 8, -2, -3 * row - 6);
                break;
            case "west":
                boulderPosition = chestLocation.add(3 * row + 4, -2, -3 * column + 8);
                break;
            default:
                return null;
        }
        return new AxisAlignedBB(boulderPosition.getX() - 0.01, boulderPosition.getY() - 0.01, boulderPosition.getZ() - 0.01, boulderPosition.getX() + 3.01, boulderPosition.getY() + 3.01, boulderPosition.getZ() + 3.01);
    }

    public static void drawArrow(AxisAlignedBB aabb, String boulderRoomDirection, char direction, int colourInt, float partialTicks) {
        double averageX = (aabb.minX + aabb.maxX) / 2;
        double thirtyPercent = (aabb.maxX - aabb.minX) * 0.3;
        double averageZ = (aabb.minZ + aabb.maxZ) / 2;
        if (((boulderRoomDirection.equals("north") || boulderRoomDirection.equals("south")) && (direction == 'u' || direction == 'd')) ||
            ((boulderRoomDirection.equals("east") || boulderRoomDirection.equals("west")) && (direction == 'l' || direction == 'r'))) {
            Utils.draw3DLine(new Vec3(averageX, aabb.minY, aabb.minZ), new Vec3(averageX, aabb.minY, aabb.maxZ), colourInt, 10, false, partialTicks);
        } else {
            Utils.draw3DLine(new Vec3(aabb.minX, aabb.minY, averageZ), new Vec3(aabb.maxX, aabb.minY, averageZ), colourInt, 10, false, partialTicks);
        }

        if ((boulderRoomDirection.equals("north") && direction == 'u') || (boulderRoomDirection.equals("south") && direction == 'd') ||
            (boulderRoomDirection.equals("east") && direction == 'l') || (boulderRoomDirection.equals("west") && direction == 'r')) {
            Utils.draw3DLine(new Vec3(averageX, aabb.minY, aabb.minZ), new Vec3(aabb.minX + thirtyPercent, aabb.minY, aabb.minZ + thirtyPercent), colourInt, 10, false, partialTicks);
            Utils.draw3DLine(new Vec3(averageX, aabb.minY, aabb.minZ), new Vec3(aabb.maxX - thirtyPercent, aabb.minY, aabb.minZ + thirtyPercent), colourInt, 10, false, partialTicks);
        } else if ((boulderRoomDirection.equals("north") && direction == 'd') || (boulderRoomDirection.equals("south") && direction == 'u') ||
                    (boulderRoomDirection.equals("east") && direction == 'r') || (boulderRoomDirection.equals("west") && direction == 'l')) {
            Utils.draw3DLine(new Vec3(averageX, aabb.minY, aabb.maxZ), new Vec3(aabb.minX + thirtyPercent, aabb.minY, aabb.maxZ - thirtyPercent), colourInt, 10, false, partialTicks);
            Utils.draw3DLine(new Vec3(averageX, aabb.minY, aabb.maxZ), new Vec3(aabb.maxX - thirtyPercent, aabb.minY, aabb.maxZ - thirtyPercent), colourInt, 10, false, partialTicks);
        } else if ((boulderRoomDirection.equals("north") && direction == 'l') || (boulderRoomDirection.equals("south") && direction == 'r') ||
                    (boulderRoomDirection.equals("east") && direction == 'd') || (boulderRoomDirection.equals("west") && direction == 'u')) {
            Utils.draw3DLine(new Vec3(aabb.minX, aabb.minY, averageZ), new Vec3(aabb.minX + thirtyPercent, aabb.minY, aabb.minZ + thirtyPercent), colourInt, 10, false, partialTicks);
            Utils.draw3DLine(new Vec3(aabb.minX, aabb.minY, averageZ), new Vec3(aabb.minX + thirtyPercent, aabb.minY, aabb.maxZ - thirtyPercent), colourInt, 10, false, partialTicks);
        } else {
            Utils.draw3DLine(new Vec3(aabb.maxX, aabb.minY, averageZ), new Vec3(aabb.maxX - thirtyPercent, aabb.minY, aabb.minZ + thirtyPercent), colourInt, 10, false, partialTicks);
            Utils.draw3DLine(new Vec3(aabb.maxX, aabb.minY, averageZ), new Vec3(aabb.maxX - thirtyPercent, aabb.minY, aabb.maxZ - thirtyPercent), colourInt, 10, false, partialTicks);
        }
    }

}