Skip to content

Commit

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

namespace Challenges\MARIO_1_interfaces;

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

namespace Challenges\MARIO_1_interfaces;

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->getName();

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

continue;
}

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

continue;
}

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

continue;
}
}
}

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

namespace Challenges\MARIO_1_interfaces;

class Mario implements Jumper
{
public function canJump(int $gapLength): bool
{
return $gapLength <= 3;
}

public function getName(): string
{
return 'M';
}
}
17 changes: 17 additions & 0 deletions challenges/MARIO_1_interfaces/Peach.php
@@ -0,0 +1,17 @@
<?php
declare(strict_types=1);

namespace Challenges\MARIO_1_interfaces;

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

public function getName(): string
{
return 'P';
}
}

0 comments on commit 093e081

Please sign in to comment.