0

我正在尝试将 puzzleWords 或 userSpecifiedWords 列表中的每个单词随机放入拼图二维数组中。单词可以从上到下垂直放置,从下到上垂直放置,从左到右水平放置,从右到左水平放置。本质上是进行单词搜索。但似乎无法弄清楚如何将下一个字符放置在正确的位置,以便满足这些要求。

我尝试最初在 2d 数组中生成一个随机位置,然后将第一个单词的第一个字符放在该位置,但在放置下一个字符时遇到了困难,然后继续下一个单词。

//Initialising the 2d array and list of words.

public class WordSearchPuzzle {
    private char [][] puzzle;
    private List<String> puzzleWords;

//What I have so far to take the word out of the array, generate a random pos in 2d array then check if the pos is empty, if empty place the first char of word in that pos else generate new pos.

for (int i = 0; i < puzzleWords.size(); i++) {
            for (int r = 0; r < puzzle.length; r++) {
                for (int c = 0; c < puzzle[r].length; c++) {
                    String s = puzzleWords.get(i);
                    int wordLength = s.length();
                    char firstChar = s.charAt(0);
                    while(puzzle[rowGenerate][colGenerate] != ' ') {
                        puzzle[rowGenerate][colGenerate] = puzzle[(int) Math.random() * gridSize + 1][(int) Math.random() * gridSize + 1];
                    }
                    puzzle[rowGenerate][colGenerate] = firstChar;
                }
4

0 回答 0