Skip to content

Commit

Permalink
Résolution du challenge MARIO_1 avec héritage
Browse files Browse the repository at this point in the history
4 objets créés :
- Jumper
- Mario qui hérite de Jumper
- Peach qui hérite de Jumper
- Level
  • Loading branch information
TainixCode committed Oct 25, 2023
1 parent 4b5a365 commit 16bc47e
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 0 deletions.
13 changes: 13 additions & 0 deletions challenges/MARIO_1_heritage/Jumper.php
@@ -0,0 +1,13 @@
<?php
declare(strict_types=1);

namespace Challenges\MARIO_1_heritage;

abstract class Jumper
{
public function __construct(
public readonly string $name
) {}

abstract public function canJump(int $gapLength): bool;
}
67 changes: 67 additions & 0 deletions challenges/MARIO_1_heritage/Level.php
@@ -0,0 +1,67 @@
<?php
declare(strict_types=1);

namespace Challenges\MARIO_1_heritage;

class Level
{
private const PLATFORM = 'P';

private string $sequence = '';
private Jumper $currentJumper;

public function __construct(
private string $platfoms,
private Jumper $jumper1,
private Jumper $jumper2
) {
$this->currentJumper = $jumper1;
}

public function run(): void
{
$gaps = explode(self::PLATFORM, $this->platfoms);

foreach ($gaps as $gap) {

$gapLength = strlen($gap);

if ($gapLength === 0) {
// Bords, personne ne saute
continue;
}

// Si les 2 peuvent sauter
if ($this->jumper1->canJump($gapLength) && $this->jumper2->canJump($gapLength)) {
// On incrémente la séquence avec le jumper courant
$this->sequence .= $this->currentJumper->name;

// On change de currentJumper
if ($this->currentJumper->name === $this->jumper1->name) {
$this->currentJumper = $this->jumper2;
} else {
$this->currentJumper = $this->jumper1;
}

continue;
}

if ($this->jumper1->canJump($gapLength)) {
$this->sequence .= $this->jumper1->name;

continue;
}

if ($this->jumper2->canJump($gapLength)) {
$this->sequence .= $this->jumper2->name;

continue;
}
}
}

public function getSequence(): string
{
return $this->sequence;
}
}
17 changes: 17 additions & 0 deletions challenges/MARIO_1_heritage/Mario.php
@@ -0,0 +1,17 @@
<?php
declare(strict_types=1);

namespace Challenges\MARIO_1_heritage;

class Mario extends Jumper
{
public function __construct()
{
parent::__construct('M');
}

public function canJump(int $gapLength): bool
{
return $gapLength <= 3;
}
}
17 changes: 17 additions & 0 deletions challenges/MARIO_1_heritage/Peach.php
@@ -0,0 +1,17 @@
<?php
declare(strict_types=1);

namespace Challenges\MARIO_1_heritage;

class Peach extends Jumper
{
public function __construct()
{
parent::__construct('P');
}

public function canJump(int $gapLength): bool
{
return $gapLength >= 3 && $gapLength <= 5;
}
}

0 comments on commit 16bc47e

Please sign in to comment.