Jump to content

PHP fighting game help


busby

Recommended Posts

hey all...ive been given a task to do using PHP...my knowledge of PHP isnt great but i know how to create websites and connect to a database etc and i think the people who gave me the task thought Id find this task easier than i really will......anyway...been pondering over this for couple of hours now and i cant figure out where to start....could someone please explain a method of doing this? at least help with where to begin?

 

here is the task

 

Mobile Fun Coding Challenge

Create the following game:

 

You set of warriors, each warrior has the following data:

● Name

● Health

● Attack

● Defence

● Speed

● Evade

 

Health, Attack, Defence and Speed are integer values between 0 and 100. Evade is a

number between 0 and 1.

 

There are currently 3 types of warrior:

● Ninja

○ Health (40-60)

○ Attack (60-70)

○ Defence (20-30)

○ Speed (90-100)

○ Evade (0.3-0.5)

 

● Samurai

○ Health (60-100)

○ Attack (75-80)

○ Defence (35-40)

○ Speed (60-80)

○ Evade (0.3-0.4)

 

● Brawler

○ Health (90-100)

○ Attack (65-75)

○ Defence (40-50)

○ Speed (40-65)

○ Evade (0.3-0.35)

 

The values for each attribute are specified as a range, on creation each warrior will be

assigned a random value within this range for the given attribute.

 

Warriors can take part in a battle, a battle has only 2 combatants. Warriors take it in turn to

attack each other. The warrior with the greatest speed takes the first attack (in the event of

two with the same speed, player with the lower defence goes first).

 

The damage dealt is (attack - oppositions defence) and is taken from the health. Damage

may be avoided, the chance of avoiding an attack is specified by the evade attribute.

 

The max amount of turns is 30 per player, if no player is defeated then the battle is a draw.

As the battle progresses a text output should be shown. When a warrior gets to zero health

the warrior is defeated.

 

Special attributes of warriors:

● Ninja

With each attack there is a 5% chance of doubling the attack strength.

 

● Samurai

When a samurai evades an attack there is a 10% chance of regaining 10 health.

 

● +Brawler

○ When a brawler’s health falls to less than 20% their defence increases by 10.

 

Allow a form to select player 1 and 2, and when submitted create a battle with the 2 players

and output the result of the battle!!

Link to comment
Share on other sites

is it right that ive only got untill tomorrow morning to do this? is that possible?

 

also...do you need a logon? cant you just go on the game and choose which one of the 3 chars to use? is this turn based thing automatic? or do you attack at the push of a button? is your opponent another person? or computer player? the task is very vague and my knowledge on anything that can be used to do this is very basic its just out of my league

Link to comment
Share on other sites

It's passed your due date but I found it interesting to try it out myself:

 

class BattleRecorder
{
  private static $instance;
  
  private function __construct() {}
  private function __clone() {}
  
  public static function get() {
    if (is_null(self::$instance)) {
      self::$instance = new self();
    }
    return self::$instance;
  }
  
  public function record($msg) {
    echo $msg, PHP_EOL;
  }
}

abstract class Warrior
{
  public $name;
  public $health;
  public $attack;
  public $defence;
  public $speed;
  public $evade;
  
  public $turns;
  public $recorder;
  
  public static function get($warrior, $name) {
    $className = ucfirst(strtolower($warrior));
    return new $className($name);
  }
  
  public function __construct($name) {
    $this->name = $name;
    
    $this->_init();
  }
  
  public function isDefeated() {
    return !$this->isAlive() || !$this->hasTurnsLeft();
  }
  
  public function isAlive() {
    return $this->health > 0;
  }
  
  public function hasTurnsLeft() {
    return $this->turns > 0;
  }
  
  public function evadedAttack() {
    return $this->evade == mt_rand(1, 100) / 100;
  }
  
  public abstract function attack(Warrior $w);
  public abstract function defend($attack);
  
  protected abstract function _init();
}

class Ninja extends Warrior
{
  public function attack(Warrior $w) {
    --$this->turns;
    
    $damage = $this->attack * (0.05 == mt_rand(1,100) / 100 ? 2 : 1);
    
    $this->recorder->record($this->name . ' dealt ' . $damage . ' damage.');
    
    $w->defend($damage);
  }
  
  public function defend($attack) {
    if ($this->evadedAttack()) {
      $this->recorder->record($this->name . ' evaded the attack.');
      return;
    }
    
    $damage = $attack - $this->defence;
    if ($damage > 0)
      $this->health -= $damage;
      
    $this->recorder->record($this->name . ' defended, but lost ' . $damage . ' health.');
  }
  
  protected function _init() {
    $this->health = mt_rand(40, 60);
    $this->attack = mt_rand(60, 70);
    $this->defence = mt_rand(20, 30);
    $this->speed = mt_rand(90, 100);
    $this->evade = mt_rand(30, 50) / 100;
  }
}

class Arena
{
  public static function fight(Warrior $w1, Warrior $w2) {
    $w1->turns = 30;
    $w1->recorder = BattleRecorder::get();
    
    $w2->turns = 30;
    $w2->recorder = BattleRecorder::get();
    
    list($w1, $w2) = self::drawFirstBlood($w1, $w2);
    while (!$w1->isDefeated() && !$w2->isDefeated()) {
      $w1->attack($w2);
      $w2->attack($w1);
    }
    
    if ($w1->isAlive() && $w2->isAlive())
      BattleRecorder::get()->record('draw!');
    elseif ($w1->isAlive())
      BattleRecorder::get()->record($w1->name . ' won!');
    else
      BattleRecorder::get()->record($w2->name . ' won!');
  }
  
  protected static function drawFirstBlood(Warrior $w1, Warrior $w2) {
    $r = array($w1, $w2);
    if ($w1->speed > $w2->speed) {
      $w1->attack($w2);
      $r = array($w2, $w1);
    } else if ($w1->speed == $w2->speed) {
      if ($w1->defence < $w2->defence) {
        $w1->attack($w2);
        $r = array($w2, $w1);
      } else {
        $w2->attack($w1);
      }
    } else {
      $w2->attack($w1);
    }
    return $r;
  }
}

 

Call/use like:

 

Arena::fight(
  Warrior::get('Ninja', 'Foo'),
  Warrior::get('Ninja', 'Bar')
);

 

Brawler and Samurai are not implemented so that's up to you. To get a specific type use Warrior::get() as demonstrated, you can pass this from the form to the function. Note that it has no error checking.

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.