对于单词搜索程序,我提示用户输入单词,最大值。260,我将他们的输入存储在一个数组列表中。在输入前 20 个单词后,程序会询问用户是否想添加更多单词(另外 20 个单词)。如果用户说不,那么程序会跳出循环,然后去创建单词搜索。
createWordSearch 方法接受单词列表作为参数。这是我的这个方法的代码:
public static WordArray createWordSearch(List<String> words) {
WordArray wordArr = new WordArray();
// Have numAttempts set to 0 because the while loop below will add one every time we create a new word search
int numAttempts = 0;
while (++numAttempts < 100) { // There will be 100 attempts to generate our word array/grid
Collections.shuffle(words); // The words will be shuffled/randomized
int messageLength = placeMessage(wordArr, "Word Search Puzzle");
int target = arraySize - messageLength;
int cellsFilled = 0;
for (String word : words) { // For each word in the array list 'words'...
cellsFilled += placeWord(wordArr, word);
if (cellsFilled == target) {
// solutions is a list
if (wordArr.solutions.size() >= minWords) { // Minimum number of words to place into the word array/grid to generate = 20
wordArr.numAttempts = numAttempts;
return wordArr;
} else { // We have fulfilled the word array/grid, but we don't have enough words, so we restart (go through the loop again)
break;
}//end of else
}//end of outer if
}//end of for loop
}//end of while loop
System.out.println("Word search has been created.");
return wordArr;
}//end of createWordSearch(words)
在哪里public static final int rows = 10, cols = 10; // Number of rows and columns for the word array/grid AND public static final int arraySize = ( (rows) * (cols));
你在这个方法中看到的方法,比如location
and placeWord
go是这样的:
public static int placeWord (WordArray wordArr, String word) {
int randDirection = rand.nextInt(DIRECTIONS.length);
int randPosition = rand.nextInt(arraySize);
for (int dir = 0; dir < DIRECTIONS.length; dir++) {
dir = ( (dir) + (randDirection) ) % DIRECTIONS.length
for (int pos = 0; pos < arraySize; pos++) {
pos = ( (pos) + (randPosition) % arraySize);
int lettersPlaced = location(wordArr, word, dir, pos);
if (lettersPlaced > 0) {
return lettersPlaced;
}//end of if
}//end of inner for loop
}//end of outer for loop
return 0;
}//end of placeWord(wordArr,word)
public static int location (WordArray wordArr, String word, int dir, int pos) {
int r = ( (pos) / (cols)); // Where r = row
int c = ( (pos) / (cols)); // Where c = column
// Checking the bounds...
if ((DIRECTIONS[dir][0] == 1 && (word.length() + c) > cols)
|| (DIRECTIONS[dir][0] == -1 && (word.length() - 1) > c)
|| (DIRECTIONS[dir][1] == 1 && (word.length() + r) > rows)
|| (DIRECTIONS[dir][1] == -1 && (word.length() - 1) > r)
)
return 0;
int i, cc, rr, overLaps = 0;
// Checking the cells...
for (i = 0, rr = r, cc = c; i < word.length(); i++) {
if (rr < rows && cc < cols) {
return 0;
}//end of if
cc += DIRECTIONS[dir][0];
rr += DIRECTIONS[dir][1];
}//end of for loop
// Placing the word...
for (i = 0, rr = r, cc = c; i < word.length(); i++) {
if (rr < rows && cc < cols) {
overLaps++;
}//end of if
if (i < word.length() - 1) {
cc += DIRECTIONS[dir][0];
rr += DIRECTIONS[dir][1];
}//end of inner if
}//end of for loop 2
int lettersPlaced = ( (word.length()) - (overLaps));
if (lettersPlaced > 0)
wordArr.solutions.add(String.format("%-10s (%d,%d)(%d,%d)", word, c, r, cc, rr));
return lettersPlaced;
}//end of location(wordArr,word,dir,pos)
在哪里public static final int[][] DIRECTIONS = {{1,0}, {0,1}, {1,1}, {1,-1}, {-1,0}, {0,-1}, {-1,-1}, {-1,1}};
当我调试我的代码时,我注意到我的程序会遍历 location 方法中的第一个 if 语句,然后继续执行其他循环,然后还循环到 placeWord 方法中。问题是它在循环中重复了很多次,并且没有脱离循环。当我单击“运行”以运行我的代码并选择查看单词搜索的选项时,我看到它是空的。
我不确定我的程序为什么要这样做,而且我压力很大,因为我快要完成了,但它是在即将到来的星期三早上到期的。如果有人推荐任何解决方案或建议我该怎么做,我将不胜感激。