Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Résolution du challenge MARIO_1 avec une interface
4 objets créés : - Jumper - Mario qui implémente Jumper - Peach qui implémente Jumper - Level
- Loading branch information
1 parent
16bc47e
commit 093e081
Showing
4 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'; | ||
} | ||
} |