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 héritage
4 objets créés : - Jumper - Mario qui hérite de Jumper - Peach qui hérite de Jumper - Level
- Loading branch information
1 parent
4b5a365
commit 16bc47e
Showing
4 changed files
with
114 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,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; | ||
} |
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_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; | ||
} | ||
} |
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_heritage; | ||
|
||
class Mario extends Jumper | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct('M'); | ||
} | ||
|
||
public function canJump(int $gapLength): bool | ||
{ | ||
return $gapLength <= 3; | ||
} | ||
} |
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_heritage; | ||
|
||
class Peach extends Jumper | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct('P'); | ||
} | ||
|
||
public function canJump(int $gapLength): bool | ||
{ | ||
return $gapLength >= 3 && $gapLength <= 5; | ||
} | ||
} |