473,406 Members | 2,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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.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
1 13062
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

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

Similar topics

1
by: Adi | last post by:
A java program we have written crashes with IBM JDK 1.3.1 on linux. It works fine on other platforms(Solaris,HPUx). It gets a SIGSERV Signal 11 and crashes just after few minutes after starting up....
5
by: Tyler | last post by:
Hi ppl, I know it's forbidden to mention Java in this newsgroup but I've been presented with a problem which requires that my C++ program communicate with another java program. What is the...
0
by: xavier vazquez | last post by:
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...
3
by: joshwa | last post by:
i need java program for moving the first line from several word documents to one document, could any one there to help me, its very urgent
1
by: =?Utf-8?B?S2Vubnk=?= | last post by:
I have one bat file that contains a command to startup Java Program. Then, I would like to create a cluster job to call the bat file. In case of one computer is down, another computer can also call...
1
by: junglenut | last post by:
please give me a reverse word java program in a form of jcreator javax.swing.*; source code.. because i really have a hard time figuring it out on how to make it.. please give me a reverse word...
6
by: moongeegee | last post by:
I have compile my java program as myjava.class. And I can run as "java myjava" without any program. As now, I need to execute myjava.class in javascript. Please shed a light how to execut "java...
5
madzman23
by: madzman23 | last post by:
Hi guyz, I kinda new here and I dont know if there is post that similar to my question, because I really needed immediately I am posting my question. Can anyone here help me how to call a Java...
6
by: davidcollins001 | last post by:
Hi, I came across a neat problem that I have been trying to code to improve my programming problem solving skills. It is quite simple but I am struggling to get my head around it. Any help would...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.