-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameBoard.php
More file actions
222 lines (221 loc) · 5.45 KB
/
GameBoard.php
File metadata and controls
222 lines (221 loc) · 5.45 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
include( "Pawn.php" );
include( "Knight.php" );
include( "Bishop.php" );
include( "Rook.php" );
include( "King.php" );
include( "Queen.php" );
class GameBoard
{
private $files;
private $ranks;
private $square;
private $selectedSquare;
private $player1;
private $player2;
private $turn;
//------------------------------------------------------------------
public function __construct( $player1,
$player2,
$ranks = 8,
$files = 8 )
{
$this->player1 = $player1;
$this->player2 = $player2;
$this->ranks = $ranks;
$this->files = $files;
$this->clearBoard( );
}
//------------------------------------------------------------------
public function __get( $field )
{
if( property_exists( __CLASS__, $field ) )
{
return $this->$field;
}
return false;
}
//------------------------------------------------------------------
public function attackPiece( )
{
return false;
}
//------------------------------------------------------------------
public function clearBoard( )
{
for( $X = 1; $X <= $this->files; $X++ )
{
for( $Y = 1; $Y <= $this->ranks; $Y++ )
{
$this->square[$X][$Y] = "";
}
}
}
//------------------------------------------------------------------
public function drawBoard( $perspective )
{
if( $perspective == $this->player1 )
{
$topRank = $this->ranks * -1;
$bottomRank = -1;
}
else
{
$topRank = 1;
$bottomRank = $this->ranks;
}
$light = "#eec";
$dark = "#775";
$tile = $light;
$HTML = "<table style='background:#555;z-index:-1;'>";
for( $Y = $topRank; $Y <= $bottomRank; $Y++ )
{
$HTML .= "
<tr style='z-index:-1'>";
for( $X = 1; $X <= $this->files; $X++ )
{
$piece = $this->square[$X][abs( $Y )];
$drawPiece = ( $piece == "" ) ? "" : $piece->iconURL;
$HTML .= "
<td style='background:{$tile};z-index:-1;'>
<div style='width:64px;
height:64px;
background:{$drawPiece};
display:block;
z-index:20;'
onclick='select( this );'
X='$X'
Y='" . abs( $Y ) . "'></div>
</td>";
$tile = ( $tile == $dark ) ? $light : $dark;
}
$HTML .= "
</tr>";
$tile = ( $tile == $dark ) ? $light : $dark;
}
$HTML .= "</table>";
echo( $HTML );
}
//------------------------------------------------------------------
public function isEmptySquare( $square )
{
$X = $square[0];
$Y = $square[1];
if( $this->square[$X][$Y] == "" )
{
return true;
}
return false;
}
//------------------------------------------------------------------
public function isLegalSquare( $square )
{
$X = $square[0];
$Y = $square[1];
if( ( $X < 1 || $X > $this->files ) ||
( $Y < 1 || $Y > $this->ranks ) )
{
return false;
}
return true;
}
//------------------------------------------------------------------
public function isObstructed( $fromSquare, $toSquare )
{
$X = $fromSquare[0];
$Y = $fromSquare[1];
$toX = $toSquare[0];
$toY = $toSquare[1];
$nextX = ($X < $toX ) ? 1 : -1;
$nextY = ($Y < $toY ) ? 1 : -1;
while( $X != $toX || $Y != $toY )
{
$X = ( $X != $toX ) ? $X += $nextX : $X;
$Y = ( $Y != $toY ) ? $Y += $nextY : $Y;
if( $X != $toX || $Y != $toY )
{
if( $this->square[$X][$Y] != "" )
{
return true;
}
}
}
return false;
}
//------------------------------------------------------------------
public function isPlayerPiece( $square )
{
$X = $square[0];
$Y = $square[1];
if( $this->square[$X][$Y] == "" )
{
return false;
}
$color = $this->square[$X][$Y]->color;
$turn = $this->turn;
if( $color == "White" && $turn == $this->player1 )
{
return true;
}
else if( $color == "Black" && $turn == $this->player2 )
{
return true;
}
return false;
}
//------------------------------------------------------------------
public function movePiece( $fromSquare, $toSquare )
{
$fromX = $fromSquare[0];
$fromY = $fromSquare[1];
$toX = $toSquare[0];
$toY = $toSquare[1];
if( $this->square[$fromX][$fromY]->name != "Knight" &&
$this->isObstructed( $fromSquare, $toSquare ) )
{
return false;
}
if( $this->square[$fromX][$fromY]->moveToSquare( $toSquare ) == true )
{
$this->square[$toX][$toY] = $this->square[$fromX][$fromY];
$this->square[$fromX][$fromY] = "";
return true;
}
return false;
}
//------------------------------------------------------------------
public function saveState( )
{
}
//------------------------------------------------------------------
public function setupChess( )
{
$this->ranks = 8;
$this->files = 8;
$this->clearBoard( );
$this->turn = $this->player1;
//Setup pawns on 2nd and 7th ranks
for( $Y = 2; $Y <= 7; $Y += 5 )
{
$color = ( $Y == 2 ) ? "White" : "Black";
for( $X = 1; $X <= 8; $X++ )
{
$this->square[$X][$Y] = new Pawn( $color, array( $X, $Y ) );
}
}
//Setup pieces on 1st and 8th ranks
for( $Y = 1; $Y <= 8; $Y += 7 )
{
$color = ( $Y == 1 ) ? "White" : "Black";
$this->square[1][$Y] = new Rook( $color, array( 1, $Y ) );
$this->square[2][$Y] = new Knight( $color, array( 2, $Y ) );
$this->square[3][$Y] = new Bishop( $color, array( 3, $Y ) );
$this->square[4][$Y] = new Queen( $color, array( 4, $Y ) );
$this->square[5][$Y] = new King( $color, array( 5, $Y ) );
$this->square[6][$Y] = new Bishop( $color, array( 6, $Y ) );
$this->square[7][$Y] = new Knight( $color, array( 7, $Y ) );
$this->square[8][$Y] = new Rook( $color, array( 8, $Y ) );
}
}
} //GameBoard
?>