include "funcs.php";
include "chessfuncs.php";
aim_header("chess");
if (!isset($_SESSION['skill']))
$_SESSION['skill'] = 1;
if (isset($_GET['skill']) && $_GET['skill'] >= 0 && $_GET['skill'] <= 5)
$_SESSION['skill'] = sprintf("%d", $_GET['skill']);
$checkmate = 0;
if ($cmd == 'new') {
$_SESSION['chess-new']++;
if (file_exists("chess/$sid.epd"))
@unlink("chess/$sid.epd");
if (file_exists("chess/$sid.pgn"))
@unlink("chess/$sid.pgn");
unset($_SESSION['highlight']);
$_SESSION['lastmsg'] = 'New game.';
} else if ($cmd == 'hint') {
$_SESSION['chess-hints']++;
$hint = aim_chess_gnuchess_hint();
$_SESSION['lasthint'] = aim_chess_san2english($hint) . " (SAN $hint)";
} else if ($from != '' && $from == $to) {
# deselect piece
$from = $to = '';
} else if ($from != '' && $to != '') {
# sanitize from/to
$coord = aim_chess_parse_coords($from);
$from = aim_chess_make_coords($coord[0], $coord[1]);
$coord = aim_chess_parse_coords($to);
$to = aim_chess_make_coords($coord[0], $coord[1]);
# execute gnuchess
# echo "Command: $from$to\n";
aim_chess_gnuchess_move("$from$to");
# so that the board printer is properly set up for a new move
$from = '';
unset($_SESSION['lasthint']);
}
if (file_exists("chess/$sid.epd")) {
if (($fd = fopen("chess/$sid.epd", "r"))) {
$buf = fgets($fd);
$epd = explode(" ", $buf);
$board = aim_chess_parse_epd($epd[0]);
fclose($fd);
}
} else
$board = aim_chess_parse_epd("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR");
if (file_exists("chess/$sid.pgn")) {
list($movelist, $checkstatus) = aim_chess_parse_pgn("chess/$sid.pgn");
if ($checkstatus['w'] == 1)
$checkinfo = "Check. :-X";
else if ($checkstatus['w'] == 2) {
$checkinfo = "Checkmate. You lose. :(";
$checkmate = 1;
$_SESSION['chess-losses']++;
}
if ($checkstatus['b'] == 1)
$checkinfo .= "Opponent in check. :)";
else if ($checkstatus['b'] == 2) {
$checkinfo .= "Checkmate. You win! :-D";
$checkmate = 1;
$_SESSION['chess-wins']++;
}
}
# print SAN for CPU move
if (ereg("^My move is: .*", $_SESSION['lastmsg'])) {
if (isset($movelist)) {
while (list($movenum, $sides) = each($movelist)) {
$lastmove = $sides['b'];
}
}
if ($lastmove != '')
$extra = " ($lastmove)";
}
echo $_SESSION['lastmsg'] . "$extra
\n";
echo $checkinfo . "
\n";
# check that first piece isn't a blank or black square
aim_chess_board_print($board, $from, $_SESSION['highlight']);
echo "[ ";
echo aim_href("new", "$baseurl/chess.php?cmd=new");
echo " ]";
echo "[ ";
echo aim_href("hint", "$baseurl/chess.php?cmd=hint");
echo " ]";
echo "[ level ";
if ($_SESSION['skill'] == 0)
echo $hl_start;
echo aim_href("1", "$baseurl/chess.php?skill=0");
if ($_SESSION['skill'] == 0)
echo $hl_end;
echo " ";
if ($_SESSION['skill'] == 1)
echo $hl_start;
echo aim_href("2", "$baseurl/chess.php?skill=1");
if ($_SESSION['skill'] == 1)
echo $hl_end;
echo " ";
if ($_SESSION['skill'] == 2)
echo $hl_start;
echo aim_href("3", "$baseurl/chess.php?skill=2");
if ($_SESSION['skill'] == 2)
echo $hl_end;
echo " ";
if ($_SESSION['skill'] == 3)
echo $hl_start;
echo aim_href("4", "$baseurl/chess.php?skill=3");
if ($_SESSION['skill'] == 3)
echo $hl_end;
echo " ";
if ($_SESSION['skill'] == 4)
echo $hl_start;
echo aim_href("5", "$baseurl/chess.php?skill=4");
if ($_SESSION['skill'] == 4)
echo $hl_end;
echo " ]
";
if (isset($_SESSION['lasthint']))
echo "Hint: " . $_SESSION['lasthint'] . "
\n";
echo "
Note: Your game in-progress will be saved for up to 1 month ";
echo "after you leave, so you can interrupt your game and come back to ";
echo "it later if you wish.";
echo "
Note 2: Pawn upgrades aren't implemented yet.\n";
echo "
\n";
aim_footer();
?>