We have a programming project due in which we are supposed to create a boggle board. We are supposed to read in a file with the boggle dice (basically just sixteen lines of six random letters), then ask the user if they want to shake the board. If they say yes, the board shuffles 250 times by choosing two random board positions and swapping their position; doing this 250 times. Once the position has been swapped, a side (one of the six letters) must randomly appear "face up" (i.e. the side that actually appears on the board). I don't want any code; i haven't even started coding the program yet. I just wanted some advice on the best way to approach this program. I know I'll need a function to read the dice from the file and place it in an array. I'm a little confused as to whether or not I just need one array to hold the "dice", or do I need sixteen different arrays; one for each die (since each die has to act separately)? I also know I'll need a function to output to the screen, and a function to shuffle the board. Thanks for your help.

## Recommended Answers

I'd put it in a 2D array. That way you can iterate over the rows and pick a random die face from each to face "up". Making your swaps with 16 different arrays could turn into a nightmare. I don't have too much to add besides that. Sounds like a …

rand() % 7 is going to get you numbers between 0 and 6 which will overstep the bounds of your array by 1 if a 6 is pulled.

I'm confused why you broke up the loop like you did?

rand() % 7 is going to get you numbers between 0 and 6 which will overstep the bounds of your array by 1 if a 6 is pulled.

I'm confused why you broke up the loop like you did?

That is correct, if you use rand() % 6 + 1 …

## All 11 Replies

I'd put it in a 2D array. That way you can iterate over the rows and pick a random die face from each to face "up". Making your swaps with 16 different arrays could turn into a nightmare. I don't have too much to add besides that. Sounds like a neat (and doable) project!

Definitely post back if you have any further questions about it and hopefully you'll get some other suggestions on this thread.

I agree with jonsca, use a 2D array. The rest is all up to you and VERY customizable. Depending on how crazy your prof is.... you could step through the array and randomize each dice using rand() generator limited by the amount of letters in alphabet.

So, I'm working on function to output to the screen. I read the dice into a 2d array. I'm trying to arrange the output into a 4x4 grid of letters. Each letter must be randomly chosen from one of the six letters on one of the sixteen dice. Everytime I compile and run it, most of the data is correct, however sometimes I get a letter which doesn't appear on that corresponding die. Can you tell me if there's something I'm doing wrong. This is just the function I'm working on.

``````void printScreen(char dice)
{
int i,x;
for(i=0;i<=3;i++)
{
x = rand() % 7;
cout << dice[i][x] << " ";
}
cout << endl;
for(i=4;i<=7;i++)
{
x = rand() % 7;
cout << dice[i][x] << " ";
}
cout << endl;
for(i=8;i<=11;i++)
{
int x = rand() % 7;
cout << dice[i][x] << " ";
}
cout << endl;
for(i=12;i<=15;i++)
{
x = rand() % 7;
cout << dice[i][x] << " ";
}
}``````

rand() % 7 is going to get you numbers between 0 and 6 which will overstep the bounds of your array by 1 if a 6 is pulled.

I'm confused why you broke up the loop like you did?

rand() % 7 is going to get you numbers between 0 and 6 which will overstep the bounds of your array by 1 if a 6 is pulled.

I'm confused why you broke up the loop like you did?

That is correct, if you use rand() % 6 + 1 you will be good. Also, when you define a function:

``void printScreen(char dice)``

You only need the second array size defined.

``void printScreen(char dice[])``

That is correct, if you use rand() % 6 + 1 you will be good. Also, when you define a function:

Yes, to get numbers from 1 to 6 that is what you would do, but he's stored them in array (zero based) so you don't need the +1.

Thanks for all your help, but I have one more question then my project should be complete, I just have to tidy it up a bit. The last thing that I'm having trouble with is pretty small. Whenever the letter Q is "face up" on the board, we're supposed to output "Qu". Since the dice are stored in a two dimensional character array, I was wondering if there is a way to convert from type char to type string. For instance, could I say

``````for(int i=0;i<16;i++)
for(int j=0;j<6;j++)
{
if(dice[i][j] == 'Q')
string  dice[i][j] = "Qu"
}``````

I know this code won't work; it was just to convey what I'm asking.

How do you display your other characters? I would just output the actual 'Q' on the die and add the extra character as if you were adding the next actual letter.

e.g.,

``````cout <<dice[i][j];
if(dice[i][j] == 'Q')
cout<<'u';``````

I know the solution to the random reordering has already been handled, but here is a solution that I find more mathematically satisfying.

1. Assign each die a random x, y coordinate
a. The coordinates should be real numbers (i.e. doubles )
b. If you prefer integers, then the range of possible values for each dimension should be significantly larger than the corresponding grid dimension and a multiple of the grid dimension.
For a 6x16 grid, use x=rand()%(6*6), y=rand()%(16*16)
2. Sort the dice by their y coordinate *in place*
3. Partition the list into h equally sized groups where h = height of dice grid
For a 6x16 grid, obviously partition into 16 groups of 6 coordinate pairs
4. Sort the dice in each partition *in place*
5. The list should represent a random distribution representing random re-ordering over a fixed size grid
So, for the die in group 4 at slot 2, it should go to Grid location row 4, column2

It may not be more efficient (haven't done the analysis), and it might be more troublesome to implement, but it is an interesting solution, no?

Thanks for all your help. My program is done.

That's an interesting concept by the way duskTreader. I can't exactly try it out now; my program finally works and it's due in four hours so I don't really want to mess with it lol. I might come back to it later though and try it out. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.