Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Getter
- public static class Game {
- private static final Random RANDOM = new Random();
- private final List<String> words;
- private final Map<String, List<Pair<Integer, Integer>>> wordLocations = new HashMap<>();
- private final int wordCount;
- private final long guildId, userId, channelId;
- private char[][] board;
- @Setter
- private long threadId;
- public Game(int wordCount, long guildId, long userId, long channelId) throws IllegalStateException {
- if (wordCount < 1)
- throw new IllegalStateException("Word count must be greater than 0!");
- this.wordCount = wordCount;
- this.words = getWords(wordCount);
- if (this.words.isEmpty())
- throw new IllegalStateException("Failed to get words for crossword!");
- this.guildId = guildId;
- this.userId = userId;
- this.channelId = channelId;
- this.board = new char[10][10];
- fillBoard();
- }
- private static List<String> getWords(int wordCount) {
- Either<List<String>, HttpStatus> response = ApiHandler.getWords(new RandomWordRequestData.Builder()
- .length(3, 10)
- .amount(wordCount)
- .build());
- if (response.isLeft())
- return List.copyOf(response.getLeft());
- else
- Constants.LOGGER.error("Failed to get words for crossword! Status: " + response.getRight());
- return List.of();
- }
- private boolean hasCharacter(int row, int column, char character) {
- return this.board[row][column] == character;
- }
- private boolean isEmpty(int row, int column) {
- return hasCharacter(row, column, '\u0000');
- }
- public boolean isOutsideBounds(int x, int y) {
- return y < 0 || y >= this.board.length || x < 0 || x >= this.board[y].length;
- }
- private void fillBoard() {
- List<String> sorted = this.words.stream().sorted(Comparator.comparingInt(String::length).reversed()).toList();
- String first = sorted.getFirst();
- while (first.length() >= this.board.length)
- expandBoard();
- int x = RANDOM.nextInt(this.board.length - first.length());
- int y = RANDOM.nextInt(this.board.length - first.length());
- boolean horizontal = RANDOM.nextBoolean();
- forcePlaceWord(first, x, y, horizontal);
- List<String> failed = new CopyOnWriteArrayList<>();
- for (int i = 1; i < sorted.size(); i++) {
- String word = sorted.get(i);
- Pair<Integer, Integer> location = findValidLocation(word);
- if (location == null) {
- failed.add(word);
- continue;
- }
- forcePlaceWord(word, location.getLeft(), location.getRight(), RANDOM.nextBoolean());
- }
- while (!failed.isEmpty()) {
- for (String word : failed) {
- Pair<Integer, Integer> location = findValidLocation(word);
- if (location == null) {
- continue;
- }
- failed.remove(word);
- forcePlaceWord(word, location.getLeft(), location.getRight(), RANDOM.nextBoolean());
- }
- }
- }
- private Pair<Integer, Integer> findValidLocation(String word) {
- for (Map.Entry<String, List<Pair<Integer, Integer>>> entry : wordLocations.entrySet()) {
- }
- return null; // No valid intersection found
- }
- private void forcePlaceWord(String word, int x, int y, boolean horizontal) {
- List<Pair<Integer, Integer>> locations = new ArrayList<>();
- if (horizontal) {
- for (int i = 0; i < word.length(); i++) {
- if (isOutsideBounds(x + i, y)) {
- expandBoard();
- }
- }
- for (int i = 0; i < word.length(); i++) {
- board[x + i][y] = word.charAt(i);
- locations.add(Pair.of(x + i, y));
- }
- } else {
- for (int i = 0; i < word.length(); i++) {
- if (isOutsideBounds(x, y + i)) {
- expandBoard();
- }
- }
- for (int i = 0; i < word.length(); i++) {
- board[x][y + i] = word.charAt(i);
- locations.add(Pair.of(x, y + i));
- }
- }
- this.wordLocations.put(word, locations);
- }
- private boolean isValidIntersection(String word, int x, int y, boolean horizontal) {
- /
- }
- private void expandBoard() {
- char[][] oldBoard = this.board;
- this.board = new char[this.board.length + 1][this.board[0].length + 1];
- for (int j = 0; j < oldBoard.length; j++) {
- System.arraycopy(oldBoard[j], 0, this.board[j], 0, oldBoard[j].length);
- }
- }
- private void expandBoardNegatively() {
- char[][] oldBoard = this.board;
- this.board = new char[this.board.length + 1][this.board[0].length + 1];
- for (int j = 0; j < oldBoard.length; j++) {
- System.arraycopy(oldBoard[j], 0, this.board[j + 1], 0, oldBoard[j].length);
- }
- }
- public char get(int x, int y) {
- return this.board[x][y];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement