简体   繁体   中英

Looking for a suitable data structure for manipulating player stats in a game

I'm planning to store player stats in a database for a game, and need to find a suitable data structure for testing against them in the code. The structure needs to work well in php, but I'm interested in any ideas for learning purposes.

In this first iteration of pseudo-code, I'm imagining multi-dimensional arrays for the data-structures, but don't think they're good enough. What other structures are available and how can the program access them efficiently as the number of stats and keys required to find each stat grow larger?

Pseudo-code: Everything is on a scale of 1 to 10

$player_base_skill_level
{
  [driving] = 4
  [running] = 6
}

$handicaps
{
  [driving][monster_trucks] = -5
  [running][long_distance] = -3
}

//User wants to drive a monster truck
$action[base_action] = 'driving'
$action[sub1_action] = 'monster_trucks'

function chance_of_success($action)
{
  $degree_of_difficulty = 8;
  $player_actual_skill_level = $player_base_skill_level[$base_action] + $handicaps[$base_action][$sub1_action];

  if($player_actual_skill_level > $degree_of_difficulty = 8)
    return 'success';
  else
    return 'failure';
}

The array keys will grow out of control in this solution as things get more complex; example: [driving][monster_trucks][large_wheels] or [driving][monster_trucks][small_wheels], so there must be a better way to structure the data.

Thanks

One thing that may be worthwhile is to generate a player statistics object. This object could either be a collection of "statistic objects" ie. handicaps, skills etc. If you do it this way you can encapsulate all of your work. For example, each object could have it's own chance_of_success method that you could call without having to worry about the specifics of that object.

I'm not sure which version of php you are using but the manual for classes/objects is here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM