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