Beispiel #1
0
        internal bool TryStringMove(string command)
        {
            var cmd = MoveCommand.Parse(command);

            return(TryPossibleMoveCommand(cmd));
        }
Beispiel #2
0
        public bool TryPossibleMoveCommand(MoveCommand moveCommand)
        {
            if (Ended)
            {
                return(false);
            }

            var fromSquare    = Board.Square(moveCommand.FromFile, moveCommand.FromRank);
            var toSquare      = Board.Square(moveCommand.ToFile, moveCommand.ToRank);
            var possibleMoves = GetPseudoLegalMoves();
            var piece         = fromSquare?.Piece;
            var move          = possibleMoves.SingleOrDefault(x => x.Piece == piece && x.ToSquare == toSquare);

            if (move == null)
            {
                return(false);
            }
            TryPerform(move);
            if (!move.IsLegal.Value)
            {
                return(false);
            }

            if (CurrentPlayer.Color == Color.Black)
            {
                move.NumberInGame = BlackPlayer.Moves.Count + 1;
            }
            else
            {
                move.NumberInGame = (BlackPlayer.Moves.FirstOrDefault()?.NumberInGame ?? 0) + 1;
            }

            PerformLegalMove(move);
            CommandCount++;
            if (CommandCount > 127) //There was not room for a bigger number
            {
                CommandCount = 0;
                PositionsDatabase.Instance.Reset();
            }
            HashHistory.Push(Hash);

            if (Ended)
            {
                return(true);
            }

            var nextMoves = GetLegalNextMoves(CurrentPlayer.Color);

            if (!nextMoves.Any())
            {
                Ended = true;
                if (CurrentPlayer.IsChecked)
                {
                    move.ScoreInfo     |= ScoreInfo.Mate;
                    CurrentPlayer.Mated = true;
                    Winner = OtherPlayer;
                }
                else
                {
                    move.ScoreInfo |= ScoreInfo.StaleMate;
                    IsStaleMate     = true;
                    Ended           = true;
                }
            }
            return(true);
        }