-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActionQuantik.php
106 lines (87 loc) · 3.08 KB
/
ActionQuantik.php
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
<?php
include "ArrayPieceQuantik.php";
class ActionQuantik
{
protected PlateauQuantik $plateau;
public function __construct(PlateauQuantik $plateau)
{
$this->plateau = $plateau;
}
public function getPlateau(): PlateauQuantik
{
return $this->plateau;
}
public function isValidePose(int $rowNum, int $colNum, PieceQuantik $piece): bool
{
if($this->plateau->getPiece($rowNum, $colNum) != PieceQuantik::initVoid()){
return false;
}
$pieceRow = $this->plateau->getRow($rowNum);
$pieceCol = $this->plateau->getCol($colNum);
$pieceCorner = $this->plateau->getCorner(PlateauQuantik::getCornerFromCoord($rowNum, $colNum));
$resultat = $this->isPieceValide($pieceCorner, $piece) && $this->isPieceValide($pieceCol, $piece) && $this->isPieceValide($pieceRow, $piece) ;
return $resultat;
}
private static function isPieceValide(array $pieces, PieceQuantik $p): bool
{
switch ($p->getForme()){
case PieceQuantik::CUBE :
return !(in_array(PieceQuantik::initBlackCube(), $pieces) or in_array(PieceQuantik::initWhiteCube(), $pieces));
case PieceQuantik::CONE :
return !(in_array(PieceQuantik::initBlackCone(), $pieces) or in_array(PieceQuantik::initWhiteCone(), $pieces));
case PieceQuantik::CYLINDRE :
return !(in_array(PieceQuantik::initBlackCylindre(), $pieces) or in_array(PieceQuantik::initWhiteCylindre(), $pieces));
case PieceQuantik::SPHERE :
return !(in_array(PieceQuantik::initBlackSphere(), $pieces) or in_array(PieceQuantik::initWhiteSphere(), $pieces));
}
}
public function posePiece(int $rowNum, int $colNum, PieceQuantik $piece):void {
$this->plateau->setPiece($rowNum, $colNum, $piece);
}
public function __toString():String
{
$s = '<p>Vous avez ';
for($i = 0; $i < PlateauQuantik::NBROWS; $i++) {
if($this->isRowWin($i) || $this->isColWin($i) || $this->isCornerWin($i)) {
$s = $s.'Gagné ^^</p>';
return $s;
}
}
$s = '';
return $s;
}
private static function isComboWin(array $pieces): bool
{
for($i = 0; $i < PlateauQuantik::NBROWS; $i++) {
$tabP[$i] = false;
}
for($i = 0; $i < PlateauQuantik::NBROWS; $i++) {
$temp = $pieces[$i]->getForme()-1;
if($temp > -1) {
$tabP[$i] = true;
}
}
for($i = 0; $i < PlateauQuantik::NBROWS; $i++) {
if($tabP[$i] == false) {
return false;
}
}
return true;
}
public function isRowWin(int $numRow): bool
{
$row = $this->plateau->getRow($numRow);
return $this->isComboWin($row);
}
public function isColWin(int $numCol): bool
{
$col = $this->plateau->getCol($numCol);
return $this->isComboWin($col);
}
public function isCornerWin(int $dir): bool
{
$cor = $this->plateau->getCorner($dir);
return $this->isComboWin($cor);
}
}
?>