尝试在2D阵列网格中使两个单词相互交叉

时间:2021-04-18 14:03:43

I have to make a crossword puzzle and it has to do the following things: -Reads input from a .txt file which contains a list of words. Words are placed one per line. -If words have a similar letter, then they can cross BUT -two words cannot be touching each other vertically or horizontally. i.e. dog and fog cannot both intersect with loon since they would touch each other. If such a case occurs, word must be skipped but stored for use in case there is a later intersection -start out with a word in the center of grid horizontally -print out the crossword

我必须做一个填字游戏,它必须做以下事情: - 从包含单词列表的.txt文件中读取输入。每行放一个单词。 - 如果单词有相似的字母,那么它们可以交叉但两个单词不能垂直或水平相互接触。即狗和雾不能与懒人相交,因为它们会相互接触。如果发生这种情况,必须跳过单词,但是存储以供以后使用时 - 在网格中心的一个单词水平打印填字游戏

I cant get any of this working so Im starting out trying to get simpler steps to work first. I am trying to get two words to successfully cross each other.

我无法得到任何这样的工作,所以我开始试图让更简单的步骤先工作。我试图让两个词成功地相互交叉。

The input1.txt file contains the following words (just trying to test it): clowning incline (seperated into two lines)

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;

}
}

An explanation of what is Im thinking when I wrote the code: The largest word, "clowning" goes in the middle row of the 2d array. Then I increment the array list and go to the next word. I look in the row where a word is already present and try to find a match with the 2nd word. Here I cheated a bit because the first letter "i" in "incline" is the 6th letter in clowning, but as I said, I just want to test this out and figure out what Im doing wrong before doing further steps. All help and guidance in the right direction is much appreciated :D

我编写代码时我在想什么的解释:最大的单词“clowning”位于2d数组的中间行。然后我增加数组列表并转到下一个单词。我查看已存在单词的行并尝试找到与第二个单词匹配的行。在这里我作弊了一点,因为“倾斜”中的第一个字母“i”是小丑中的第6个字母,但正如我所说,我只是想测试一下,并在做进一步的步骤之前弄清楚我做错了什么。非常感谢所有正确方向的帮助和指导:D

1 个解决方案

#1


0  

If you kept track of a word's starting coordinate, direction, and length, you wouldn't need to copy the letters into a visual crossword until the very end. You want to break the problem down into steps like, "Can my word intersect with this list member?", "What are the valid placements of my word against this word?", "Would this placement of my word collide with any of the other words in this list?". Each of those questions should map to a different method.

如果您跟踪单词的起始坐标,方向和长度,则不需要将字母复制到可视填字游戏中直到最后。你想把这个问题分解成一些步骤,比如“我的单词可以与这个列表成员相交吗?”,“我的单词对这个单词的有效位置是什么?”,“我的单词的这个位置是否会与任何一个单词冲突这个清单中的其他词语?“这些问题中的每一个都应该映射到不同的方法。

#1


0  

If you kept track of a word's starting coordinate, direction, and length, you wouldn't need to copy the letters into a visual crossword until the very end. You want to break the problem down into steps like, "Can my word intersect with this list member?", "What are the valid placements of my word against this word?", "Would this placement of my word collide with any of the other words in this list?". Each of those questions should map to a different method.

如果您跟踪单词的起始坐标,方向和长度,则不需要将字母复制到可视填字游戏中直到最后。你想把这个问题分解成一些步骤,比如“我的单词可以与这个列表成员相交吗?”,“我的单词对这个单词的有效位置是什么?”,“我的单词的这个位置是否会与任何一个单词冲突这个清单中的其他词语?“这些问题中的每一个都应该映射到不同的方法。