private bool IsCheckOrStallMate(out bool isStallMate) { // Use this only in between moves, since it's quite 'slow' (20 microseconds); // slow, but exact way to see if the color to move is check-mate // Do this with a clone, since making captures reorders the indices of pieces in PiecePos. // This reorders future moves. Somehow, this gives problems Board clone = new Board(false); clone.LoadFrom(board); Move[] moves = moveGenerator.GenerateMoves(null); // try all moves. If no one is valid. It's checkmate. bool hasValidMove = false; for (int i = 0; i < moveGenerator.nrGeneratedMoves; i++) { if (board.MakeMove(moves[i])) { hasValidMove = true; } board.UnMakeMove(moves[i]); if (hasValidMove) break; } board.LoadFrom(clone); if (!hasValidMove) { // maybe it's a stall-mate : in this case, the king is not in check isStallMate = !board.IsInCheck(); } else isStallMate = false; return !hasValidMove; }
private void StoreCurrentThinking(int score) { // stores the best found mobes, so far and the score currentScore = score; int nrPVMoves = PrincipalVariation.Length; if (nrPVMoves > maxNrThinkMoves) nrPVMoves = maxNrThinkMoves; bool TTStillMatchesPV = true; // this signals if the PV matches the TT int nrThinkMoves = 0; Move[] thinkMoves = new Move[maxNrThinkMoves]; ulong[] thinkMoveHashValues = new ulong[maxNrThinkMoves]; // First store everything in a clone, since making captures reorders the indices of pieces in PiecePos. // This reorders future moves. Somehow, this gives problems Board clone = new Board(false); clone.LoadFrom(board); // for (int i = 0; i < nrPVMoves; i++) { if (TTStillMatchesPV) { int ttIndex = transpositionTable.GetIndex(board.HashValue, 0); if (ttIndex >= 0) { Move ttMove = new Move(transpositionTable.slots[ttIndex].compressedMove); if (ttMove != PrincipalVariation[i]) TTStillMatchesPV = false; } else TTStillMatchesPV = false; } thinkMoves[i] = PrincipalVariation[i]; thinkMoveHashValues[i] = board.HashValue; board.MakeMove(PrincipalVariation[i]); nrThinkMoves++; } // finished the PrincipalVariation. Now follow the TT nrCurrentMovesFromPV = nrThinkMoves; int ttIndex2; while ((ttIndex2 = transpositionTable.GetIndex(board.HashValue, 0)) != -1) { Move move = new Move(transpositionTable.slots[ttIndex2].compressedMove); if (move.moveType == Const.NoMoveID) break; if (nrThinkMoves >= maxNrThinkMoves - 1) break; thinkMoves[nrThinkMoves] = move; thinkMoveHashValues[nrThinkMoves] = board.HashValue; board.MakeMove(move); nrThinkMoves++; // Check if this move has occured before. Otherwise an endless loop would occurr. // Allow for 3 entries, since this is possible for the 3-move rule (?) int nrSameEntries = 0; for (int i = 0; i < nrThinkMoves; i++) if (thinkMoveHashValues[i] == board.HashValue) nrSameEntries++; if (nrSameEntries >= 3) break; } // now rewind the board by undoing the moves made for (int i = nrThinkMoves - 1; i >= 0; i--) board.UnMakeMove(thinkMoves[i]); // switch back to the original board board.LoadFrom(clone); // // So found the moves, now store them currentMoves = new Move[nrThinkMoves]; for (int i = 0; i < nrThinkMoves; i++) currentMoves[i] = thinkMoves[i]; }
public bool UserMoveIsLegal(string moveString) { // slow : only use for validation of user move Move move = board.FindMoveOnBoard(moveString); if (move == Move.NoMove()) return false; // not found // check if this move leaves the king in check : // Do this with a clone, since making captures reorders the indices of pieces in PiecePos. // This reorders future moves. Somehow, this gives problems Board clone = new Board(false); clone.LoadFrom(board); board.MakeMove(move); board.ToggleMoveColor(); // this was toggled by MakeMove. Undo it. bool leavesKingInCheck = board.IsInCheck(); board.LoadFrom(clone); return !leavesKingInCheck; }