Welcome to Dream.In.Code
Become a PHP Expert!

Join 136,921 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,777 people online right now. Registration is fast and FREE... Join Now!




PHP Random Grid

 
Reply to this topicStart new topic

PHP Random Grid

JBrace1990
10 Mar, 2008 - 05:20 PM
Post #1

D.I.C Regular
Group Icon

Joined: 9 Mar, 2008
Posts: 477



Thanked: 22 times
Dream Kudos: 350
My Contributions
I need help making a random grid in PHP... i'm not exactly sure how to do it, except for using mt_rand() a few times to come up with random locations....

i'm thinking of storing it as x0:x:x, though again, not sure how to do it....

maybe an mt_rand for the first, second, and third sets? the only problem then is that I want a universe of 30 galaxies, 9 solar systems, and 9 planets (or to be varied), and while some of them have locations in them, some of them shouldn't.... leaving me with the problem i'm having right now.... so, could someone be able to help me somehow?

Thank you to anyone who helps...
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: PHP Random Grid
10 Mar, 2008 - 05:36 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,231



Thanked: 220 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Can you define what you mean by grid? Are you talking about actual lined grid like tic tac toe? Or are you talking a grid like a matrice or multi-dimensional array? What exactly are you attempting to do?

Any clarification you can provide would be of use. Thanks! smile.gif
User is online!Profile CardPM
+Quote Post

JBrace1990
RE: PHP Random Grid
10 Mar, 2008 - 05:43 PM
Post #3

D.I.C Regular
Group Icon

Joined: 9 Mar, 2008
Posts: 477



Thanked: 22 times
Dream Kudos: 350
My Contributions
ah, of course....

basically, it would be a few dimensions.... each would contain 9 planets, some slots empty.... so maybe only 4 planets each, some might have more.... i need to make several of them (270),and I basically need a way to refer to them with coordinates, such as 1:12:3., which would mean galaxy 1, solar system 12, planet 3.... for this example, there would be 4 planets, one in 1, 3, 6, and 8.... the rest would be empty....
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: PHP Random Grid
10 Mar, 2008 - 06:24 PM
Post #4

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,231



Thanked: 220 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
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!" decap.gif




User is online!Profile CardPM
+Quote Post

JBrace1990
RE: PHP Random Grid
10 Mar, 2008 - 06:44 PM
Post #5

D.I.C Regular
Group Icon

Joined: 9 Mar, 2008
Posts: 477



Thanked: 22 times
Dream Kudos: 350
My Contributions
ok, whenever I do that, I wind up with it showing the array contents.... I'd go about showing it in a table by putting in $array[1];, and then the other values into table, right?

I want something where it'd show it in a table, and someone would be able to go through the tables one at a time and view it...

EDIT: Also, I want to store it into a MySQL database.... so i'd have something similar to "INSERT INTO universe VALUES $array[0] => $array[0] => $array[0]", rhough i'm not sure how i'd be able to do that without typing it all (and i know that's not exactly how it should be done)

This post has been edited by JBrace1990: 10 Mar, 2008 - 06:49 PM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: PHP Random Grid
10 Mar, 2008 - 07:39 PM
Post #6

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,231



Thanked: 220 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Ok well I am not going to do ALL your work for you. I already told you what you need to do to iterate through the contents of your universe. Simply remove the place where it has print_r($universe) because that was to show you the contents.

Then you can pull out each galaxy from the universe and iterate through solar systems, planets etc, printing table cells etc. If you don't know how to print a standard table then I recommend you read up more on how to use loops and echoing <td> tags in the loop.

For entering this data into a database that will involve a whole new deal because you will have to build tables and relate them to one another. A system like this isn't going to be done (and shouldn't be done) in a single database table. You will have at least 3 tables, one for galaxies, one for solar systems and one for planets/objects. Each linked together with a one to many relationship.

You can then iterate through this array I have created you and execute insert queries for each level (aka each loop of the three main functions) and that should get you moving in the right direction.

smile.gif


User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/3/08 10:13PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month