By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,529 Members | 1,860 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,529 IT Pros & Developers. It's quick & easy.

cross word puzzle java program

P: 3
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.ArrayList;

import java.util.Random;

import javax.swing.JOptionPane;

public class CrossWordPuzzle {

private char[][] puzzle;

private ArrayList<String> words, definitions;

private ArrayList<String> horizontaldefinitionPrint, horizontalcoordinates;

private ArrayList<String> verticaldefinitionPrint, verticalcoordinates;

/**
* 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.toString(i-2).charAt(0);
else
puzzle[i][1]= Integer.toString(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.toString(j-2).charAt(0);
else
puzzle[1][j]= Integer.toString(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<String>();

definitions = new ArrayList<String>();

horizontaldefinitionPrint = new ArrayList<String>();

horizontalcoordinates = new ArrayList<String>();

verticaldefinitionPrint = new ArrayList<String>();

verticalcoordinates = new ArrayList<String>();

}

/**
* 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.toUpperCase();
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 senseOfDirection = random.nextBoolean();

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

//If true, horizontal...else vertical
if (senseOfDirection)
{
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;

horizontalcoordinates.add("(" + y + ", " + x + ")");

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

horizontaldefinitionPrint.add(definitions.get(word Index));

definitions.remove(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;

verticalcoordinates.add("(" + y + ", " + x + ")");

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

verticaldefinitionPrint.add(definitions.get(wordIn dex));

definitions.remove(wordIndex);
}

words.remove(wordIndex);
}
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.print(puzzle[i][j]);
}
System.out.println( );
}
if (horizontalcoordinates.size() > 0)
{
System.out.println("\nHorizontales: ");

for (int i = 0; i < horizontalcoordinates.size(); i++)
{
System.out.print(horizontalcoordinates.get(i));
System.out.println(" " + horizontaldefinitionPrint.get(i));
}
}

if (verticalcoordinates.size()>0)
{
System.out.println("\nVerticales: ");

for (int i = 0; i < verticalcoordinates.size(); i++)
{
System.out.print(verticalcoordinates.get(i));
System.out.println(" " + verticaldefinitionPrint.get(i));
}
}
}

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

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




help need soon i suppose to handle this on monday.....
TX
Oct 22 '06 #1
Share this Question
Share on Google+
1 Reply


P: 3
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...)
Oct 22 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.