Esempio n. 1
0
function savePromotion()
{
    global $mysql;
    global $movesArray, $FENarray, $curTurn;
    $num_moves = count($FENarray) - 1;
    // subtract one for initpos
    // when we run the promotion script, the color to be promoted
    // is the oppposite of the color who's turn it is
    $piece = $_POST['promotion'];
    if ('white' == $curTurn) {
        $piece = strtolower($piece);
    }
    // save the promoted piece in the movesArray
    $movesArray[$num_moves]['promo'] = $piece;
    // seperate the FEN board from the rest of the FEN
    $FEN = $FENarray[$num_moves];
    $FENbits = trim(substr($FEN, strpos($FEN, ' ')));
    $xFEN = expandFEN(substr($FEN, 0, strpos($FEN, ' ')));
    // insert the promoted piece
    sqr2idx($movesArray[$num_moves]['toSq'], $idx);
    FENplace($xFEN, $idx, $movesArray[$num_moves]['promo']);
    // and repack the FEN
    $FENhead = packFEN($xFEN);
    $FEN = $FENhead . ' ' . $FENbits;
    // and save the new and improved FEN to the history
    $query = "\n\t\tSELECT MAX(h_time)\n\t\tFROM " . T_HISTORY . "\n\t\tWHERE h_game_id = '{$_SESSION['game_id']}'\n\t";
    $result = $mysql->fetch_value($query, __LINE__, __FILE__);
    $query = "\n\t\tUPDATE " . T_HISTORY . "\n\t\tSET h_fen = '{$FEN}'\n\t\tWHERE h_game_id = '{$_SESSION['game_id']}'\n\t\t\tAND h_time = '{$result}'\n\t";
    $mysql->query($query, __LINE__, __FILE__);
    updateTimestamp();
}
Esempio n. 2
0
function movetoFEN()
{
    global $FENarray, $movesArray, $board, $COLS, $initpos, $pieceColor;
    $num_moves = count($FENarray) - 1;
    // get the post info out
    foreach ($_POST as $key => $var) {
        ${$key} = $var;
    }
    // reverse row and col so i don't confuse myself
    $colFrom = $fromCol;
    $colTo = $toCol;
    $rowFrom = $fromRow;
    $rowTo = $toRow;
    // and convert it to something we can use
    colrow2idx($colFrom, $rowFrom, $idxFrom);
    colrow2idx($colTo, $rowTo, $idxTo);
    // get the current FEN data
    $FENitems = explode(' ', $FENarray[$num_moves]);
    $thatFEN = expandFEN($FENitems[0]);
    $CM = $FENitems[1];
    $CI = $FENitems[2];
    $EP = $FENitems[3];
    $PN = $FENitems[4];
    $MN = $FENitems[5];
    $newEP = '-';
    // get original placement of rooks
    $origARookPos = strpos($initpos, 'R');
    $origHRookPos = strrpos($initpos, 'R');
    $origKingPos = strpos($initpos, 'K');
    // separate the castle indicator
    $WK = false !== strpos($CI, 'K') ? 'K' : '';
    $WQ = false !== strpos($CI, 'Q') ? 'Q' : '';
    $BK = false !== strpos($CI, 'k') ? 'k' : '';
    $BQ = false !== strpos($CI, 'q') ? 'q' : '';
    // put board into expanded FEN string
    $xFEN = "";
    for ($i = 7; $i >= 0; $i--) {
        for ($j = 0; $j < 8; $j++) {
            $xFEN .= $board[$i][$j];
        }
    }
    // get the piece that is moving
    $piece = FENplace($xFEN, $idxFrom);
    // check for castling move
    if ('false' != $_POST['castleMove']) {
        if ('white' == $pieceColor[$piece]) {
            // clear the castle indicators
            $WK = '';
            $WQ = '';
            // make the move
            if ('a' == $_POST['castleMove']) {
                FENplace($xFEN, $origKingPos + 56, '0');
                // delete the king
                FENplace($xFEN, $origARookPos + 56, '0');
                // delete the rook
                FENplace($xFEN, 2 + 56, 'K');
                // place the king
                FENplace($xFEN, 3 + 56, 'R');
                // place the rook
            } elseif ('h' == $_POST['castleMove']) {
                FENplace($xFEN, $origKingPos + 56, '0');
                // delete the king
                FENplace($xFEN, $origHRookPos + 56, '0');
                // delete the rook
                FENplace($xFEN, 6 + 56, 'K');
                // place the king
                FENplace($xFEN, 5 + 56, 'R');
                // place the rook
            } else {
                die("castleMove is incorrect");
            }
        } elseif ('black' == $pieceColor[$piece]) {
            // clear the castle indicators
            $BK = '';
            $BQ = '';
            // make the move
            if ('a' == $_POST['castleMove']) {
                FENplace($xFEN, $origKingPos, '0');
                // delete the king
                FENplace($xFEN, $origARookPos, '0');
                // delete the rook
                FENplace($xFEN, 2, 'k');
                // place the king
                FENplace($xFEN, 3, 'r');
                // place the rook
            } elseif ('h' == $_POST['castleMove']) {
                FENplace($xFEN, $origKingPos, '0');
                // delete the king
                FENplace($xFEN, $origHRookPos, '0');
                // delete the rook
                FENplace($xFEN, 6, 'k');
                // place the king
                FENplace($xFEN, 5, 'r');
                // place the rook
            } else {
                die("castleMove is incorrect");
            }
        } else {
            echo "<pre>";
            for ($i = 0; $i < $idxFrom; $i++) {
                echo " ";
            }
            echo "|\n";
            echo $xFEN . "</pre>";
        }
    } else {
        // make the move
        $piece = FENplace($xFEN, $idxFrom, '0');
        $capt = FENplace($xFEN, $idxTo, $piece);
        $PN++;
        // if we have a pawn advance, or a capture
        if ('P' == strtoupper($piece) || '0' != $capt) {
            $PN = 0;
        }
        // reset the ply count
        // if we have a pawn double advance
        if ('P' == strtoupper($piece) && 2 == abs($rowFrom - $rowTo)) {
            colrow2til($colTo, ($rowFrom + $rowTo) * 0.5, $newEP);
        }
        // set the en passant indicator
        // if we moved a castling piece
        if ('K' == $piece) {
            $WK = '';
            $WQ = '';
        } elseif ('k' == $piece) {
            $BK = '';
            $BQ = '';
        } elseif ('R' == $piece) {
            if ($colFrom == $origARookPos) {
                // a-side moved
                $WQ = '';
            } elseif ($colFrom == $origHRookPos) {
                // h-side moved
                $WK = '';
            }
        } elseif ('r' == $piece) {
            if ($colFrom == $origARookPos) {
                // a-side moved
                $BQ = '';
            } elseif ($colFrom == $origHRookPos) {
                // h-side moved
                $BK = '';
            }
        }
    }
    // check for en passant capture
    colrow2til($colTo, $rowTo, $tilTo);
    if ($tilTo == $EP && 'P' == strtoupper($piece)) {
        // get the idx of the captured pawn
        colrow2idx($colTo, $rowFrom, $idxCapt);
        // and remove the captured pawn
        FENplace($xFEN, $idxCapt, '0');
    }
    $FENbit = packFEN($xFEN);
    // search for ambiguous castle notation
    //--------------------------------------------
    // remove any extra information from the current castle notations
    if ('' != $WK) {
        $WK = 'K';
    }
    if ('' != $WQ) {
        $WQ = 'Q';
    }
    if ('' != $BK) {
        $BK = 'k';
    }
    if ('' != $BQ) {
        $BQ = 'q';
    }
    // get current position of main pieces
    $whiteBackRank = substr($xFEN, -8);
    $blackBackRank = substr($xFEN, 0, 8);
    // search the ends of the back ranks for rooks
    // and add unambiguous notation if needed
    if (strrpos($whiteBackRank, 'R') > $origHRookPos && '' != $WK) {
        $WK = $WK . substr($COLS, $origHRookPos, 1);
    }
    if (strpos($whiteBackRank, 'R') < $origARookPos && '' != $WQ) {
        $WQ = $WQ . substr($COLS, $origARookPos, 1);
    }
    if (strrpos($blackBackRank, 'r') > $origHRookPos && '' != $BK) {
        $BK = $BK . substr($COLS, $origHRookPos, 1);
    }
    if (strpos($blackBackRank, 'r') < $origARookPos && '' != $BQ) {
        $BQ = $BQ . substr($COLS, $origARookPos, 1);
    }
    $castlingAvail = $WK . $WQ . $BK . $BQ;
    if ('' == $castlingAvail) {
        $castlingAvail = '-';
    }
    // increase the move number (if needed)
    $MN = "w" == $CM ? $MN : ++$MN;
    // make sure to use the pre-increment (++var) here
    // toggle the current move
    $CM = "w" == $CM ? "b" : "w";
    // put the whole thing together and return
    return "{$FENbit} {$CM} {$castlingAvail} {$newEP} {$PN} {$MN}";
}