473,768 Members | 2,322 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

cross word puzzle java program

3 New Member
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
Oct 22 '06 #1
1 13104
xavier vazquez
3 New Member
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
4309
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. THe program creates a good number of threads. Any suggestions most welcome. Have tried following setting LD_ASSUME_KERNEL=2.2.5 disabling JIT by passing -Djava.compiler=NONE
5
3095
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 best,simplest and quickest way to go about this? I was thinking, I could have my C++ program write data to a file and then my Java program pick it up from there but this is a little too
0
2024
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 other....how i can fix this this run the random words for the program import javax.swing.JOptionPane; import java.util.ArrayList; import java.util.Random; public class CrossWordPuzzleTester {
3
1282
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
3586
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 the bat file and startup my Java Program. For this purpose, I have tried to place the bat file into share drive of two computers which are cluster machine. Also, I used Generic Application to create a job to call my bat file and this job is...
1
1755
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 source code so that i can study its construction figure.. (need input from the user type of program) T_T please help..:c
6
6857
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 myjava" in my javascript. Thanks a million.
5
10771
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 Program using the PHP. The PHP code should call a Java program that will extract all the data from the database, I know we can do this in PHP, but I want a more secure one so I want to make a prototype that will call the Java Program that get all the...
6
2849
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 be much appreciated. The problem is the following: a starting word, such as head, is transformed into a second word, such as tail, by way of changing a single letter at a time. All intermediate words must be in the dictionary. The aim is to find...
0
9576
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9407
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9843
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8840
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7384
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6656
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5283
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3932
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3534
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.