Newer
Older
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
<?php
class PieceSquadro {
// Constantes de classe
const BLANC = 0;
const NOIR = 1;
const VIDE = -1;
const NEUTRE = -2;
const NORD = 0;
const EST = 1;
const SUD = 2;
const OUEST = 3;
// Attributs privés
private int $couleur;
private int $direction;
// Constructeur privé
private function __construct(int $couleur, int $direction) {
$this->couleur = $couleur;
$this->direction = $direction;
}
// Méthodes d'accès
public function getCouleur(): int {
return $this->couleur;
}
public function getDirection(): int {
return $this->direction;
}
// Méthode pour inverser la direction
public function inverseDirection(): void {
if ($this->direction === self::NORD) {
$this->direction = self::SUD;
} elseif ($this->direction === self::SUD) {
$this->direction = self::NORD;
} elseif ($this->direction === self::EST) {
$this->direction = self::OUEST;
} elseif ($this->direction === self::OUEST) {
$this->direction = self::EST;
}
}
// Méthode de conversion en chaîne de caractères
public function __toString(): string {
return "Couleur: {$this->couleur}, Direction: {$this->direction}";
}
// Méthodes statiques pour initialiser les pièces
public static function initVide(): PieceSquadro {
return new self(self::VIDE, self::NEUTRE);
}
public static function initNeutre(): PieceSquadro {
return new self(self::NEUTRE, self::NEUTRE);
}
public static function initNoirNord(): PieceSquadro {
return new self(self::NOIR, self::NORD);
}
public static function initNoirSud(): PieceSquadro {
return new self(self::NOIR, self::SUD);
}
public static function initBlancEst(): PieceSquadro {
return new self(self::BLANC, self::EST);
}
public static function initBlancOuest(): PieceSquadro {
return new self(self::BLANC, self::OUEST);
}
// Méthode pour convertir un objet en JSON
public function toJson(): string {
return json_encode([
'couleur' => $this->couleur,
'direction' => $this->direction
]);
}
// Méthode statique pour créer une instance depuis une chaîne JSON
public static function fromJson(string $json): PieceSquadro {
$data = json_decode($json, true);
return new self($data['couleur'], $data['direction']);
}
}
?>