Newer
Older

G. Daryl M. OKOU
a validé
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
require_once 'JoueurSquadro.php'; // Assurez-vous d'avoir la classe JoueurSquadro
require_once 'PlateauSquadro.php'; // Assurez-vous d'avoir la classe PlateauSquadro
class PartieSquadro {
// Définition des constantes pour identifier les joueurs
public const PLAYER_ONE = 0;
public const PLAYER_TWO = 1;
private ?int $partieId = 0;
private array $joueurs = [];
public int $joueurActif;
public string $gameStatus = 'initialized';
public PlateauSquadro $plateau;
// 🔹 Constructeur : Crée une nouvelle partie avec un premier joueur
public function __construct(JoueurSquadro $playerOne) {
$this->joueurs[self::PLAYER_ONE] = $playerOne;
$this->joueurActif = self::PLAYER_ONE;
$this->plateau = new PlateauSquadro();
}
// 🔹 Ajoute un deuxième joueur à la partie
public function addJoueur(JoueurSquadro $player): void {
if (!isset($this->joueurs[self::PLAYER_TWO])) {
$this->joueurs[self::PLAYER_TWO] = $player;
$this->gameStatus = 'waitingForPlayer'; // La partie peut commencer
} else {
throw new Exception("La partie a déjà deux joueurs !");
}
}
// 🔹 Récupère le joueur actif
public function getJoueurActif(): JoueurSquadro {
return $this->joueurs[$this->joueurActif];
}
// 🔹 Retourne le nom du joueur actif
public function getNomJoueurActif(): string {
return $this->joueurs[$this->joueurActif]->getNom();
}
// 🔹 Retourne l'ID de la partie
public function getPartieID(): int {
return $this->partieId;
}
// 🔹 Définit l'ID de la partie
public function setPartieID(int $id): void {
$this->partieId = $id;
}
// 🔹 Retourne la liste des joueurs
public function getJoueurs(): array {
return $this->joueurs;
}
// 🔹 Convertit l'état de la partie en JSON
public function toJson(): string {
return json_encode([
'partieId' => $this->partieId,
'joueurs' => [
self::PLAYER_ONE => $this->joueurs[self::PLAYER_ONE]->getNom(),
self::PLAYER_TWO => $this->joueurs[self::PLAYER_TWO]->getNom() ?? null
],
'joueurActif' => $this->joueurActif,
'gameStatus' => $this->gameStatus,
'plateau' => $this->plateau->toJson()
]);
}
// 🔹 Restaure une partie à partir d'un JSON
public static function fromJson(string $json, array $joueurs): PartieSquadro {
$data = json_decode($json, true);
$partie = new PartieSquadro($joueurs[self::PLAYER_ONE]);
if (isset($joueurs[self::PLAYER_TWO])) {
$partie->addJoueur($joueurs[self::PLAYER_TWO]);
}
$partie->setPartieID($data['partieId']);
$partie->joueurActif = $data['joueurActif'];
$partie->gameStatus = $data['gameStatus'];
$partie->plateau = PlateauSquadro::fromJson($data['plateau']);
return $partie;
}
// 🔹 Convertit l'objet en une représentation textuelle
public function __toString(): string {
return "Partie ID: " . $this->partieId . " | Joueur Actif: " . $this->getNomJoueurActif();
}
}