Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7920135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:06:14+00:00 2026-06-03T16:06:14+00:00

class Gunner { public $health; public $attack; function __construct($health, $attack) { $this->health = $health;

  • 0
class Gunner {

    public $health;
    public $attack;

    function __construct($health, $attack) {
        $this->health = $health;
        $this->attack = $attack;
    }
}

$player = array();
$player[] = new Gunner(100, 20);
$player[] = new Gunner(100, 20);
$player[] = new Gunner(100, 20);

$enemy = array();
$enemy[] = new Gunner(100, 20);
$enemy[] = new Gunner(100, 20);

I want to have some while loop that run through as long as the both arrays has “entities” / objects. How do I do that?
I want to fight each and every entity, like $player[0] would be fighting (aka do a rand(1,20)) and then remove from the opposites health until its 0. And when its 0 or less I would remove the entity (object) from the array.

I am not sure how the while loop or the delete from array would look like.

while ((count($attacker) > 0) && (count($defender) > 0))
{
    $attacker_attack = rand(1, 25);

    $defender[0]->health -= $attacker_attack;

    if (!$defender[0]->IsAlive()) {
        unset($defender[0]);
        array_values($defender);
    }

    $defender_attack = rand(1, 20);

    $attacker[0]->health -= $defender_attack;

    if (!$attacker[0]->IsAlive()) {
        unset($attacker[0]);
        array_values($attacker);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T16:06:15+00:00Added an answer on June 3, 2026 at 4:06 pm

    You mean something like this (demo)?

    class Gunner
    {
        public $health;
        public $attack;
    
        public function __construct($health, $attack)
        {
            $this->health = $health;
            $this->attack = $attack;
        }
    }
    
    $attacker = array
    (
        new Gunner(100, 20),
        new Gunner(100, 20),
        new Gunner(100, 20),
    );
    
    $defender = array
    (
        new Gunner(100, 30),
        new Gunner(100, 30),
    );
    
    while ((count($attacker) > 0) && (count($defender) > 0)) // fight till death!
    {
        $defender[0]->health -= $attacker[0]->attack;
    
        if ($defender[0]->health <= 0) // defender dead?
        {
            unset($defender[0]); $defender = array_values($defender);
        }
    
        if (count($defender) > 0) // are any def alive for counter-attack?
        {
            $attacker[0]->health -= $defender[0]->attack;
    
            if ($attacker[0]->health <= 0) // attacker dead?
            {
                unset($attacker[0]); $attacker = array_values($attacker);
            }
        }
    }
    
    print_r($attacker);
    print_r($defender);
    

    PS: I updated the code to reflect your last comment, it’s kinda unclear how the turns should be played.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

class MyClass { var $lambda; function __construct() { $this->lambda = function() {echo 'hello world';};
class MyClass<T> where T: new() { public Test() { var t = new T();
class C { public: C(C& c) { i = c.i; j = 100; }
class LinkedList{ private $first; public function Merge(LinkedList $ll){ //We can't access $ll->first for merging
class Size{ Size(){ } //public final Size SMALL = new Size();//infinite loop final Size
class Foo { public: Foo() { do_something = &Foo::func_x; } int (Foo::*do_something)(int); // function
class A { public: int i; }; A *a = new A(); How to
class ZiggyTest{ public static void main(String[] args){ RunnableThread rt = new RunnableThread(); Thread t1
I have a JSON response that looks like this. I want to extract the
class Object { }; Class Derived : public Object { }; boost::shared_ptr<Object> mObject(new Derived);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.