예제 #1
0
function getMoves($method = false)
{
    // movesArray is a 2D array that contains, for every move:
    // $movesArray[$i] which contains an array that consists of:
    // -- ALWAYS --
    //   'piece'   = the PGN code of the piece that was moved, ie. k for black king, or R for white rook
    //   'fromSq'  = the FROM square counted a1 to h8 as 0 to 63
    //   'fromRow' = the FROM rank counted 1 to 8 as 0 to 7
    //   'fromCol' = the FROM file counted a to h as 0 to 7
    //   'toSqr'   = the TO square
    //   'toRow'   = the TO rank
    //   'toCol'   = the TO file
    // -- SOMETIMES --
    //   'captSqr' = the same as the TO square above unless en passant, then it is the captured pawn square
    //   'captRow' = the same as the TO rank above unless en passant, then it is the captured pawn rank
    //   'captCol' = the same as the TO file above unless en passant, then it is the captured pawn file
    //   'captPiece' = the PGN code of the piece that was captured
    //   'extra'   = contains either 'ep' for en passant, 'O-O-O', or 'O-O' for castle moves
    //   'promo'   = the PGN code of the piece that the pawn promoted to
    //   'check'   = contains check information as either 'check' or 'mate'
    /* based on player's preferences, display the history */
    $moves = array();
    // Make sure that $moves is defined
    $html = '';
    if (!isset($_SESSION['pref_history'])) {
        $_SESSION['pref_history'] = 'pgn';
    }
    $method = false !== $method ? $method : $_SESSION['pref_history'];
    switch ($method) {
        case 'verbous':
            $moves = getMovesVerbous();
            break;
        case 'coord':
            $moves = getMovesCoordinate();
            break;
        case 'alg':
            $moves = getMovesAlg();
            break;
        case 'desc':
            // way too hard right now, but go ahead
            $moves = getMovesDescriptive();
            break;
        case 'int':
            $moves = getMovesInternational();
            break;
        case 'pgn':
        case 'longalg':
        default:
            $moves = getMovesLongAlg();
            break;
    }
    $comma = '';
    $html .= "var moves = [";
    for ($i = 0; $i < count($moves); $i++) {
        $html .= $comma;
        if (($i - 1) % 4 == 0) {
            $html .= "\n      ";
        }
        $html .= "['" . $moves[$i][0] . "','";
        if (isset($moves[$i][1])) {
            $html .= $moves[$i][1];
        }
        $html .= "']";
        $comma = ",";
    }
    $html .= "\n    ];\n    ";
    return $html;
}
예제 #2
0
function saveGame()
{
    global $mysql, $chess;
    global $movesArray, $FENarray;
    global $oppColorArray, $colorArray;
    call('**** saveGame( ) ****');
    // convert the previous move to an FEN string
    $fullFEN = movetoFEN();
    // (chessutils.inc.php)
    // save the full FEN into the history table
    $query = "\n\t\tINSERT INTO " . T_HISTORY . "\n\t\t\t(h_time, h_game_id, h_fen)\n\t\tVALUES\n\t\t\t(NOW( ), '{$_SESSION['game_id']}', '{$fullFEN}')\n\t";
    $mysql->query($query, __LINE__, __FILE__);
    // get the entire FEN list back out of the table for the email
    $query = "\n\t\tSELECT h_fen\n\t\tFROM " . T_HISTORY . "\n\t\tWHERE h_game_id = '{$_SESSION['game_id']}'\n\t\tORDER BY h_time\n\t";
    $FENarray = $mysql->fetch_value_array($query, __LINE__, __FILE__);
    // and convert the current FEN array to an array of standard moves for the email
    FENtomoves();
    // (chessutils.inc.php)
    // check for stalemates and checkmates
    // and update the games table if needed
    $FENbits = explode(' ', $fullFEN);
    // break up the FEN
    call($FENbits);
    call(__FILE__ . ' : ' . __LINE__);
    $chess->init_gamestate($fullFEN);
    $state = $chess->get_status($FENbits[1]);
    // get the gamestate of the current FEN
    $playerMoved = $oppColorArray[$FENbits[1]];
    $otherPlayer = $colorArray[$FENbits[1]];
    // if the game is over due to stalemate, or checkmate
    // make sure the database knows about it
    call("gameState = {$state}\ncheckmate = " . gsMate . "\nstalemate = " . gsStalemate);
    if (gsStalemate == $state) {
        $query = "\n\t\t\tUPDATE " . T_GAME . "\n\t\t\tSET g_game_message = 'Draw'\n\t\t\t\t, g_message_from = '{$_SESSION[$playerMoved]['p_color']}'\n\t\t\tWHERE g_id = '{$_SESSION['game_id']}'\n\t\t";
        $mysql->query($query, __LINE__, __FILE__);
        adjust_stats($_SESSION[$playerMoved]['p_id'], $_SESSION[$otherPlayer]['p_id'], 0.5, 0.5);
    } elseif (gsMate == $state) {
        $query = "\n\t\t\tUPDATE " . T_GAME . "\n\t\t\tSET g_game_message = 'Checkmate'\n\t\t\t\t, g_message_from = '{$_SESSION[$playerMoved]['p_color']}'\n\t\t\tWHERE g_id = '{$_SESSION['game_id']}'\n\t\t";
        $mysql->query($query, __LINE__, __FILE__);
        adjust_stats($_SESSION[$playerMoved]['p_id'], $_SESSION[$otherPlayer]['p_id'], 1, 0);
    }
    // notify opponent of move via email (if we don't already know about, because we're right there)
    if (!isset($_SESSION['shared']) || !$_SESSION['shared']) {
        call("webchessMail('move', {$_SESSION[$otherPlayer]['p_email']}, getMovesLongAlg(true), {$_SESSION[$playerMoved]['p_username']}, {$_SESSION['game_id']})");
        webchessMail('move', $_SESSION[$otherPlayer]['p_email'], getMovesLongAlg(true), $_SESSION[$playerMoved]['p_username'], $_SESSION['game_id']);
    }
    updateTimestamp();
}