cross word puzzle java program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xavier vazquez
    New Member
    • Oct 2006
    • 3

    #1

    cross word puzzle java program

    I have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of the other....how i can fix this


    the program look like this


    import java.util.Array List;

    import java.util.Rando m;

    import javax.swing.JOp tionPane;

    public class CrossWordPuzzle {

    private char[][] puzzle;

    private ArrayList<Strin g> words, definitions;

    private ArrayList<Strin g> horizontaldefin itionPrint, horizontalcoord inates;

    private ArrayList<Strin g> verticaldefinit ionPrint, verticalcoordin ates;

    /**
    * Creates a new object of the class, within it, a CrossWord Puzzle empty with sizes NxN
    */
    public CrossWordPuzzle ()
    {
    //Size set to minimum
    puzzle = new char[18][18];

    //Generating horizontal coordinates
    for (int i=0; i<18; i++)
    {
    if (i<2)
    puzzle[i][0]=' ';
    else if (i<12)
    puzzle[i][0]='0';
    else
    puzzle[i][0]='1';
    }

    for (int i=0; i<18; i++)
    {
    if (i<2)
    puzzle[i][1]=' ';
    else if (i<12)
    puzzle[i][1]= Integer.toStrin g(i-2).charAt(0);
    else
    puzzle[i][1]= Integer.toStrin g(i-12).charAt(0);
    }

    //Generating Vertical Coordinates
    for (int j=0; j<18; j++)
    {
    if (j<2)
    puzzle[0][j]=' ';
    else if (j<12)
    puzzle[0][j]='0';
    else
    puzzle[0][j]='1';
    }

    for (int j=0; j<18; j++)
    {
    if (j<2)
    puzzle[1][j]=' ';
    else if (j<12)
    puzzle[1][j]= Integer.toStrin g(j-2).charAt(0);
    else
    puzzle[1][j]= Integer.toStrin g(j-12).charAt(0);
    }

    //Generating dots
    for (int i=2; i<18; i++)
    {
    for (int j=2; j<18; j++)
    {
    puzzle[i][j]='.';
    }
    }
    words = new ArrayList<Strin g>();

    definitions = new ArrayList<Strin g>();

    horizontaldefin itionPrint = new ArrayList<Strin g>();

    horizontalcoord inates = new ArrayList<Strin g>();

    verticaldefinit ionPrint = new ArrayList<Strin g>();

    verticalcoordin ates = new ArrayList<Strin g>();

    }

    /**
    * Adds a word to the CrossWord Puzzle, with its definition given by the parameter meaning
    * @param word Word to be added
    * @param meaning Meaning of the added word
    */
    public void addWord(String word, String meaning)
    {
    word = word.toUpperCas e();
    words.add(word) ;
    definitions.add (meaning);
    }

    /**
    * Generates a CrossWord Puzzle with the words added
    */
    public void generate()
    {
    Random random = new Random();
    int coord_x;
    int coord_y;

    do
    {
    boolean senseOfDirectio n = random.nextBool ean();

    int wordIndex = random.nextInt( words.size());
    String word = words.get(wordI ndex);

    //If true, horizontal...el se vertical
    if (senseOfDirecti on)
    {
    do
    {
    coord_x = random.nextInt( 16) + 2;
    coord_y = random.nextInt( 16) + 2;
    }
    while(18-coord_y <= word.length());

    int x = coord_x - 2;

    int y = coord_y - 2;

    horizontalcoord inates.add("(" + y + ", " + x + ")");

    for (int i = 0; i<=word.length( )-1; i++)
    {
    puzzle[coord_x][coord_y]=word.charAt(i) ;
    coord_y++;
    }

    horizontaldefin itionPrint.add( definitions.get (wordIndex));

    definitions.rem ove(wordIndex);
    }

    else
    {
    do
    {
    coord_x = random.nextInt( 16) + 2;
    coord_y = random.nextInt( 16) + 2;
    }
    while(18-coord_x <= word.length());

    int x = coord_x - 2;

    int y = coord_y - 2;

    verticalcoordin ates.add("(" + y + ", " + x + ")");

    for (int i = 0; i<=word.length( )-1; i++)
    {
    puzzle[coord_x][coord_y]=word.charAt(i) ;
    coord_x++;
    }

    verticaldefinit ionPrint.add(de finitions.get(w ordIndex));

    definitions.rem ove(wordIndex);
    }

    words.remove(wo rdIndex);
    }
    while (words.size() > 0);

    }


    /**
    * Displays the CrossWord Puzzle solution on the console
    * (If the generate method hasn't been applied, displays an error on the console)
    */
    public void displaySol()
    {

    if (words.size() == 0)
    {
    for (int i=0; i<18; i++)
    {
    for (int j=0; j<18; j++)
    {
    System.out.prin t(puzzle[i][j]);
    }
    System.out.prin tln( );
    }
    if (horizontalcoor dinates.size() > 0)
    {
    System.out.prin tln("\nHorizont ales: ");

    for (int i = 0; i < horizontalcoord inates.size(); i++)
    {
    System.out.prin t(horizontalcoo rdinates.get(i) );
    System.out.prin tln(" " + horizontaldefin itionPrint.get( i));
    }
    }

    if (verticalcoordi nates.size()>0)
    {
    System.out.prin tln("\nVertical es: ");

    for (int i = 0; i < verticalcoordin ates.size(); i++)
    {
    System.out.prin t(verticalcoord inates.get(i));
    System.out.prin tln(" " + verticaldefinit ionPrint.get(i) );
    }
    }
    }

    else
    {
    String error = "Please use the \'generate\' method before displaying the solution";
    JOptionPane.sho wMessageDialog( null, error, "CrossWordPuzzl e Tester - Runtime Error", 2);
    }
    }

    public int numberOfWords()
    {
    int number = words.size();
    return number;
    }
    }




    help need soon i suppose to handle this on monday.....
    TX
  • xavier vazquez
    New Member
    • Oct 2006
    • 3

    #2
    In this program, the problem i am having is with the generate method...

    it is supposed to generate random words at random coordinates, but it seems

    to be overlapping the generated words.


    For example, if it generates a horizontal word (i.e. dog ) and after that it

    generates a vertical word (i.e. cat ) starting where the 'd' is positioned in the

    array, the 'c' in 'cat' overlaps the 'd' in 'dog' displaying in the console something

    like this:

    . . . . c a t . . .
    . . . . o . . . . .
    . . . . g . . . . .

    I have tried several things but to no avail. Can anyone suggest something i can

    do with the 'generate()' method that could help me out with my problem?


    (This homework program is due on Monday at 11:59pm... please, i need help.

    Time's running out...)

    Comment

    Working...