Chris Stevenson
| >
>I'd figure this might help in figuring out the error. Also, what part of my program is causing it, so I can figure out a workaround thus far.
>
>
>begin 0644 classgen.d
aheh... oops... here:
classgen.d:
----------------------------------------------------------
import std.random;
import std.c.stdio;
import diceroll;
enum { //PHB character classes
BARBARIAN = 1,
BARD,
CLERIC,
DRUID,
FIGHTER,
MONK,
PALADIN,
RANGER,
ROGUE,
SORCERER,
WIZARD
}
//diceRoll modes
const int NORMAL_ROLL = 3;
const int ELITE_ROLL = 4;
const int EXCEP_ROLL = 5;
const int TWINK_ROLL = 6;
// Yeah, this is a little confusing below. These integers represent the
relative
// priority of the ability scores, by class. These are in the order of the
// AbilityScores enum type above, with a lower number indicating a greater
priority.
const int [6] [int.max] abiPriority =
[ [1, 2, 2, 3, 3, 3], // Barbarian
[3, 2, 3, 2, 3, 1], // Bard
[3, 3, 2, 3, 1, 2], // Cleric
[3, 2, 2, 3, 1, 3], // Druid,
[1, 2, 2, 3, 3, 3], // Fighter
[2, 2, 3, 3, 1, 3], // Monk
[2, 3, 3, 3, 2, 1], // Paladin
[2, 1, 3, 3, 2, 3], // Ranger
[3, 1, 3, 2, 3, 2], // Rogue
[3, 2, 3, 2, 3, 1], // Sorcerer
[3, 2, 2, 1, 3, 3] ]; // Wizard
class Character
{
invariant
{
//Check that the character to be generated is between level 1
//and level 20.
assert (0 < minLevel && minLevel <= maxLevel && maxLevel <= 20);
int totalLevel;
foreach(int level; classLevels)
{
assert(level > 0 && level <= 20);
totalLevel += level;
}
assert(totalLevel <= 20);
}
private:
int [int] classLevels;
int[6] abilityScores;
public:
int minLevel = 1;
int maxLevel = 1;
void pickClass( int c )
{
classLevels[c] = rand() % (maxLevel - minLevel + 1);
}
void pickClass(int[] c)
{
int i;
for (i=0; i < c.length; i++)
classLevels[c[i]] = rand() % ( (maxLevel - minLevel + 1) / c.length);
}
void generateAbilityScores(int rollMode)
{
int[6] priority;
int[6] scores;
int[] chosen = classLevels.keys;
foreach( int c; chosen)
{
for (int stupidCtr; stupidCtr < priority.length; stupidCtr++)
priority[stupidCtr] += abiPriority[c][stupidCtr];
}
Dice dice = new Dice();
dice.diceSize = 6;
switch (rollMode)
{
case NORMAL_ROLL:
dice.diceNum = 3; // 3d6, straight up
break;
default: ///D compiler may not like this
case ELITE_ROLL:
dice.diceNum = 4; // 4d6, drop lowest
dice.dropLowest = 1;
break;
case EXCEP_ROLL: // 5d6 drop 2 lowest
dice.diceNum = 5;
dice.dropLowest = 2;
break;
case TWINK_ROLL: // 6d6, drop 3 lowest, reroll 1s and 2s
dice.diceSize = 4;
dice.diceNum = 6;
dice.dropLowest = 3;
dice.diceMod = 6;
break;
}
dice.rollMultiple (scores[]);
scores.sort;
// This findPriority() turned out a little ugly. Here's the idea:
// priority[] holds the relative priority of the 6 ability scores,
// with a lower number indicating a more important priority. findPriority()
// finds the index in priority[] with the smallest number, sets the priority
// of than index to int.max (to remove in from future consideration), and
returns
// that index. If there's a tie between two or more indicies, findPriority()
// randomly chooses between them.
int findPriority()
{
int lowest = int.max;
int []result;
int indexResult;
foreach (int abiIndex,int current; priority)
if (current <= lowest)
{
if (current < lowest) // this will also reset result[0]
{ // if it found a tie before
lowest = current;
result.length = 1;
result[0] = abiIndex;
}
else
{
result.length = result.length + 1;
result[result.length-1] = abiIndex;
}
}
if (result.length > 1)
indexResult = rand() % result.length;
priority[result[indexResult]] = int.max;
return result[indexResult];
}
int scoresIndex;
int i;
for (i=0; i < abilityScores.length; i++)
abilityScores[findPriority()] = scores[scoresIndex++];
return;
}
void printOutput()
{
char[][int] classString;
classString[BARBARIAN] = "Barbarian";
classString[BARD] = "Bard";
classString[CLERIC] = "Cleric";
classString[DRUID] = "Druid";
classString[FIGHTER] = "Fighter";
classString[MONK] = "Monk";
classString[PALADIN] = "Paladin";
classString[RANGER] = "Ranger";
classString[ROGUE] = "Rogue";
classString[SORCERER] = "Sorcerer";
classString[WIZARD] = "Wizard";
foreach (int c, int level; classLevels)
printf(" %.*s %d\n" , classString[c] , level);
printf("STR = %d\n", abilityScores[0]);
printf("DEX = %d\n", abilityScores[1]);
printf("CON = %d\n", abilityScores[2]);
printf("INT = %d\n", abilityScores[3]);
printf("WIS = %d\n", abilityScores[4]);
printf("CHA = %d\n", abilityScores[5]);
}
}
/// FOR TESTING ONLY
int main()
{
Character me = new Character();
me.maxLevel = 5;
me.pickClass(FIGHTER);
me.generateAbilityScores(ELITE_ROLL);
me.printOutput();
return 0;
}
|