Hello everyone! I am a first year student studying computer science and I am having a little trouble figuring out how to implement this part of a program.

I am writing a word search puzzle generator that takes in a list of words and dimensions (such as "cat", "animal", "fish", etc...) and then populates the board in random directions with the words on the list, such as up, down, left, right, diagonal up, diagonal left, etc.

One of the things I'm having trouble with is having words that are too long to fit in the random spot they are assigned to. For example, if I had a 5x5 and had the word 'bear' and wanted to fit it as such below and wanted the word to wrap around the board, how would I calculate the way to do so?

A x x x x
x R x x x
x x x x x
x x x B x
x x x x E

I can't think of a way to do it accurately without having a switch statement that repeats tons of code over and over. I've heard a good way to do it would be to mod the value to wrap it around, but I am not too sure how to do this correctly. >_<

Could anyone please give me some insight as to a useful algorithm I could use to do this properly? The rest of my program works just fine; just the wrapping is giving me headaches.

Mucho thanks. ^^

So you got B at 4,4 and E at 5,5. Continue to increment the coordinates but check that its within range of the puzzle. So A at 6,6 is outside the range so subtract the maximum puzzle dimensions from it. So 6-5 = 1. Then A goes at 1,1 and R at 2,2.

Although, I've never seen a word search puzzle that wraps the words in such a way.

Sometime last night I figured out how to do it by using the modulus operator.

x = (x + 1)%boardSize;

Same for the y axis and x-1 with an error check for out of bounds for the opposite directions.

Yeah, it's weird. Our CS teacher likes making us do more complicated problems than normal.

Thanks for the help though.

This article has been dead for over six months. Start a new discussion instead.