...@@ -14,14 +14,14 @@ class PartieSquadro { ...@@ -14,14 +14,14 @@ class PartieSquadro {
public string $gameStatus = 'initialized'; public string $gameStatus = 'initialized';
public PlateauSquadro $plateau; public PlateauSquadro $plateau;
// 🔹 Constructeur : Crée une nouvelle partie avec un premier joueur // Constructeur : Crée une nouvelle partie avec un premier joueur
public function __construct(JoueurSquadro $playerOne) { public function __construct(JoueurSquadro $playerOne) {
$this->joueurs[self::PLAYER_ONE] = $playerOne; $this->joueurs[self::PLAYER_ONE] = $playerOne;
$this->joueurActif = self::PLAYER_ONE; $this->joueurActif = self::PLAYER_ONE;
$this->plateau = new PlateauSquadro(); $this->plateau = new PlateauSquadro();
} }
// 🔹 Ajoute un deuxième joueur à la partie // Ajoute un deuxième joueur à la partie
public function addJoueur(JoueurSquadro $player): void { public function addJoueur(JoueurSquadro $player): void {
if (!isset($this->joueurs[self::PLAYER_TWO])) { if (!isset($this->joueurs[self::PLAYER_TWO])) {
$this->joueurs[self::PLAYER_TWO] = $player; $this->joueurs[self::PLAYER_TWO] = $player;
...@@ -31,32 +31,32 @@ class PartieSquadro { ...@@ -31,32 +31,32 @@ class PartieSquadro {
} }
} }
// 🔹 Récupère le joueur actif // Récupère le joueur actif
public function getJoueurActif(): JoueurSquadro { public function getJoueurActif(): JoueurSquadro {
return $this->joueurs[$this->joueurActif]; return $this->joueurs[$this->joueurActif];
} }
// 🔹 Retourne le nom du joueur actif // Retourne le nom du joueur actif
public function getNomJoueurActif(): string { public function getNomJoueurActif(): string {
return $this->joueurs[$this->joueurActif]->getNom(); return $this->joueurs[$this->joueurActif]->getNom();
} }
// 🔹 Retourne l'ID de la partie // Retourne l'ID de la partie
public function getPartieID(): int { public function getPartieID(): int {
return $this->partieId; return $this->partieId;
} }
// 🔹 Définit l'ID de la partie // Définit l'ID de la partie
public function setPartieID(int $id): void { public function setPartieID(int $id): void {
$this->partieId = $id; $this->partieId = $id;
} }
// 🔹 Retourne la liste des joueurs // Retourne la liste des joueurs
public function getJoueurs(): array { public function getJoueurs(): array {
return $this->joueurs; return $this->joueurs;
} }
// 🔹 Convertit l'état de la partie en JSON // Convertit l'état de la partie en JSON
public function toJson(): string { public function toJson(): string {
return json_encode([ return json_encode([
'partieId' => $this->partieId, 'partieId' => $this->partieId,
...@@ -70,7 +70,7 @@ class PartieSquadro { ...@@ -70,7 +70,7 @@ class PartieSquadro {
]); ]);
} }
// 🔹 Restaure une partie à partir d'un JSON // Restaure une partie à partir d'un JSON
public static function fromJson(string $json, array $joueurs): PartieSquadro { public static function fromJson(string $json, array $joueurs): PartieSquadro {
$data = json_decode($json, true); $data = json_decode($json, true);
$partie = new PartieSquadro($joueurs[self::PLAYER_ONE]); $partie = new PartieSquadro($joueurs[self::PLAYER_ONE]);
...@@ -87,7 +87,7 @@ class PartieSquadro { ...@@ -87,7 +87,7 @@ class PartieSquadro {
return $partie; return $partie;
} }
// 🔹 Convertit l'objet en une représentation textuelle // Convertit l'objet en une représentation textuelle
public function __toString(): string { public function __toString(): string {
return "Partie ID: " . $this->partieId . " | Joueur Actif: " . $this->getNomJoueurActif(); return "Partie ID: " . $this->partieId . " | Joueur Actif: " . $this->getNomJoueurActif();
} }
......
<?php
require_once 'JoueurSquadro.php';
require_once 'PlateauSquadro.php';
require_once 'PartieSquadro.php';
/**
* Classe de test pour PartieSquadro.
*/
class TestPartieSquadro
{
/**
* Teste la création d'une partie avec un joueur.
*/
public function testCreationPartie()
{
$joueur1 = new JoueurSquadro("Alice", 1);
$partie = new PartieSquadro($joueur1);
echo "Test création partie: \n";
echo "Joueur actif: " . $partie->getNomJoueurActif() . "\n";
echo "Game status: " . $partie->gameStatus . "\n\n";
}
/**
* Teste l'ajout d'un deuxième joueur.
*/
public function testAjoutJoueur()
{
$joueur1 = new JoueurSquadro("Alice", 1);
$joueur2 = new JoueurSquadro("Bob", 2);
$partie = new PartieSquadro($joueur1);
$partie->addJoueur($joueur2);
echo "Test ajout joueur: \n";
echo "Joueurs dans la partie: \n";
foreach ($partie->getJoueurs() as $joueur) {
echo "- " . $joueur->getNomJoueur() . "\n";
}
echo "Game status après ajout: " . $partie->gameStatus . "\n\n";
}
/**
* Teste la récupération du joueur actif.
*/
public function testGetJoueurActif()
{
$joueur1 = new JoueurSquadro("Alice", 1);
$partie = new PartieSquadro($joueur1);
echo "Test récupération joueur actif: \n";
echo "Joueur actif: " . $partie->getJoueurActif()->getNomJoueur() . "\n\n";
}
/**
* Teste la conversion en JSON.
*/
public function testToJson()
{
$joueur1 = new JoueurSquadro("Alice", 1);
$joueur2 = new JoueurSquadro("Bob", 2);
$partie = new PartieSquadro($joueur1);
$partie->addJoueur($joueur2);
$json = $partie->toJson();
echo "Test toJson: \n";
echo "JSON généré: $json\n\n";
}
/**
* Teste la reconstruction d'une partie à partir d'un JSON.
*/
public function testFromJson()
{
$json = '{
"partieId": 10,
"joueurs": {
"0": "Alice",
"1": "Bob"
},
"joueurActif": 0,
"gameStatus": "waitingForPlayer",
"plateau": "{}"
}';
$joueur1 = new JoueurSquadro("Alice", 1);
$joueur2 = new JoueurSquadro("Bob", 2);
$joueurs = [
PartieSquadro::PLAYER_ONE => $joueur1,
PartieSquadro::PLAYER_TWO => $joueur2
];
$partie = PartieSquadro::fromJson($json, $joueurs);
echo "Test fromJson: \n";
echo "ID de la partie: " . $partie->getPartieID() . "\n";
echo "Joueur actif: " . $partie->getNomJoueurActif() . "\n";
echo "Game status: " . $partie->gameStatus . "\n\n";
}
}
// Exécution des tests
$test = new TestPartieSquadro();
$test->testCreationPartie();
$test->testAjoutJoueur();
$test->testGetJoueurActif();
$test->testToJson();
$test->testFromJson();
?>