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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
class PlateauSquadro
{
// Constantes pour les vitesses de déplacement
const BLANC_V_ALLER = [0, 1, 3, 2, 3, 1, 0];
const BLANC_V_RETOUR = [0, 3, 1, 2, 1, 3, 0];
const NOIR_V_ALLER = [0, 3, 1, 2, 1, 3, 0];
const NOIR_V_RETOUR = [0, 1, 3, 2, 3, 1, 0];
// Variables d'instance
private $plateau; // Tableau de 7 ArrayPieceSquadro
private $lignesJouables; // Indices des lignes jouables
private $colonnesJouables; // Indices des colonnes jouables
public function __construct()
{
$this->initCasesVides();
$this->initCasesNeutres();
$this->initCasesBlanches();
$this->initCasesNoires();
$this->lignesJouables = [1, 2, 3, 4, 5];
$this->colonnesJouables = [1, 2, 3, 4, 5];
}
private function initCasesVides()
{
$this->plateau = array_fill(0, 7, new ArrayPieceSquadro());
for ($i = 0; $i < 7; $i++) {
for ($j = 0; $j < 7; $j++) {
$this->plateau[$i]->add(PieceSquadro::initVide());
}
}
}
private function initCasesNeutres()
{
foreach ([0, 6] as $i) {
foreach ([0, 6] as $j) {
$this->plateau[$i]->remove($j);
$this->plateau[$i]->add(PieceSquadro::initNeutre(), $j);
}
}
}
private function initCasesBlanches()
{
foreach ($this->lignesJouables as $i) {
$this->plateau[$i]->remove(0);
$this->plateau[$i]->add(PieceSquadro::initBlancOuest(), 0);
}
}
private function initCasesNoires()
{
foreach ($this->colonnesJouables as $j) {
$this->plateau[6]->remove($j);
$this->plateau[6]->add(PieceSquadro::initNoirSud(), $j);
}
}
public function getPlateau()
{
return $this->plateau;
}
public function getPiece($x, $y)
{
return $this->plateau[$x]->get($y);
}
public function setPiece(PieceSquadro $piece, $x, $y)
{
$this->plateau[$x]->remove($y);
$this->plateau[$x]->add($piece, $y);
}
public function retireLigneJouable($index)
{
$this->lignesJouables = array_values(array_diff($this->lignesJouables, [$index]));
}
public function retireColonneJouable($index)
{
$this->colonnesJouables = array_values(array_diff($this->colonnesJouables, [$index]));
}
public function getCoordDestination($x, $y)
{
$piece = $this->getPiece($x, $y);
$vitesse = $piece->getCouleur() === PieceSquadro::BLANC
? ($piece->getDirection() === PieceSquadro::EST ? self::BLANC_V_ALLER[$x] : self::BLANC_V_RETOUR[$x])
: ($piece->getDirection() === PieceSquadro::NORD ? self::NOIR_V_ALLER[$y] : self::NOIR_V_RETOUR[$y]);
if ($piece->getCouleur() === PieceSquadro::BLANC) {
$newY = $piece->getDirection() === PieceSquadro::EST ? $y + $vitesse : $y - $vitesse;
return [$x, min(max($newY, 0), 6)];
} elseif ($piece->getCouleur() === PieceSquadro::NOIR) {
$newX = $piece->getDirection() === PieceSquadro::NORD ? $x - $vitesse : $x + $vitesse;
return [min(max($newX, 0), 6), $y];
}
return [$x, $y];
}
public function getDestination($x, $y)
{
[$newX, $newY] = $this->getCoordDestination($x, $y);
return $this->getPiece($newX, $newY);
}
}