我必须做一个纵横填字游戏,它必须做以下事情:从一个包含单词列表的.txt文件中输入-Reads。每行放置一个单词。-If单词有一个相似的字母,那么它们可以交叉,但是-two单词不能垂直或水平地相互接触。也就是说,狗和雾不能同时与潜水艇相交,因为它们会互相接触。如果发生这样的情况,则必须跳过单词,但将单词存储起来以备以后与网格中心的单词水平地-print出纵横填字游戏的情况下使用
我不能让任何这些工作,所以我开始尝试让更简单的步骤首先工作。我正在尝试让两个单词成功地相互交叉。
这个input1.txt文件包含以下单词(只是尝试测试它):clowning incline (分成两行)
import java.util.*;
import java.io.*;
/**
The class A2 reads a list of words from standard input and
produces a crossword layout.
*/
public class A2
{
public static void main(String[] args) throws IOException
{
Scanner in = new Scanner (new File("input1.txt"));
Crossword board = new Crossword();
ArrayList<String> str = new ArrayList<>();
String words = "The words are:";
System.out.println(words);
while (in.hasNextLine() == true) //reads all the words in the input1.txt file into an ArrayList of strings
{
int i = 0;
str.add(i, in.nextLine());
String wurds = "";
wurds += str.get(i);
System.out.println(wurds); //prints out the words that are in the .txt file
i++;
}
board.fill(str); //fills the 2d array board with the words
System.out.print(board.toString()); //converts the 2d array into a string
}
}
/**
The class Crossword knows how to build a crossword layout from
a list of words.
*/
class Crossword
{
private final int ROWS = 20;
private final int COLUMNS = 20;
char[][] crossword;
/**
Constructs a crossword board with 20 rows and 20 columns, filled with empty spaces.
*/
public Crossword()
{
crossword = new char[ROWS][COLUMNS];
for (int i = 0; i < ROWS; i++)
for (int j = 0; j < COLUMNS; j++)
crossword[i][j] = ' ';
}
/**
Finds the largest word in an ArrayList.
@param from the first position of the tail region
@param anArray the arrayList which is being searched
@return largestWord the word with the largest length
*/
private int largestWord(int from, ArrayList<String> anArray)
{
int largestWord = from;
for (int i = from + 1; i < anArray.size(); i++)
if ((anArray.get(i)).length() > (anArray.get(largestWord)).length())
largestWord = i;
return largestWord;
}
/**
Fills the crossword board with words from an ArrayList.
@param a1 the ArrayList of strings which will fill the board.
*/
public void fill(ArrayList<String> a1)
{
int i = 0;
int j;
int count = 0;
while (i < a1.size())
{
int maxPos = largestWord(i, a1);
for (j = 0; j < (a1.get(maxPos)).length(); j++)
{
crossword[ROWS/2][j] = (a1.get(maxPos)).charAt(j);
} //everything is OK until this point
i++;
while (j < (a1.get(0)).length())
{
int x = 0;
for (int u = 0; u < (a1.get(i)).length(); u++)
if ((a1.get(i)).charAt(x) != crossword[ROWS/2][j])
{
count++; //count is being incremented to remember the column position
if (crossword[ROWS/2][j] == (a1.get(i)).charAt(x))
{
crossword[(ROWS/2) + u][count] = (a1.get(i)).charAt(u);
}
j++;
}
}
i++;
}
}
/**
Converts the 2d array into a string.
*/
public String toString()
{
String r = "";
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLUMNS; j++)
r = r + "|" + crossword[i][j];
r = r + "|\n";
}
return r;
}
}当我写代码时,解释一下我在想什么:最大的单词"clowning“位于2d数组的中间行。然后我递增数组列表并转到下一个单词。我在已经存在一个单词的行中查找,并尝试找到与第二个单词匹配的单词。这里我作弊了一点,因为“斜面”中的第一个字母"i“是小丑中的第六个字母,但正如我所说的,我只是想在做进一步的步骤之前测试一下,找出我做错了什么。非常感谢所有正确方向的帮助和指导:
发布于 2011-11-20 13:04:33
如果您跟踪单词的起始坐标、方向和长度,则直到最后才需要将字母复制到可视纵横填字游戏中。你想把问题分解成几个步骤,比如,“我的单词可以与这个列表成员相交吗?”,“我的单词相对于这个单词的有效位置是什么?”,“我的单词的这个位置是否会与列表中的任何其他单词发生冲突?”这些问题中的每个都应该映射到不同的方法。
https://stackoverflow.com/questions/8199377
复制相似问题