Friday, February 24

Board generator algorithm/pseudocode

[This post is subject to editing until the algorithm is perfect. -M]

Problem: 16 6-sided dice with random letters on them. Assign these letters to a 4x4 grid, with only one letter from each die in play.

Solution:
  1. Assign each die to a one-dimensional array

  2. Assign the board to a two-dimensional, 4x4 array of letters

  3. Order the dice in a linked list.

  4. Generate a random number from 1-[# of dice in array]. Remove that die from the field.

  5. With the selected die, generate a random number from 1-6 to determine the letter on that die. Place that letter in the next available grid location.

  6. Return to step 4 until the supply of dice is exhausted.


EDIT 2/27/2006:
That's what I planned, but this is what I did:
I didn't want to mess around with linked lists, because, well, my memory's not too strong on that, so I fudged it a bit, but I fudged it effectively. Basically, instead of a linked list, I made a seed string 'ABCDEFGHIJKLMNOP', then did a loop counting down to 1 from 16, and chose a random number between 1 and whatever the loop value was. I then took that letter number from the seed string, added it to another string, and deleted the letter from the seed string.
This resulted in the letters being scrambled. I then used that string, selected a die from the array, selected a random letter from that die, and put that into the array. I got some nice random grids, and so far, everything is working well.

No comments: