public function testBlackFirst() { $chess = new ChessPublicator(); $chess->move('e4'); $fen = $chess->fen(); $chess->load($fen); // do setup with black first $chess->move('e5'); $chess->move('Nf3'); $chess->move('Nc6'); //~ $pgn = $chess->pgn([ 'max_width' => 40, 'new_line' => PHP_EOL ]); $pgn = $chess->pgn(); //~ echo $pgn;exit; // check setup ok $this->assertContains('[SetUp "1"]', $pgn); $this->assertContains('[FEN "' . $fen . '"]', $pgn); // check movements $this->assertContains('1. ... e5', $pgn); $this->assertContains('2. Nf3 Nc6', $pgn); }
public function testMoveToSAN() { $chess = new ChessPublicator(); // normal pawn move $move = $chess->buildMovePublic($chess->turn(), $chess->getBoard(), Chess::SQUARES['e2'], Chess::SQUARES['e4'], Chess::BITS['NORMAL']); $chess->makeMovePublic($move); $undo = $chess->undo(); $this->assertSame($undo['san'], 'e4'); // normal knight move $chess->makeMovePublic($move); $move = $chess->buildMovePublic($chess->turn(), $chess->getBoard(), Chess::SQUARES['g8'], Chess::SQUARES['f6'], Chess::BITS['NORMAL']); $chess->makeMovePublic($move); $undo = $chess->undo(); $this->assertSame($undo['san'], 'Nf6'); // normal pawn capture $chess->load('rnbqkbnr/ppp1pppp/8/3p4/4P3/8/PPPP1PPP/RNBQKBNR w KQkq d6 0 2'); $move = $chess->buildMovePublic($chess->turn(), $chess->getBoard(), Chess::SQUARES['e4'], Chess::SQUARES['d5'], Chess::BITS['CAPTURE']); $chess->makeMovePublic($move); $undo = $chess->undo(); $this->assertSame($undo['san'], 'exd5'); // en passant capture $chess->load('rnbqkbnr/ppp2ppp/8/3Pp3/8/8/PPPP1PPP/RNBQKBNR w KQkq - 0 1'); $move = $chess->buildMovePublic($chess->turn(), $chess->getBoard(), Chess::SQUARES['d5'], Chess::SQUARES['e6'], Chess::BITS['EP_CAPTURE']); $chess->makeMovePublic($move); $undo = $chess->undo(); $this->assertSame($undo['san'], 'dxe6'); // normal knight capture $chess->load('rnbqkb1r/ppp1pppp/5n2/3P4/8/5N2/PPPP1PPP/RNBQKB1R b KQkq - 2 3'); $move = $chess->buildMovePublic($chess->turn(), $chess->getBoard(), Chess::SQUARES['f6'], Chess::SQUARES['d5'], Chess::BITS['CAPTURE']); $chess->makeMovePublic($move); $undo = $chess->undo(); $this->assertSame($undo['san'], 'Nxd5'); // promotion $chess->load('8/2KP4/8/5k2/8/8/8/8 w - - 0 1'); $move = $chess->buildMovePublic($chess->turn(), $chess->getBoard(), Chess::SQUARES['d7'], Chess::SQUARES['d8'], Chess::BITS['PROMOTION'], Chess::ROOK); $chess->makeMovePublic($move); $undo = $chess->undo(); $this->assertSame($undo['san'], 'd8=R'); // check $chess->load('3R4/2K5/8/5k2/8/8/8/8 w - - 0 1'); $move = $chess->buildMovePublic($chess->turn(), $chess->getBoard(), Chess::SQUARES['d8'], Chess::SQUARES['f8'], Chess::BITS['NORMAL']); $chess->makeMovePublic($move); $undo = $chess->undo(); $this->assertSame($undo['san'], 'Rf8+'); // checkmate $chess->load('5k2/8/1R3K2/8/8/8/8/8 w - - 0 1'); $move = $chess->buildMovePublic($chess->turn(), $chess->getBoard(), Chess::SQUARES['b6'], Chess::SQUARES['b8'], Chess::BITS['NORMAL']); $chess->makeMovePublic($move); $undo = $chess->undo(); $this->assertSame($undo['san'], 'Rb8#'); // ambiguous moves: row $chess->load('2N2k2/8/3p4/8/2N5/8/1K6/8 w - - 0 1'); $move = $chess->buildMovePublic($chess->turn(), $chess->getBoard(), Chess::SQUARES['c4'], Chess::SQUARES['d6'], Chess::BITS['CAPTURE']); $chess->makeMovePublic($move); $undo = $chess->undo(); $this->assertSame($undo['san'], 'N4xd6'); // ambiguous moves: rank > 0 & file > 0 $chess->load('8/8/8/2qqq3/2qPq3/2qqq3/1n6/K6k b - - 0 1'); // this one is really ambiguous haha $move = $chess->buildMovePublic($chess->turn(), $chess->getBoard(), Chess::SQUARES['d5'], Chess::SQUARES['d4'], Chess::BITS['CAPTURE']); $chess->makeMovePublic($move); $undo = $chess->undo(); $this->assertSame($undo['san'], 'Qd5xd4'); // ambiguous moves: col $chess->load('5k2/8/3p4/8/2N1N3/8/1K6/8 w - - 0 1'); $move = $chess->buildMovePublic($chess->turn(), $chess->getBoard(), Chess::SQUARES['e4'], Chess::SQUARES['d6'], Chess::BITS['CAPTURE']); $chess->makeMovePublic($move); $undo = $chess->undo(); $this->assertSame($undo['san'], 'Nexd6'); // ambiguous moves: col $chess->load('5k2/8/3p4/8/2N1N3/8/1K6/8 w - - 0 1'); $move = $chess->buildMovePublic($chess->turn(), $chess->getBoard(), Chess::SQUARES['c4'], Chess::SQUARES['d6'], Chess::BITS['CAPTURE']); $chess->makeMovePublic($move); $undo = $chess->undo(); $this->assertSame($undo['san'], 'Ncxd6'); // ambiguous moves: normal capture $chess->load('5k2/8/3p2R1/8/2N5/8/1K6/8 w - - 0 1'); $move = $chess->buildMovePublic($chess->turn(), $chess->getBoard(), Chess::SQUARES['c4'], Chess::SQUARES['d6'], Chess::BITS['CAPTURE']); $chess->makeMovePublic($move); $undo = $chess->undo(); $this->assertSame($undo['san'], 'Nxd6'); // ambiguous moves: normal capture $chess->load('5k2/8/3p2R1/8/2N5/8/1K6/8 w - - 0 1'); $move = $chess->buildMovePublic($chess->turn(), $chess->getBoard(), Chess::SQUARES['g6'], Chess::SQUARES['d6'], Chess::BITS['CAPTURE']); $chess->makeMovePublic($move); $undo = $chess->undo(); $this->assertSame($undo['san'], 'Rxd6'); // generate moves test $chess->load('8/ppp2P2/pkp5/ppp5/5PPP/5PKP/5PPP/8 w - - 0 1'); $moves = $chess->generateMovesPublic(); array_walk($moves, function (&$move) use($chess) { $move = $chess->moveToSANPublic($move); }); $this->assertContains('f8=Q', $moves); $this->assertContains('f8=R', $moves); $this->assertContains('f8=B', $moves); $this->assertContains('f8=N', $moves); $this->assertContains('f5', $moves); $this->assertContains('g5', $moves); $this->assertContains('h5', $moves); }
public function testInStalemate() { $chess = new ChessPublicator(); $this->assertSame($chess->inStalemate(), false); // fen source: https://www.redhotpawn.com/forum/only-chess/interesting-stalemate-position.152109 // start fen : 3b3k/p6p/1p5P/3q4/8/n7/PP6/K4Q2 w - - 0 1 $chess->load('7k/p6p/1p3b1P/3q4/8/n7/PP6/K7 w - - 0 2'); $this->assertSame($chess->inStalemate(), true); }