Advertisement
TurtyWurty

Crossword Game

Feb 17th, 2024
709
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.70 KB | None | 0 0
  1. @Getter
  2.     public static class Game {
  3.         private static final Random RANDOM = new Random();
  4.         private final List<String> words;
  5.         private final Map<String, List<Pair<Integer, Integer>>> wordLocations = new HashMap<>();
  6.         private final int wordCount;
  7.         private final long guildId, userId, channelId;
  8.         private char[][] board;
  9.         @Setter
  10.         private long threadId;
  11.  
  12.         public Game(int wordCount, long guildId, long userId, long channelId) throws IllegalStateException {
  13.             if (wordCount < 1)
  14.                 throw new IllegalStateException("Word count must be greater than 0!");
  15.  
  16.             this.wordCount = wordCount;
  17.  
  18.             this.words = getWords(wordCount);
  19.             if (this.words.isEmpty())
  20.                 throw new IllegalStateException("Failed to get words for crossword!");
  21.  
  22.             this.guildId = guildId;
  23.             this.userId = userId;
  24.             this.channelId = channelId;
  25.  
  26.             this.board = new char[10][10];
  27.             fillBoard();
  28.         }
  29.  
  30.         private static List<String> getWords(int wordCount) {
  31.             Either<List<String>, HttpStatus> response = ApiHandler.getWords(new RandomWordRequestData.Builder()
  32.                     .length(3, 10)
  33.                     .amount(wordCount)
  34.                     .build());
  35.             if (response.isLeft())
  36.                 return List.copyOf(response.getLeft());
  37.             else
  38.                 Constants.LOGGER.error("Failed to get words for crossword! Status: " + response.getRight());
  39.  
  40.             return List.of();
  41.         }
  42.  
  43.         private boolean hasCharacter(int row, int column, char character) {
  44.             return this.board[row][column] == character;
  45.         }
  46.  
  47.         private boolean isEmpty(int row, int column) {
  48.             return hasCharacter(row, column, '\u0000');
  49.         }
  50.  
  51.         public boolean isOutsideBounds(int x, int y) {
  52.             return y < 0 || y >= this.board.length || x < 0 || x >= this.board[y].length;
  53.         }
  54.  
  55.         private void fillBoard() {
  56.             List<String> sorted = this.words.stream().sorted(Comparator.comparingInt(String::length).reversed()).toList();
  57.             String first = sorted.getFirst();
  58.             while (first.length() >= this.board.length)
  59.                 expandBoard();
  60.  
  61.             int x = RANDOM.nextInt(this.board.length - first.length());
  62.             int y = RANDOM.nextInt(this.board.length - first.length());
  63.             boolean horizontal = RANDOM.nextBoolean();
  64.             forcePlaceWord(first, x, y, horizontal);
  65.  
  66.             List<String> failed = new CopyOnWriteArrayList<>();
  67.             for (int i = 1; i < sorted.size(); i++) {
  68.                 String word = sorted.get(i);
  69.                 Pair<Integer, Integer> location = findValidLocation(word);
  70.                 if (location == null) {
  71.                     failed.add(word);
  72.                     continue;
  73.                 }
  74.  
  75.                 forcePlaceWord(word, location.getLeft(), location.getRight(), RANDOM.nextBoolean());
  76.             }
  77.  
  78.             while (!failed.isEmpty()) {
  79.                 for (String word : failed) {
  80.                     Pair<Integer, Integer> location = findValidLocation(word);
  81.                     if (location == null) {
  82.                         continue;
  83.                     }
  84.  
  85.                     failed.remove(word);
  86.                     forcePlaceWord(word, location.getLeft(), location.getRight(), RANDOM.nextBoolean());
  87.                 }
  88.             }
  89.         }
  90.  
  91.         private Pair<Integer, Integer> findValidLocation(String word) {
  92.             for (Map.Entry<String, List<Pair<Integer, Integer>>> entry : wordLocations.entrySet()) {
  93.                
  94.             }
  95.  
  96.             return null;  // No valid intersection found
  97.         }
  98.  
  99.         private void forcePlaceWord(String word, int x, int y, boolean horizontal) {
  100.             List<Pair<Integer, Integer>> locations = new ArrayList<>();
  101.             if (horizontal) {
  102.                 for (int i = 0; i < word.length(); i++) {
  103.                     if (isOutsideBounds(x + i, y)) {
  104.                         expandBoard();
  105.                     }
  106.                 }
  107.  
  108.                 for (int i = 0; i < word.length(); i++) {
  109.                     board[x + i][y] = word.charAt(i);
  110.                     locations.add(Pair.of(x + i, y));
  111.                 }
  112.             } else {
  113.                 for (int i = 0; i < word.length(); i++) {
  114.                     if (isOutsideBounds(x, y + i)) {
  115.                         expandBoard();
  116.                     }
  117.                 }
  118.  
  119.                 for (int i = 0; i < word.length(); i++) {
  120.                     board[x][y + i] = word.charAt(i);
  121.                     locations.add(Pair.of(x, y + i));
  122.                 }
  123.             }
  124.  
  125.             this.wordLocations.put(word, locations);
  126.         }
  127.  
  128.         private boolean isValidIntersection(String word, int x, int y, boolean horizontal) {
  129.             /
  130.         }
  131.  
  132.         private void expandBoard() {
  133.             char[][] oldBoard = this.board;
  134.             this.board = new char[this.board.length + 1][this.board[0].length + 1];
  135.             for (int j = 0; j < oldBoard.length; j++) {
  136.                 System.arraycopy(oldBoard[j], 0, this.board[j], 0, oldBoard[j].length);
  137.             }
  138.         }
  139.  
  140.         private void expandBoardNegatively() {
  141.             char[][] oldBoard = this.board;
  142.             this.board = new char[this.board.length + 1][this.board[0].length + 1];
  143.             for (int j = 0; j < oldBoard.length; j++) {
  144.                 System.arraycopy(oldBoard[j], 0, this.board[j + 1], 0, oldBoard[j].length);
  145.             }
  146.         }
  147.  
  148.         public char get(int x, int y) {
  149.             return this.board[x][y];
  150.         }
  151.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement