Letter Grid Randomizer
For the very same Word Racer clone I had to write a grid randomizer that would be able to customize the frequency of vowels, a max instance count for each vowel, consonant and special character ( less frequently word encountered consonants ).
Sounds complicated ? Let's see
You will have to watch a particular case which could generate an infinite loop which occurs when
Sounds complicated ? Let's see
// precalcsOf course this code won't compile. Of course there may be better ways of doing it. Of course I hate pure pseudocode.
charsToGenerate = x;
vowelFrequency = [0.0 - 1.0]
neededVowels = charsToGenerate * vowelFrequency
neededConsonants = charsToGenerate - neededVowels
// iteration
for ( i = 0; i < charsToGenerate; i++ ) {
randomLetter = GetRandomChar();
}
// selectors
char GetRandomChar() {
if ( neededVowels > 0 && neededConsonants > 0) {
if ( rand() & 1 ) {
return chooseVowel();
} else {
return chooseConsonant();
}
} else {
if ( neededConsonants > 0 ) {
return chooseConsonant();
} else {
return chooseVowel();
}
}
}
char chooseVowel() {
select random vowel
while ( vowel's InstanceCount >= maxVowelInstanceCount ) {
select random vowel
}
increment vowel's InstanceCount
neededVowels--
return vowel
}
char chooseConsonant() {
select random consonant
done = false
while ( !done ) {
if ( IsSpecialChar( consonant ) ) {
if ( instanceCount < maxSpecialCharInstanceCount ) {
done = true
} else {
select random consonant
}
} else {
if ( instanceCount < maxConsonantInstanceCount ) {
done = true
} else {
select random consonant
}
}
}
increment consonant's InstanceCount
neededConsonants--
return consonant
}
You will have to watch a particular case which could generate an infinite loop which occurs when
( neededVowels * maxVowelInstanceCount + nrOfSpecialChars * maxSpecialCharInstanceCount + ( nrOfConsonants - nrOfSpecialChars ) * maxConsonantInstanceCount ) < charsToGenerate