What a great little task. Below is the code I have come up with. I call it "Playing God" and in the example I build the universe. At the top I create the universe array with three variables to tell how many galaxies I want, how many solar systems and planets in each solar system. I then load the universe up with stuff and use a function I call "playGod" to specify three coordinates and an object to place at that location. As you can see, I have created my own planet called "Martyr's Planet". There we chase naked women, drink a lot, and program all day long while little newbies are our slaves! It is a wonderful planet.
Now to the code... check it out and it should be pretty straight forward for you.
php
<html>
<head>
<title>The Universe</title>
</head>
<body>
<?PHP
$universe = array();
$planetCount = 3;
$solarSystemCount = 5;
$galaxyCount = 10;
// Lets load up the universe array
function loadUniverse() {
global $galaxyCount;
global $universe;
// Create galaxy arrays and put them in the universe.
for ($i = 0; $i < $galaxyCount; $i++) {
$universe[] = createGalaxy();
}
}
// Create solar system arrays for each galaxy we create
function createGalaxy() {
global $solarSystemCount;
$galaxy = array();
for ($i = 0; $i < $solarSystemCount; $i++) {
$galaxy[] = createSolarSystem();
}
return $galaxy;
}
// Load each solar system with 3 planets (this of course can be of varying numbers)
function createSolarSystem() {
global $planetCount;
$system = array();
for ($i = 0; $i < $planetCount; $i++) {
$system[] = array("planet1", "planet2", "planet3");
}
return $system;
}
// Now lets play god and give it three coordinates and an object to place there
function playGod($x, $y, $z, $object){
global $universe;
$universe[$x][$y][$z] = $object;
}
// Load the universe
loadUniverse();
// Put Martyr's New kick ass DIC planet here at galaxy 3, solarsystem 2, planet 3 location
// Remember indexes start at zero!
playGod(2,1,2,"Martyr's Planet");
// Just print out the universe to show what we have.
print_r($universe);
?>
</body>
</html>
Essentially we have created a multidimensional jagged array coordinate system. It mimics what you would use for a true space coordinate system on three axis.
Just use the playGod function to place planets, ships, blackholes whatever at each coordinate. You can then loop through items one at a time or all at once.
Want to print the items at galaxy 3? Pull it out of the universe and iterate through it.
php
<?php
// Pull out galaxy 3
$galaxy3 = $universe[2];
// Loop through the solar systems and print how many planets it has
foreach ($galaxy3 as $key => $value) {
echo "Solar System: $key has ". count($value) . " planets! <br/>";
}
?>
That should be all you need to get started being your own god of the universe! Enjoy!
"At DIC we be gods every day, when we are not ninjas of course!"