Coding for the hell of it.

Coding for the hell of it.

I complain about tutorial / code tips not offering any real-world value, but then I also write (and publish) stuff like this while I'm waiting for my air fryer to finish cooking my chicken.

No idea why. Boredom maybe?

<?php

interface MarvelCharacterInterface
{
    public function attack(): string;
    public function limitBreak(): string;
}

class CaptainAmerica implements MarvelCharacterInterface
{
    public int $power = 5;
    public int $hp = 60;

    public function attack(): string
    {
        return 'Shield attack!';
    }

    public function limitBreak(): string
    {
        return '"I can do this all day.." PUNCH!';
    }
}

class Ironman implements MarvelCharacterInterface
{
    public int $power = 6;
    public int $hp = 60;

    public function attack(): string
    {
        return 'Repulsor Beam!';
    }

    public function limitBreak(): string
    {
        return 'ARC REACTOR EXPLOSION!?!';
    }
}

class Thor implements MarvelCharacterInterface
{
    public int $power = 8;
    public int $hp = 80;

    public function attack(): string
    {
        return 'Thundeerrrrr!!';
    }

    public function limitBreak(): string
    {
        return 'GODBLAST IN YOUR FACE!!';
    }
}

class Thanos implements MarvelCharacterInterface
{
    public int $power = 15;
    public int $hp = 100;

    public function attack(): string
    {
        return 'Big Punch!';
    }

    public function limitBreak(): string
    {
        return 'Infinity Beam!!!!';
    }
}

interface BattleInterface
{
    public function addMember(MarvelCharacterInterface $character): self;
    public function getBattleReport(): array;
    public function fight(MarvelCharacterInterface $enemy): bool;
}

class Avengers implements BattleInterface
{
    public array $squad = [];
    public array $battleReport = [];

    public function addMember(MarvelCharacterInterface $character): self
    {
        $this->squad[] = $character;

        return $this;
    }

    public function getBattleReport(): array
    {
        return $this->battleReport;
    }

    public function fight(MarvelCharacterInterface $enemy): bool
    {
        while(true) {

            $avenger = rand(0, count($this->squad) -1);
            $attack = rand(0, 1);

            $currentAvenger = $this->squad[$avenger];
            $damage = $currentAvenger->power * ($attack + 1);

            $action = $currentAvenger::class . " attacks " . $enemy::class . ' with ';
            $action .= $attack === 0 ? $currentAvenger->attack() : $currentAvenger->limitBreak();
            $action .= ' ' . $enemy::class . ' loses ' . $damage . 'HP';

            $this->battleReport[] = $action;

            $enemy->hp -= $damage;

            if ($enemy->hp <= 0) {
                $this->battleReport[] = $enemy::class . ' is DEAD.';
                return true;
            }

            $avenger = rand(0, count($this->squad) -1);
            $attack = rand(0, 1);

            $currentAvenger = $this->squad[$avenger];
            $damage = $enemy->power * ($attack + 1);

            $action = $enemy::class . " attacks " . $currentAvenger::class . ' with ';
            $action .= $attack === 0 ? $enemy->attack() : $enemy->limitBreak();
            $action .= ' ' . $currentAvenger::class . ' loses ' . $damage . 'HP';

            $this->battleReport[] = $action;

            $this->squad[$avenger]->hp -= $damage;

            if ($this->squad[$avenger]->hp <= 0) {
                unset($this->squad[$avenger]);
                $this->squad = array_values($this->squad);
                $this->battleReport[] = $currentAvenger::class . ' is DEAD.';
            }

            if (count($this->squad) === 0) {
                $this->battleReport[] = $enemy::class . ' won with ' . $enemy->hp . 'HP';
                return false;
            }

        }

        return false;
    }
}

$avengers = new Avengers();

$result = $avengers
    ->addMember(new Ironman())
    ->addMember(new CaptainAmerica())
    ->addMember(new Thor())
    ->fight(new Thanos());

print_r($avengers->getBattleReport());

if (!$result) {
    echo "The Avengers were defeated...\n";
} else {
    echo "The Avengers have WON.\n";
}        

Ok lets do this! First to 2 is the winner.

Avengers 0 - 1 Thanos.

Avengers 1 - 1 Thanos

Avengers 2 - 1 Thanos

YEESSSSS!!!!




要查看或添加评论,请登录

Stuart Todd的更多文章

  • What is code rot?

    What is code rot?

    Code rot aka Technical debt, builds up over time. It is the process where the quality of the code deteriorates.

社区洞察

其他会员也浏览了