-
Notifications
You must be signed in to change notification settings - Fork 2
/
ChessBoard.php
166 lines (153 loc) · 5.3 KB
/
ChessBoard.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
/**
* Description of ChessBoard
*
* @author durand_y
*/
error_reporting (E_ALL);
ini_set ("display_errors", 1);
class ChessBoard
{
//attributs
private $instance;
private $board;
private $length = 8;
private $height = 8;
private $correspondance;
//auto-load
function __autoload($class_name)
{
include $class_name . '.php';
}
//getter setter
public function __get($name)
{
return $this->$name;
}
public function __set($name, $value)
{
$this->$name = $value;
}
//constructor
public function __construct()
{
$this->correspondance["a"] = 0;
$this->correspondance["b"] = 1;
$this->correspondance["c"] = 2;
$this->correspondance["d"] = 3;
$this->correspondance["e"] = 4;
$this->correspondance["f"] = 5;
$this->correspondance["g"] = 6;
$this->correspondance["h"] = 7;
for ($i = 0; $i < $this->height; $i++)
{
for ($j = 0; $j < $this->length; $j++)
{
switch ($i)
{
case 0:
switch ($j)
{
case 0:
$this->board[$i][$j] = new Tower($i, $j, "White", TRUE);
break;
case 1:
$this->board[$i][$j] = new Knight($i, $j, "White", TRUE);
break;
case 2:
$this->board[$i][$j] = new Bishop($i, $j, "White", TRUE);
break;
case 3:
$this->board[$i][$j] = new Queen($i, $j, "White", TRUE);
break;
case 4:
$this->board[$i][$j] = new King($i, $j, "White", TRUE);
break;
case 5:
$this->board[$i][$j] = new Bishop($i, $j, "White", TRUE);
break;
case 6:
$this->board[$i][$j] = new Knight($i, $j, "White", TRUE);
break;
case 7:
$this->board[$i][$j] = new Tower($i, $j, "White", TRUE);
break;
}
break;
case 1:
$this->board[$i][$j] = new Pawn ($i, $j, "White", true);
break;
case 6:
$this->board[$i][$j] = new Pawn($i, $j, "Black", TRUE);
break;
case 7:
switch ($j)
{
case 0:
$this->board[$i][$j] = new Tower($i, $j, "Black", TRUE);
break;
case 1:
$this->board[$i][$j] = new Knight($i, $j, "Black", TRUE);
break;
case 2:
$this->board[$i][$j] = new Bishop($i, $j, "Black", TRUE);
break;
case 3:
$this->board[$i][$j] = new Queen($i, $j, "Black", TRUE);
break;
case 4:
$this->board[$i][$j] = new King($i, $j, "Black", TRUE);
break;
case 5:
$this->board[$i][$j] = new Bishop($i, $j, "Black", TRUE);
break;
case 6:
$this->board[$i][$j] = new Knight($i, $j, "Black", TRUE);
break;
case 7:
$this->board[$i][$j] = new Tower($i, $j, "Black", TRUE);
break;
}
break;
default :
$this->board[$i][$j] = new None($i, $j, "", FALSE);
break;
}
}
}
}
public function is_valid ($from, $to)
{
if ($this->board[$this->correspondance[$from[0]]][$from[1]] != "-")
{
if ($this->board[$this->correspondance[$from[0]]][$from[1]]->check ($to) == true)
{
return true;
}
else
return false;
}
return false;
}
public function display ()
{
for ($i = 0; $i < $this->height; $i++)
{
echo "|" ;
for ($j = 0; $j < $this->length; $j++)
{
echo $this->board[$i][$j]->display()."|";
}
echo "<br/>";
}
}
public static function get_instance ()
{
if (isset (self::$instance))
{
return (self::$instance);
}
return new ChessBoard ();
}
}
?>