First bit of PHP in a while

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



and I could use your thoughts on improving the code.The end state is a
web form that generates a D&D type character, and takes into account
class minimums, etc. This is my first effort.


####
<?php

//  USS Chargen


// Data structures
$prefs = array(
  "Cleric"    => array("Wisdom"),
  "Paladin"   => array("Charisma", "Wisdom", "Strength"),
  "Thief"     => array("Dexterity"),
  );

$mins = array(
  "Paladin"   => array("Charisma" => 17, "Wisdom" => 12, "Strength" => 12),
  "Cleric"    => array("Wisdom" => 9),
);

$character    = array(
  "Stats" => array("Strength" => 0, "Intelligence" => 0, "Wisdom" => 0,
    "Dexterity" => 0, "Constitution" => 0, "Charisma" => 0),
  "Rolls" =>  gen_stats(),
);


// Functions

function gen_stats(){
  // Generate a set of six 3d6 rolls.
  $stats  = array();
  $x      = 0;
  for( $x = 0; $x < 6; $x++) {
    $roll = random_int(1,6) + random_int(1,6) + random_int(1,6);
    array_push($stats, $roll);
  }
  sort($stats);
  return $stats;
}

function print_character($character){
  // The end product.
  foreach ( $character['Stats'] as $s => $s_value ){
    echo $s . ":  " . $s_value . "\n";;
  }
  echo "\n";
}

function arrange_for_class($character, $prefs, $class){
  // Character classes have preferred stats. The prefs array lists them in
  //  order of preference, and the array_pop takes the last (highest)
  //  value and assigns it.
  foreach ( $prefs[$class] as $p ){
    $character['Stats'][$p] = array_pop($character['Rolls']);
  }
  return $character;
}

function minimums_for_class($character, $mins, $class){
  // Certain character classes have minimum stats.
  // Since this character is of the class, they must have at least
  //  the minimum.
  foreach ( $mins[$class] as $m => $m_value ){
    $character['Stats'][$m] = max( $character['Stats'][$m], $m_value);
  }
  return $character;
}

function finish_stat_assignment($character){
  // Randomly assign the rest of the stats.
  shuffle($character['Rolls']);
  foreach ( $character['Stats'] as $s => $s_value ){
    if ( $s_value == 0 ){
      $character['Stats'][$s] = array_pop($character['Rolls']);
    }
  }
  return $character;
}


// Main

$class         = "Paladin";
$character  = arrange_for_class($character, $prefs, $class);
$character  = minimums_for_class($character, $mins, $class);
$character  = finish_stat_assignment($character);
print_character($character);

?>

####

Leam

Site Reliability Engineer        (reuel.net/resume)
Scribe: The Domici War        (domiciwar.net)
General Ne'er-do-well           (github.com/LeamHall)



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux