/// <summary> /// Generates the move. /// Adds check verification to move generation, /// returns null if its own king will be in check. /// </summary> /// <param name="board">The board</param> /// <param name="from">The starting square</param> /// <param name="to">The ending square</param> /// <returns></returns> internal override Move GenerateMove(Board board, int from, int to) { Move move = base.GenerateMove(board, from, to); if (move != null) { // verify for king in check move.Make(board); bool result = !board.WhiteKingInCheck(); move.TakeBack(board); return(result ? move : null); } else { return(null); } }
/// <summary> /// Generates the move. /// Adds check verification to move generation, /// returns null if its own king will be in check. /// </summary> /// <param name="board">The board</param> /// <param name="from">The starting square</param> /// <param name="to">The ending square</param> /// <returns></returns> internal override Move GenerateMove(Board board, int from, int to) { Move move = base.GenerateMove(board, from, to); if (move != null) { move.IncrementMoves();// the number of moves is incremented after Black moves // verify for king in check move.Make(board); bool result = !board.BlackKingInCheck(); move.TakeBack(board); return(result ? move : null); } else { return(null); } }
/// <summary> /// Go to the next board configuration in history. /// </summary> public void Next() { // if the game is not the at the end if (!IsLast) { Move move = moveHistory[currentBoardIndex]; // build the event args CancelMoveEventArgs moveEventArgs = new CancelMoveEventArgs(move, currentBoardIndex - 1); // raise the GoingForward event OnGoingForward(moveEventArgs); // if the operation was cancelled if (moveEventArgs.Cancel) { return; } // make the move move.Make(currentBoard); // add the current board hash to the history AddHistoryHash(); // generate the possible moves GenerateMoves(); // set the status of the game SetStatus(); // increment the current board index currentBoardIndex++; // raise the GoneForward event OnGoneForward(new MoveEventArgs(move, currentBoardIndex - 1)); } }
/// <summary> /// Makes a move. /// If the move is illegal or it's null, throws an Argument(Null)Exception. /// </summary> /// <param name="move">The move</param> public void Make(Move move) { // check to see if the move it's not null and it's valid if (move == null) { throw new ArgumentNullException("move", Resources.NullMoveMsg); } bool findMove = false; foreach (Move m in PossibleMoves) { if (move.From == m.From && move.To == m.To) { findMove = true; break; } } if (!findMove) { throw new ArgumentException(Resources.IllegalMoveMsg, "move"); } // build the event args CancelMoveEventArgs moveEventArgs = new CancelMoveEventArgs(move, currentBoardIndex - 1); // raise the Moving event OnMoving(moveEventArgs); // if the move was cancelled if (moveEventArgs.Cancel) { return; } // remove the moves after current board index (if any) if (!IsLast) { moveHistory.RemoveRange(currentBoardIndex, moveHistory.Count - currentBoardIndex); } // if this is a promotion, the promotion is not set and the promotion delegate is not null, call the promotion delegate if (move is PromotionMove && (move as PromotionMove).PromotionType == null && Promote != null) { (move as PromotionMove).PromotionType = Promote(); } // make the move move.Make(currentBoard); // add the current hash to the history AddHistoryHash(); // generate the possible moves GenerateMoves(); // set the status of the game SetStatus(); // increment the current board index currentBoardIndex++; // add move to history moveHistory.Add(move); // raise the Moved event OnMoved(new MoveEventArgs(move, currentBoardIndex - 1)); }
/// <summary> /// Makes the move, it doesn't check if it's a valid move. /// </summary> /// <param name="board">The board</param> internal override void Make(Board board) { rookMove.Make(board); // make the rook move first base.Make(board); // make the king move last to set the after board status }