chesslablab / php-chess

A chess library for PHP offering move validation, common formats, multiple variants, UCI engine support, explanation of chess positions, and image recognition.
https://chesslablab.github.io/php-chess/
MIT License
110 stars 52 forks source link

@programarivm Could please send me (random vs random) code? #526

Closed programarivm closed 6 months ago

programarivm commented 6 months ago
          @programarivm Could please send me (random vs random) code?

Originally posted by @EarlyEdition in https://github.com/chesslablab/php-chess/issues/488#issuecomment-2079966404

ghost commented 6 months ago

https://chessboardjs.com/examples#5002

I meant something like that.

programarivm commented 6 months ago

Let me elaborate on this one a bit. It seems as if an algorithm needs to be designed for this purpose. Here is how to make a random move from the start position.

$board = new Board();

$legal = [];

foreach ($board->getPieces($board->getTurn()) as $piece) {
    if ($sqs = $piece->sqs()) {
        $legal[$piece->getSq()] = $sqs;
    }
}

$from = array_rand($legal);
shuffle($legal[$from]);
$to = $legal[$from][0];

echo "{$from}{$to}";
d2d4