Example #1
0
        /// <summary>
        /// Player should at least:
        /// * Check if there is immediate checkmate available
        /// * Check if there is possible checkmate in two turns.
        ///
        /// </summary>
        /// <param name="allMoves"></param>
        /// <returns></returns>
        private SingleMove AnalyzeBestMove(IList <SingleMove> allMoves)
        {
            var isMaximizing = IsPlayerWhite;


            if (Phase == GamePhase.MidEndGame || Phase == GamePhase.EndGame)
            {
                var checkMate = MoveResearch.ImmediateCheckMateAvailable(allMoves, Board, isMaximizing);
                if (checkMate != null)
                {
                    return(checkMate);
                }

                var twoTurnCheckMates = MoveResearch.CheckMateInTwoTurns(allMoves, Board, isMaximizing);
                if (twoTurnCheckMates.Count > 1)
                {
                    var evaluatedCheckMates = MoveResearch.GetMoveScoreListParallel(twoTurnCheckMates, SearchDepth, Board, isMaximizing);
                    return(MoveResearch.SelectBestMove(evaluatedCheckMates, isMaximizing));
                }
                else if (twoTurnCheckMates.Count > 0)
                {
                    return(twoTurnCheckMates.First());
                }
            }

            // TODO separate logic to different layers. e.g. player depth at 2, 4 and when to use simple isCheckMate
            var        evaluated = MoveResearch.GetMoveScoreListParallel(allMoves, SearchDepth, Board, isMaximizing);
            SingleMove bestMove  = MoveResearch.SelectBestMove(evaluated, isMaximizing);

            return(bestMove);
        }
Example #2
0
 public void UpdateCastlingStatusFromMove(SingleMove move)
 {
     if (move.NewPos.row == 0)
     {
         WhiteLeftCastlingValid  = false;
         WhiteRightCastlingValid = false;
     }
     else if (move.NewPos.row == 7)
     {
         BlackLeftCastlingValid  = false;
         BlackRightCastlingValid = false;
     }
 }
Example #3
0
        public sealed override void ReceiveMove(IMove opponentMove)
        {
            LatestOpponentMove = opponentMove;

            if (!_connectionTestOverride)
            {
                // Basic validation
                var move = new SingleMove(opponentMove);
                if (Board.ValueAt(move.PrevPos) == null)
                {
                    throw new ArgumentException($"Player [isWhite={!IsPlayerWhite}] Tried to move a from position that is empty");
                }

                if (Board.ValueAt(move.PrevPos) is PieceBase opponentPiece)
                {
                    if (opponentPiece.IsWhite == IsPlayerWhite)
                    {
                        throw new ArgumentException($"Opponent tried to move player piece");
                    }
                }

                // TODO intelligent analyzing what actually happened

                if (Board.ValueAt(move.NewPos) is PieceBase playerPiece)
                {
                    // Opponent captures player targetpiece
                    if (playerPiece.IsWhite == IsPlayerWhite)
                    {
                        move.Capture = true;
                    }
                    else
                    {
                        throw new ArgumentException("Opponent tried to capture own piece.");
                    }
                }

                Board.ExecuteMove(move);
                GameHistory.Add(opponentMove);
                TurnCount++;
            }
        }