function doUndo() { global $CFG_TABLE; global $board, $numMoves; /* old PHP versions don't have _POST, _GET and _SESSION as auto_globals */ if (!minimum_version("4.1.0")) { global $_POST, $_GET, $_SESSION; } /* get the last move from the history */ /* NOTE: MySQL currently has no support for subqueries */ $tmpMaxTime = mysql_query("SELECT Max(timeOfMove) FROM " . $CFG_TABLE[history] . " WHERE gameID = " . $_SESSION['gameID']); $maxTime = mysql_result($tmpMaxTime, 0); $moves = mysql_query("SELECT * FROM " . $CFG_TABLE[history] . " WHERE gameID = " . $_SESSION['gameID'] . " AND timeOfMove = '{$maxTime}'"); /* if there actually is a move... */ if ($lastMove = mysql_fetch_array($moves, MYSQL_ASSOC)) { /* if the last move was played by this player */ /* undo move */ $fromRow = $lastMove['fromRow']; $fromCol = $lastMove['fromCol']; $toRow = $lastMove['toRow']; $toCol = $lastMove['toCol']; $board[$fromRow][$fromCol] = getPieceCode($lastMove['curColor'], $lastMove['curPiece']); $board[$toRow][$toCol] = 0; /* check for en-passant */ /* if pawn moves diagonally without replacing a piece, it's en passant */ if ($lastMove['curPiece'] == "pawn" && $toCol != $fromCol && is_null($lastMove['replaced'])) { if ($lastMove['curColor'] == "black") { $board[$fromRow][$toCol] = getPieceCode("white", "pawn"); } else { $board[$fromRow][$toCol] = getPieceCode("black", "pawn"); } } /* check for castling */ if (($board[$fromRow][$fromCol] & COLOR_MASK) == KING && abs($toCol - $fromCol) == 2) { /* move rook back as well */ if ($toCol - $fromCol == 2) { $board[$fromRow][7] = $board[$fromRow][5]; $board[$fromRow][5] = 0; } else { $board[$fromRow][0] = $board[$fromRow][3]; $board[$fromRow][3] = 0; } } /* restore lost piece */ if (!is_null($lastMove['replaced'])) { if ($lastMove['curColor'] == "black") { $board[$toRow][$toCol] = getPieceCode("white", $lastMove['replaced']); } else { $board[$toRow][$toCol] = getPieceCode("black", $lastMove['replaced']); } } /* remove last move from history */ $numMoves--; mysql_query("DELETE FROM " . $CFG_TABLE[history] . " WHERE gameID = " . $_SESSION['gameID'] . " AND timeOfMove = '{$maxTime}'"); /* else */ /* output error message */ } }
else if ($PLAY){ echo "$now # $JOGADOR_NAME ($playersColor) PLY: $PLY playing at game: ". $row['gameID']."\n"; for ($i = 0; $i < 8; $i++) for ($j = 0; $j < 8; $j++) $board[$i][$j] = "."; for ($i = 0; $i < 8; $i++) for ($j = 0; $j < 8; $j++) $tboard[$i][$j] = 0; $pieces = mysql_query("SELECT * FROM pieces WHERE gameID = ".$gameID); while ($thisPiece = mysql_fetch_array($pieces, MYSQL_ASSOC)) { $board[$thisPiece["row"]][$thisPiece["col"]] = getPieceFileCode($thisPiece["piece"]); $tboard[$thisPiece["row"]][$thisPiece["col"]] = getPieceCode($thisPiece["color"], $thisPiece["piece"]); if ($thisPiece["color"] == "white") $board[$thisPiece["row"]][$thisPiece["col"]] = strtoupper($board[$thisPiece["row"]][$thisPiece["col"]]); } $nu2ch[0]='a'; $nu2ch[1]='b'; $nu2ch[2]='c'; $nu2ch[3]='d'; $nu2ch[4]='e'; $nu2ch[5]='f'; $nu2ch[6]='g'; $nu2ch[7]='h';
function loadGame() { global $CFG_TABLE; global $board, $playersColor; /* old PHP versions don't have _POST, _GET and _SESSION as auto_globals */ if (!minimum_version("4.1.0")) { global $_POST, $_GET, $_SESSION; } /* clear board data */ for ($i = 0; $i < 8; $i++) { for ($j = 0; $j < 8; $j++) { $board[$i][$j] = 0; } } /* get data from database */ $pieces = mysql_query("SELECT * FROM " . $CFG_TABLE[pieces] . " WHERE gameID = " . $_SESSION['gameID']); /* setup board */ while ($thisPiece = mysql_fetch_array($pieces, MYSQL_ASSOC)) { $board[$thisPiece["row"]][$thisPiece["col"]] = getPieceCode($thisPiece["color"], $thisPiece["piece"]); } /* get current player's color */ $tmpQuery = "SELECT whitePlayer, blackPlayer FROM " . $CFG_TABLE[games] . " WHERE gameID = " . $_SESSION['gameID']; $tmpTurns = mysql_query($tmpQuery); $tmpTurn = mysql_fetch_array($tmpTurns, MYSQL_ASSOC); if ($tmpTurn['whitePlayer'] == $_SESSION['playerID']) { $playersColor = "white"; } else { $playersColor = "black"; } }
function loadGame($game_id) { global $db,$db_prefix, $board, $playersColor, $MSG_LANG,$db_prefix,$db; /* clear board data */ for ($i = 0; $i < 8; $i++) { for ($j = 0; $j < 8; $j++) { $board[$i][$j] = 0; } } $pieces = mysql_query("SELECT * FROM {$db_prefix}pieces WHERE game_id = '$game_id'"); /* setup board */ while ($thisPiece = mysql_fetch_array($pieces, MYSQL_ASSOC)) { $board[$thisPiece["row"]][$thisPiece["col"]] = getPieceCode($thisPiece["color"], $thisPiece["piece"]); } /* get current player's color */ $tmpQuery = "SELECT white_player, black_player FROM {$db_prefix}games WHERE game_id = '$game_id'"; $tmpTurns = mysql_query($tmpQuery); $tmpTurn = mysql_fetch_array($tmpTurns, MYSQL_ASSOC); if ($tmpTurn['white_player'] == $_SESSION['player_id']) { $playersColor = "white"; } else { $playersColor = "black"; } }