我的班级最近被分配了一个程序,我们需要创建一个填字游戏,使用二维数组,以及递归。使用说明如下:
***编写一个程序,从一个名为words.txt的输入文件中读取单词,创建一个纵横字谜显示,最大化单词之间的重叠字母。假设电路板有15行15列。
输入文件的第一行是' ',一个表示存在多少单词的数字。然后提供单词,每行一个。当您的程序运行时,它应该显示它找到的解决方案,以及每个解决方案的重叠数。在最后,它应该显示找到的最大重叠数的解决方案,或者如果没有填字游戏是可能的,则显示一条消息。*
我很难理解这个概念。我理解需要嵌入"for"循环来循环递归,但我不确定从哪里开始。下面是一些示例代码,可以帮助我们开始:
/* Dictionary.java
* Read in words from a file. The first line in the file should
* be the number of words. Subsequent lines have the words
* themselves, one per line. For instance, a sample file
* could contain:
* 5
* Java
* Programming
* Euphoria
* Consternation
* Education
*
* This sample code is not meant to be stand-alone, but rather
* is meant for you to copy and paste into some other class.
*/
//the following are needed to implement reading from the file
import java.io.*; // Used for IOException, File
import java.util.Scanner; // Used for File input
public class ReadWords
{
// Declare an array of strings to hold the words
String[] wordsArray;
// chain off to another method to avoid static errors
public static void main( String[] args)
{
ReadWords theInstance = new ReadWords();
theInstance.doIt();
}
// ------------------------------------------------------------------------
// Read in the words
void doIt()
{ // Use a try-catch block for exception handling. What this does is
// provide a place in your program to handle potential file read errors
try {
// Define a Scanner to read from an input file. Note that the name of
// the file given in the code below MUST match the actual filename of
// the words file. This file should be in the same directory
// as the source code for this project
File wordsFile = new File("words.txt"); // declare the file
// Ensure file exists and is in the correct directory
if( ! wordsFile.exists()) {
System.out.println("*** Error *** n" +
"Your words file has the wrong name or is " +
"in the wrong directory. n" +
"It should be in " + System.getProperty("user.dir") + "n" +
"n" +
"Aborting program...nn");
System.exit( -1); // Terminate the program
}
Scanner inputFile = new Scanner( wordsFile);
// while there are words in the input file, add them to the dictionary
int numberOfWords = inputFile.nextInt();
// use this value to allocate memory for the words array
wordsArray = new String[ numberOfWords];
// Now read this many words
for( int i=0; i< wordsArray.length; i++) {
// read next word and store into array
wordsArray[ i] = inputFile.next().toUpperCase();
}
}
catch (IOException e)
{
System.out.println("Error in words file read");
System.exit( -1);
}
// echo the words found
System.out.println("The words read are: ");
for( int i=0; i< wordsArray.length; i++) {
System.out.println( wordsArray[ i]);
}
}//end doIt()
}//end class
如果有人有任何建议或建议,这将是最有帮助的。
好的。以下是我的想法。给定一个15x15的矩阵,一个单词有225个可能的起始位置(取决于单词的长度)。另外,每个位置都有2种可能的方向。这为你提供了一个单词450个可能的位置。
我会这样做:
-
创建一个对象来表示矩阵的状态(保存当前插入的所有单词)。该对象应该有一个方法,该方法给出一个新单词,将返回该单词适合的所有可能位置(和方向)。它还应该跟踪交叉次数。
-
从第一个单词开始
-
找到所有适合的位置。
。如果找不到位置,这就是死胡同。
-
对于每个位置,
。创建一个矩阵的实例,其中单词位于该位置。
b。获取下一个单词并递归重复3,直到所有插入的单词