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
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)
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