views:

48

answers:

1

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

+2  A: 

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

GWW
The php version is 5.2
JMC
Then classes/objects are definitely what you should look into. You may need to learn some basic object oriented programming before you start though. http://en.wikipedia.org/wiki/Object-oriented_programming
GWW
I second learning OOP so you can use classes/members/methods to create objects, rather than using global multidimensional arrays.
Jeff Standen
@GWW +1 this is a good suggestion. I planned to use OOP within the bounds of my comfort zone for the final iteration of the game..., but re-reading the wikipedia article made me realize I haven't explored abstraction to it's fullest possibilities.
JMC
It's extremely worthwhile to do so. Most if not all of the major game engines out there use OOP. IT will save you so much time in the long run. Just don't go too crazy with it.
GWW