-
Notifications
You must be signed in to change notification settings - Fork 5
/
chessutils.php
440 lines (365 loc) · 10.9 KB
/
chessutils.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
<?php
// $Id: chessutils.php,v 1.12 2010/08/14 16:57:54 sandking Exp $
/*
This file is part of WebChess. http://webchess.sourceforge.net
Copyright 2010 Jonathan Evraire, Rodrigo Flores
WebChess is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
WebChess is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with WebChess. If not, see <http://www.gnu.org/licenses/>.
*/
$_CHESSUTILS = true;
#require('chess.inc');
/* these are utility functions used by other functions */
function getPieceName($piece)
{
switch($piece & COLOR_MASK)
{
case PAWN:
$name = "pawn";
break;
case KNIGHT:
$name = "knight";
break;
case BISHOP:
$name = "bishop";
break;
case ROOK:
$name = "rook";
break;
case QUEEN:
$name = "queen";
break;
case KING:
$name = "king";
break;
}
return $name;
}
function getPieceCode($color, $piece)
{
switch($piece)
{
case "pawn":
$code = PAWN;
break;
case "knight":
$code = KNIGHT;
break;
case "bishop":
$code = BISHOP;
break;
case "rook":
$code = ROOK;
break;
case "queen":
$code = QUEEN;
break;
case "king":
$code = KING;
break;
}
if ($color == "black")
$code = BLACK | $code;
return $code;
}
function getPGNCode($piecename)
{
switch($piecename)
{
case 'pawn':
$pgnCode = "";
break;
case 'knight':
$pgnCode = "N";
break;
case 'bishop':
$pgnCode = "B";
break;
case 'rook':
$pgnCode = "R";
break;
case 'queen':
$pgnCode = "Q";
break;
case 'king':
$pgnCode = "K";
break;
case '':
$pgnCode="";
break;
}
return $pgnCode;
}
function isBoardDisabled()
{
global $board, $isPromoting, $isUndoRequested, $isDrawRequested, $isGameOver, $playersColor;
/* if current player is promoting, a message needs to be replied to (Undo or Draw) or the game is over, then board is Disabled */
$tmpIsBoardDisabled = (($isPromoting || $isUndoRequested || $isDrawRequested || $isGameOver) == true);
/* if opponent is in the process of promoting, then board is diabled */
if (!$tmpIsBoardDisabled)
{
if ($playersColor == "white")
$promotionRow = 7;
else
$promotionRow = 0;
for ($i = 0; $i < 8; $i++)
if (($board[$promotionRow][$i] & COLOR_MASK) == PAWN)
$tmpIsBoardDisabled = true;
}
return $tmpIsBoardDisabled;
}
function moveToPGNString($curColor, $piece, $fromRow, $fromCol, $toRow, $toCol, $pieceCaptured, $promotedTo, $isChecking)
{
$pgnString = "";
/* check for castling */
if (($piece == "king") && (abs($toCol - $fromCol) == 2))
{
/* if king-side castling */
if (($toCol - $fromCol) == 2)
$pgnString .= ("O-O");
else
$pgnString .= ("O-O-O");
}
else
{
/* PGN code for moving piece */
$pgnString .= getPGNCode($piece);
/* source square */
$pgnString .= chr($fromCol + 97).($fromRow + 1);
/* check for captured pieces */
if ($pieceCaptured != "" || ($piece == 'pawn' && $fromCol != $toCol))
$pgnString .= "x";
else
$pgnString .= "-";
/* destination square */
$pgnString .= chr($toCol + 97).($toRow + 1);
/* check for pawn promotion */
if ($promotedTo != "")
$pgnString .= "=".getPGNCode($promotedTo);
}
/* check for CHECK */
if ($isChecking)
$pgnString .= "+";
/* if checkmate, $pgnString .= "#"; */
return $pgnString;
}
function moveToVerbousString($curColor, $piece, $fromRow, $fromCol, $toRow, $toCol, $pieceCaptured, $promotedTo, $isChecked)
{
$verbousString = "";
/* ex: white queen from a4 to c6 */
$verbousString .= $curColor." ".$piece." from ".chr($fromCol + 97).($fromRow + 1)." to ".chr($toCol + 97).($toRow + 1);
/* check for castling */
if (($piece == "king") && (abs($toCol - $fromCol) == 2))
$verbousString .= " (castled)";
/* check for en passant */
if (($piece == "pawn") && ($toCol != $fromCol) && ($pieceCaptured == ""))
$verbousString .= " eating pawn en-passant";
if ($pieceCaptured != "")
$verbousString .= " eating ".$pieceCaptured;
if ($promotedTo != "")
$verbousString .= "<br>Pawn promoted to ".$promotedTo;
return $verbousString;
}
function webchessMail($msgType, $msgTo, $move, $opponent, $gameID)
{
global $CFG_MAILADDRESS, $CFG_MAINPAGE;
/* default message and subject */
$mailmsg = "";
$mailsubject = APP_NAME;
/* load specific message and subject */
switch($msgType)
{
case 'test':
require 'mailmsgtest.php';
break;
case 'invitation':
require 'mailmsginvite.php';
break;
case 'withdrawal':
require 'mailmsgwithdraw.php';
break;
case 'resignation':
require 'mailmsgresign.php';
break;
case 'move':
require 'mailmsgmove.php';
break;
// ToDo: mailmsgundorequest.php ??
}
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=utf-8\r\n";
$headers .= 'From: ' . APP_NAME . ' <'.$CFG_MAILADDRESS.">\r\n";
/* Some MTAs may require for you to uncomment the following line. Do so if mail notification doesn't work */
//$headers .= "To: ".$msgTo."\r\n";
$headers .= 'Reply-To: ' . APP_NAME . ' <'.$CFG_MAILADDRESS.">\r\n";
mail($msgTo, $mailsubject, $mailmsg, $headers);
}
/* returns true if current version of PHP is greater than vercheck */
/* donated to PHP page (http://www.php.net/manual/en/function.version-compare.php) */
/* by [email protected] and is PHP < 4.1.0 safe */
function minimum_version( $vercheck ) {
$minver = explode(".", $vercheck);
$curver = explode(".", phpversion());
if (($curver[0] < $minver[0])
|| (($curver[0] == $minver[0])
&& ($curver[1] < $minver[1]))
|| (($curver[0] == $minver[0])
&& ($curver[1] == $minver[1])
&& ($curver[2][0] < $minver[2][0])))
return false;
else
return true;
}
/* allow WebChess to be run on PHP systems < 4.1.0, using old http vars */
/* heavily based on php4-1-0_varfix.php by Tom Harrison ([email protected]) */
/* only doing the opposite: creating _SESSION, _GET and _POST based on */
/* their HTTP_*_VARS equivalent */
function createNewHttpVars($type)
{
global $HTTP_POST_VARS, $HTTP_GET_VARS, $HTTP_SESSION_VARS;
$temp = array();
switch(strtoupper($type))
{
case 'POST': $temp2 = &$HTTP_POST_VARS; break;
case 'GET': $temp2 = &$HTTP_GET_VARS; break;
case 'SESSION': $temp2 = &$HTTP_SESSION_VARS; break;
default: return 0;
}
while (list($varname, $varvalue) = each($temp2)) {
$temp[$varname] = $varvalue;
}
return ($temp);
}
function fixOldPHPVersions()
{
global $_fixOldPHPVersions;
if (isset($_fixOldPHPVersions))
return;
if (!minimum_version("4.1.0"))
{
global $_POST, $_GET, $_SESSION;
$_POST = createNewHttpVars("POST");
$_GET = createNewHttpVars("GET");
//$_SESSION = createNewHttpVars("SESSION");
if (!isset($HTTP_SESSION_VARS["_SESSION"]))
session_register("_SESSION");
}
$_fixOldPHPVersions = true;
}
// this function was taken from the PHP documentation
// http://www.php.net/manual/en/function.mt-srand.php
// seed with microseconds
function make_seed() {
list($usec, $sec) = explode(' ', microtime());
return (float) $sec + ((float) $usec * 100000);
}
// this function was provided to the PHP documentation
// by [email protected] and slightly modified to use
// the above make_seed()
// http://www.php.net/manual/en/function.srand.php
// ensures srand() is only called once
function init_srand($seed = '')
{
static $wascalled = FALSE;
if (!$wascalled){
$seed = $seed === '' ? make_seed() : $seed;
srand($seed);
$wascalled = TRUE;
}
}
function coordsToSquare($xRow,$xCol)
{
$xRow+=1;
$xCol+=1;
if($xCol==1) $newCol="a";
if($xCol==2) $newCol="b";
if($xCol==3) $newCol="c";
if($xCol==4) $newCol="d";
if($xCol==5) $newCol="e";
if($xCol==6) $newCol="f";
if($xCol==7) $newCol="g";
if($xCol==8) $newCol="h";
return $newCol.$xRow;
}
function moveToPGNString2($curColor, $piece, $fromRow, $fromCol, $toRow, $toCol, $pieceCaptured, $promotedTo, $isChecking)
{
$pgnString = "";
/* check for castling */
if (($piece == "king") && (abs($toCol - $fromCol) == 2))
{
/* if king-side castling */
if (($toCol - $fromCol) == 2)
$pgnString .= ("O-O");
else
$pgnString .= ("O-O-O");
}
else
{
/* PNG code for moving piece */
$pgnString .= getPGNCode($piece);
/* source square */
$pgnString .= chr($fromCol + 97).($fromRow + 1);
/* check for captured pieces */
if ($piece='pawn' && $fromCol!=$toCol)
$pgnString .="x";
else
{
if ($pieceCaptured != "")
$pgnString .= "x";
else
$pgnString .= "";
}
/* destination square */
$pgnString .= chr($toCol + 97).($toRow + 1);
/* check for pawn promotion */
if ($promotedTo != "")
$pgnString .= "=".getPGNCode($promotedTo);
}
/* check for CHECK */
if ($isChecking)
$pgnString .= "+";
/* if checkmate, $pgnString .= "#"; */
return $pgnString;
}
function ReturnGameInfo($GameID)
{
global $CFG_TABLE;
global $pWhite,$pWhiteF,$pWhiteL,$pBlack,$pBlackF,$pBlackL,$gStart,$MyColor,$isDraw;
$tmpGameQ = mysql_query("SELECT whitePlayer,blackPlayer,dateCreated,gameMessage FROM " . $CFG_TABLE[games] . " WHERE gameID = " . $GameID) or die(mysql_error());
$tmpGame = mysql_fetch_array($tmpGameQ, MYSQL_ASSOC);
$gStart = $tmpGame['dateCreated'];
$isDraw="";
if($tmpGame['gameMessage']=="draw"){$isDraw=true;}else{$isDraw="";}
$tmpBlackQ = mysql_query("SELECT nick,firstName,lastName FROM " . $CFG_TABLE[players] . " WHERE playerID = ".$tmpGame['blackPlayer']);
$xBlack = mysql_fetch_array($tmpBlackQ, MYSQL_ASSOC);
$pBlack = $xBlack['nick'];
$pBlackF = $xBlack['firstName'];
$pBlackL = $xBlack['lastName'];
$tmpWhiteQ = mysql_query("SELECT nick,firstName,lastName FROM " . $CFG_TABLE[players] . " WHERE playerID = ".$tmpGame['whitePlayer']);
$xWhite = mysql_fetch_array($tmpWhiteQ, MYSQL_ASSOC);
$pWhite = $xWhite['nick'];
$pWhiteF = $xWhite['firstName'];
$pWhiteL = $xWhite['lastName'];
if ($tmpGame['whitePlayer'] == $_SESSION['playerID'])
{
$tmpOpponent = $tmpBlackQ;
$MyColor="white";
}
elseif ($tmpGame['blackPlayer'] == $_SESSION['playerID'])
{
$tmpOpponent = $tmpWhiteQ;
$MyColor="black";
}
else
{
$MyColor="none";
}
}
?>