0

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

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • I've add the `homework` tag, because this clearly is. In future, 'fess up! – Bohemian Nov 20 '11 at 04:13
  • Can you ask a specific question. I'm a little confused about what exactly you're looking for. – heneryville Nov 20 '11 at 04:18
  • might be useful: [algorithm to generate a crossword](http://stackoverflow.com/questions/943113/algorithm-to-generate-a-crossword) – happymeal Nov 20 '11 at 04:40
  • I need help with my "fill" method. I already have one word printed out horizontally in the center of the grid and I need to make the second word intersect it and printed out And yes, this is homework but I dont want people to do the code for me, just some guidance :P – user1055678 Nov 20 '11 at 04:41
  • @happymeal thank you for the reply. Yes, that is precisely the algorithm Im trying to work with but the problem is I cant seem to put into programming code :p – user1055678 Nov 20 '11 at 04:51
  • for starters, you should define a method which checks if a new word can be placed into the puzzle. – happymeal Nov 20 '11 at 05:03

1 Answers1

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.

phatfingers
  • 9,770
  • 3
  • 30
  • 44
  • Thanks for the reply. Working with what you said, I think Ive got how to do the valid placement and no colliding methods. Im stuck on how to keep track of the starting coordinate and direction of a word though. Any help? – user1055678 Nov 20 '11 at 14:53
  • How about a PlacedWord class? The class could hold the word itself, an (x,y) placement of the first letter, and a direction. You start with a list of String words, but build a list of PlacedWords. – phatfingers Nov 20 '11 at 17:27