Jump to content

[SOLVED] Array help? (looping)


Recommended Posts

Basically I need help for looping arrays(?) for a game i'm making..actually i've had one for a quite a while but want to amp up the raiding and stuff. Items are done, etc. etc. (11k items to be exact :P) but i'm just stuck on how to get this effect...i've tried looping the arrays using there keys and if the $_ARRAY[$KEY] for example(a user) hp is equal to or below zero i unset the array, and well i'm just stumped because i'm not sure how to really do this..anyways to better explain heres some screenshots of the effect i'm trying to get.

 

Attack #1

ss1pk9.gif

 

Attack #2

ss2rw5.gif

 

Attack #3

ss3ys3.gif

 

So as you can tell basically what it does is it loops through however many people are in the raid, they attack, then the boss/mob attacks, and it calculates the damage given to the users and if their hp falls below 0 a skull is shown, and it loops through until either the boss/mob is dead, or all the users are dead..any help would be awesome, i'll try and post my earlier code up, it's on the other computer at the moment. If you need a more thorough explanation i will try my best to do so.

Link to comment
Share on other sites

Alright, basically it goes like this....

 

There are 5 players in the party attacking the boss..

 

Player 1 attacks

Player 2 attacks

Player 3 attacks

Player 4 Attacks

Player 5 attacks

 

Combine total damage...

 

Minus that damage from the boss

 

check to see if boss is dead...if not...continue on for the boss to attack back

 

boss attacks..

 

minus the boss's attack from the hp of the players

 

see if any of the players died...if so..display a skull

 

check to see if they all died..if so the raid was lost everyone(players) died

 

if not..repeat...

 

Basically that's as down to the point as I can get unfortunately. Hope that clears a little more confusion?

 

I just need to know how I would set something like this up...getting the players attack/hp is no problem but once I get that..how do I loop through and get those results...arggh..i'm pulling my hair out with this one. :P

Link to comment
Share on other sites

show us what your array looks like and we'll show you how to loop through it.

 

most of the it's something like this:

 

$players = array(

1 => array('name' => 'buddhasmak', 'hp' = 1200),

2 => array('name' => 'bobafrost', 'hp' = 1324),

3 => array('name' => 'tool', 'hp' = 0)

);

 

//count players

$cnt = count($players);

 

//the loop

for ($i = 0; $i < $cnt; $i++) {

  $player = $players[$i];

  $hp = $player['hp'];

 

  if ($hp <= 0) show_skull();

  $name = $player['name'];

}

 

 

Does that help?

Link to comment
Share on other sites

I'd be storing the users in a database and it will just be a list of the users separated by commas, and another row for their hp, stored relevant to the usernames position for example..

 

$usernames = "xnowandtheworldx,beer,phpfreaks,master,guardian";
$hp            = "100,324,987,1039,1948";
$array_user = explode(",", $usernames);
$array_hp    = explode(",", $hp);

//loop here

 

EDIT: I'll try that out mlin! Thanks for the help i'll let you know if it works! :)

Link to comment
Share on other sites

show us what your array looks like and we'll show you how to loop through it.

 

most of the it's something like this:

 

$players = array(

1 => array('name' => 'buddhasmak', 'hp' = 1200),

2 => array('name' => 'bobafrost', 'hp' = 1324),

3 => array('name' => 'tool', 'hp' = 0)

);

 

//count players

$cnt = count($players);

 

//the loop

for ($i = 0; $i < $cnt; $i++) {

  $player = $players[$i];

  $hp = $player['hp'];

 

  if ($hp <= 0) show_skull();

  $name = $player['name'];

}

 

 

Does that help?

thats a good idia but its better to store the results somewhere in a Database or a file . for each round so that the game can be made more dinamic for every one ... not only for the server :D

 

But mlin gave a god idia :) i mean each round you sa.dir.bg take away the lost HP from the specific user arrray. But make a some kind of a statistics for each round to make a "Top DMG " or "Top score" ;)

Link to comment
Share on other sites

Alright, one more thing..with the code you supplied theres an error in it, and I can't seem to find it lol.

 

Parse error: syntax error, unexpected '=', expecting ')' line 4

 

$players = array(
1 => array('name' => 'buddhasmak', 'hp' = 1200),
2 => array('name' => 'bobafrost', 'hp' = 1324),
3 => array('name' => 'tool', 'hp' = 0)
);

//count players
$cnt = count($players);

//the loop
for ($i = 0; $i < $cnt; $i++) {
   $player = $players[$i];
   $hp = $player['hp'];

   if ($hp <= 0) echo "DEAD!";
   $name = $player['name'];
}

Link to comment
Share on other sites

my bad. the hp need > signs like the other values...ie.

 

$players = array(

1 => array('name' => 'buddhasmak', 'hp' => 1200),

2 => array('name' => 'bobafrost', 'hp' => 1324),

3 => array('name' => 'tool', 'hp' => 0)

);

 

see the difference right? Sorry again, that was my bad. Let us know how it goes.

Link to comment
Share on other sites

Awsome! Works man. Just had to change it to a zero based counting with the array unless I wanted to add +1 to the end of the count(*) part. ;) But it works just gotta figure out how to loop it until everyone attacks, then have the boss attack, then have the players attack again, until someone wins. Joy. Haha. Thanks again though!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.