Re: First bit of PHP in a while

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

 




On 02/06/2021 02:36, Leam Hall wrote:
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)


I just saw this, thought I would offer my 2cent/pence/other.


One main thing you could probably improve is by using objects and classes across this. For example, the functions to do with generating stats could belong to a CharacterGenerate class. This way, your logic is better encapsulated, and you don't need to worry about duplication of function/method names when your code grows.

Likewise, your data structures could also become real class instances instead of multi-dimensional arrays. This makes them easier to work with in the future, and more flexible if you wish to extend characters with other attributes, as you're not relying on attribute positions in an array:

class CharacterStats
{
    public int $strength;
    public int $intelligence;
    public int $wisdom;
   ...
}

class CharacterClass extends SplEnum
{
    const __default = self::Cleric;

    const Cleric = 1;
    const Paladin = 2;
    const Thief = 3;
    ...
}

class Character
{
    public CharacterStats $stats;
    public string $name;
    public CharacterClass $characterClass;
   ...
}


It might look a lot more complicated at first glance, but it will make your code more readable overall, and it will a lot easier to extend and add features in the future, which is likely for a D&D character generator ;)



[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