function getlegalmoves($standard, $fen)
{
    $promotepiece = array('Q', 'R', 'B', 'N');
    $standard->resetGame($fen);
    $toMove = $standard->toMove();
    if ($standard->inCheckMate()) {
        return array('success' => 1, 'result' => oppositewins($toMove), 'msg' => 'wins');
    }
    if ($standard->inRepetitionDraw()) {
        return array('success' => 1, 'result' => 'Draw', 'msg' => 'in repetition');
    }
    if ($standard->inStaleMate()) {
        return array('success' => 1, 'result' => 'Draw', 'msg' => 'in stalemate');
    }
    if ($standard->inDraw()) {
        return array('success' => 1, 'result' => 'Draw', 'msg' => 'in draw');
    }
    if ($standard->gameOver()) {
        return array('success' => 1, 'result' => $standard->gameOver(), 'msg' => 'wins');
    }
    $ploc = $standard->getPieceLocations($toMove);
    $toArray = $standard->toArray();
    $newloc = array();
    if ($ploc) {
        foreach ($ploc as $loc) {
            $newloc[$loc] = $toArray[$loc];
        }
    }
    $list = array();
    if (empty($newloc)) {
        return false;
    } else {
        foreach ($newloc as $sq => $loc) {
            $move = $standard->getPossibleMoves(strtoupper($loc), $sq, $toMove, true);
            if (!empty($move)) {
                foreach ($move as $m) {
                    $promotemove = 0;
                    if (strtoupper($loc) == 'P') {
                        $promotemove = $standard->isPromoteMove($sq, $m);
                    }
                    if (!empty($promotemove)) {
                        foreach ($promotepiece as $piece) {
                            $legalmove = $standard->_convertSquareToSAN($sq, $m, $piece);
                            $parseMove = $standard->_parseMove($legalmove);
                            $validMove = $standard->_validMove($parseMove);
                            if ($validMove === true) {
                                $standard->resetGame($fen);
                                $standard->moveSAN($legalmove);
                                $renderFen = $standard->renderFen();
                                $result = '';
                                if ($standard->inCheckMate()) {
                                    $result = '#';
                                } else {
                                    if ($standard->inRepetitionDraw()) {
                                        $result = ' Draw in Repetition';
                                    } else {
                                        if ($standard->inStaleMate()) {
                                            $result = ' Stalemate Draw';
                                        } else {
                                            if ($standard->inDraw()) {
                                                $result = ' Draw';
                                            } else {
                                                if ($standard->gameOver()) {
                                                    $result = '#';
                                                }
                                            }
                                        }
                                    }
                                }
                                $list[] = array('move' => $legalmove, 'fen' => $renderFen, 'result' => $result);
                                $standard->resetGame($fen);
                            }
                        }
                    } else {
                        $legalmove = $standard->_convertSquareToSAN($sq, $m);
                        $parseMove = $standard->_parseMove($legalmove);
                        $validMove = $standard->_validMove($parseMove);
                        if ($validMove === true) {
                            $standard->resetGame($fen);
                            $standard->moveSAN($legalmove);
                            $renderFen = $standard->renderFen();
                            $result = '';
                            if ($standard->inCheckMate()) {
                                $result = '#';
                            } else {
                                if ($standard->inRepetitionDraw()) {
                                    $result = ' Draw in Repetition';
                                } else {
                                    if ($standard->inStaleMate()) {
                                        $result = ' Stalemate Draw';
                                    } else {
                                        if ($standard->inDraw()) {
                                            $result = ' Draw';
                                        } else {
                                            if ($standard->gameOver()) {
                                                $result = '#';
                                            }
                                        }
                                    }
                                }
                            }
                            $list[] = array('move' => $legalmove, 'fen' => $renderFen, 'result' => $result);
                            $standard->resetGame($fen);
                        }
                    }
                }
            }
        }
    }
    return $list;
}
Exemple #2
0
function getlegalmoves($standard)
{
    global $promotepiece;
    $toMove = $standard->toMove();
    if ($standard->gameOver()) {
        return array('success' => 1, 'result' => $standard->gameOver());
    }
    if ($standard->inCheckMate()) {
        return array('success' => 1, 'result' => oppositewins($toMove));
    }
    if ($standard->inDraw()) {
        return array('success' => 1, 'result' => 3);
    }
    if ($standard->inStaleMate()) {
        return array('success' => 1, 'result' => 4);
    }
    if ($standard->inRepetitionDraw()) {
        return array('success' => 1, 'result' => 5);
    }
    $ploc = $standard->getPieceLocations($toMove);
    $toArray = $standard->toArray();
    $newloc = array();
    if ($ploc) {
        foreach ($ploc as $loc) {
            $newloc[$loc] = $toArray[$loc];
        }
    }
    $list = array();
    if (empty($newloc)) {
        return false;
    } else {
        foreach ($newloc as $sq => $loc) {
            $move = $standard->getPossibleMoves(strtoupper($loc), $sq, $toMove, true);
            if (!empty($move)) {
                foreach ($move as $m) {
                    $promotemove = 0;
                    if (strtoupper($loc) == 'P') {
                        $promotemove = $standard->isPromoteMove($sq, $m);
                    }
                    if (!empty($promotemove)) {
                        foreach ($promotepiece as $piece) {
                            $legalmove = $standard->_convertSquareToSAN($sq, $m, $piece);
                            $parseMove = $standard->_parseMove($legalmove);
                            $validMove = $standard->_validMove($parseMove);
                            if ($validMove === true) {
                                $list[] = $legalmove;
                            }
                        }
                    } else {
                        $legalmove = $standard->_convertSquareToSAN($sq, $m);
                        $parseMove = $standard->_parseMove($legalmove);
                        $validMove = $standard->_validMove($parseMove);
                        if ($validMove === true) {
                            $list[] = $legalmove;
                        }
                    }
                }
            }
        }
    }
    return $list;
}