459,377 Members | 1,657 Online
Need help? Post your question and get tips & solutions from a community of 459,377 IT Pros & Developers. It's quick & easy.

# How do I randomly distribute words in a string onto a table

P: 3
I trying to write a JavaScript program where it takes a string and will randomly distributed the reversed words onto a 16x16 table, meaning a string can go to the right-to-left(i.e. Four would look like rouF), down-to-up, as well as diagonally down and diagonally up. Also letter can intersect and be shared(i.e the letter S could be used for seven going diagonally up and score going diagonally down) So far, I've used string.split to convert the string to an array and have created the table, but I'm stuck as to get to the next steps. Any help or clues is very much appreciated. Thanks!

NOTE: I put up a sample of my project for what the results should look like
Attached Images
 puzzle.PNG (15.0 KB, 87 views)
Mar 14 '11 #1

Well, it's going to be quite an involved algorithm. The way I would approach it is like this
1. Get a random number from 1 to 4. That will decide whether it will be horizontal, vertical, positive slope, or negative slope.
2. Get another random number to decide if I want to try to intersect it with an existing word.
3. Get random x, y coordinate for the start of the word. Taking into account the length of the word.
4. Check array for conflicting intersections. If conflict, go to prior step and try again. Otherwise, fill in the array.

4 Replies

 Expert Mod 10K+ P: 12,430 Can you show us what the results are supposed to look like given inputs? Mar 14 '11 #2

 P: 3 Just did, look at the png image. Sorry about that, don't know why I didn't of doing that before. Would probably help clarify things Mar 14 '11 #3

 Expert Mod 10K+ P: 12,430 Well, it's going to be quite an involved algorithm. The way I would approach it is like thisGet a random number from 1 to 4. That will decide whether it will be horizontal, vertical, positive slope, or negative slope. Get another random number to decide if I want to try to intersect it with an existing word. Get random x, y coordinate for the start of the word. Taking into account the length of the word. Check array for conflicting intersections. If conflict, go to prior step and try again. Otherwise, fill in the array. Mar 14 '11 #4

 P: 3 I see, that sortof makes sense. Could I see how that would look like code-wise if that's not too much to ask(just trying to get an idea of the problem). Could be any example, not just this Mar 14 '11 #5