예제 #1
0
        private bool TryGetUserMove(Coordinate from, Coordinate to, out Move move)
        {
            ReadOnlyCollection<Move> possibleMoves = m_gameState.GenerateMoves();
            move = possibleMoves.SingleOrDefault(x => x.From.File == from.File && x.From.Rank == from.Rank && x.To.File == to.File && x.To.Rank == to.Rank);

            return move != null;
        }
예제 #2
0
파일: GameState.cs 프로젝트: Kashll/Chess
        public bool MakeMove(Move move)
        {
            bool isLegalMove = GenerateMoves().Any(x =>
                x.MoveType == move.MoveType &&
                x.PromoteTo == move.PromoteTo &&
                ((x.From == null && move.From == null) || (x.From != null && move.From != null && x.From.File == move.From.File && x.From.Rank == move.From.Rank)) &&
                ((x.To == null && move.To == null) || (x.To != null && move.To != null && x.To.File == move.To.File && x.To.Rank == move.To.Rank)));

            if (!isLegalMove)
                throw new ArgumentException("illegal move");

            switch (move.MoveType)
            {
                case MoveType.Standard:
                    Piece pieceToMove = Board[move.From.BoardRow(), move.From.BoardColumn()].Piece;
                    Board[move.To.BoardRow(), move.To.BoardColumn()].Piece = Board[move.From.BoardRow(), move.From.BoardColumn()].Piece;
                    Board[move.From.BoardRow(), move.From.BoardColumn()].Piece = null;

                    if (pieceToMove.Type == PieceType.King)
                    {
                        m_whiteKingMoved |= pieceToMove.Color == Color.White;
                        m_blackKingMoved |= pieceToMove.Color == Color.Black;
                    }
                    else if (pieceToMove.Type == PieceType.Rook)
                    {
                        m_whiteQueenRookMoved |= pieceToMove.Color == Color.White && move.From.BoardRow() == 0 && move.From.BoardColumn() == 0;
                        m_whiteKingRookMoved |= pieceToMove.Color == Color.White && move.From.BoardRow() == 0 && move.From.BoardColumn() == 7;
                        m_blackQueenRookMoved |= pieceToMove.Color == Color.Black && move.From.BoardRow() == 7 && move.From.BoardColumn() == 0;
                        m_blackKingRookMoved |= pieceToMove.Color == Color.Black && move.From.BoardRow() == 7 && move.From.BoardColumn() == 7;
                    }

                    break;
                case MoveType.PawnStart:
                    m_enPassantColumn = move.From.BoardColumn();
                    Board[move.To.BoardRow(), move.To.BoardColumn()].Piece = Board[move.From.BoardRow(), move.From.BoardColumn()].Piece;
                    Board[move.From.BoardRow(), move.From.BoardColumn()].Piece = null;
                    break;
                case MoveType.EnPassant:
                    int rowOffset = m_playerTurn == Color.White ? -1 : 1;
                    Board[move.To.BoardRow(), move.To.BoardColumn()].Piece = Board[move.From.BoardRow(), move.From.BoardColumn()].Piece;
                    Board[move.From.BoardRow(), move.From.BoardColumn()].Piece = null;
                    Board[move.To.BoardRow() + rowOffset, move.To.BoardColumn()].Piece = null;
                    break;
                case MoveType.Promotion:
                    Board[move.To.BoardRow(), move.To.BoardColumn()].Piece = move.PromoteTo;
                    Board[move.From.BoardRow(), move.From.BoardColumn()].Piece = null;
                    break;
                case MoveType.CastleKingside:
                    int kRow = m_playerTurn == Color.White ? 0 : 7;
                    Board[kRow, 5].Piece = Board[kRow, 7].Piece;
                    Board[kRow, 6].Piece = Board[kRow, 4].Piece;
                    Board[kRow, 4].Piece = null;
                    Board[kRow, 7].Piece = null;

                    if (m_playerTurn == Color.White)
                    {
                        m_whiteKingMoved = true;
                        m_whiteKingRookMoved = true;
                    }
                    else
                    {
                        m_blackKingMoved = true;
                        m_blackKingRookMoved = true;
                    }
                    break;
                case MoveType.CastleQueenside:
                    int qRow = m_playerTurn == Color.White ? 0 : 7;
                    Board[qRow, 2].Piece = Board[qRow, 4].Piece;
                    Board[qRow, 3].Piece = Board[qRow, 0].Piece;
                    Board[qRow, 0].Piece = null;
                    Board[qRow, 4].Piece = null;

                    if (m_playerTurn == Color.White)
                    {
                        m_whiteKingMoved = true;
                        m_whiteQueenRookMoved = true;
                    }
                    else
                    {
                        m_blackKingMoved = true;
                        m_blackQueenRookMoved = true;
                    }

                    break;
                default:
                    throw new ArgumentOutOfRangeException("move");
            }

            // reset enPassant state
            if (move.MoveType != MoveType.PawnStart)
                m_enPassantColumn = null;

            // check to see if the game is over
            bool hasWhiteKing = false;
            bool hasBlackKing = false;
            for (int i = 0; i < Board.GetLength(0); i++)
            {
                for (int j = 0; j < Board.GetLength(0); j++)
                {
                    if (Board[i, j].Piece != null && Board[i, j].Piece.Type == PieceType.King)
                    {
                        hasWhiteKing |= Board[i, j].Piece.Color == Color.White;
                        hasBlackKing |= Board[i, j].Piece.Color == Color.Black;
                    }
                }
            }
            if (!hasWhiteKing)
                GameResult = GameResult.BlackWin;
            else if (!hasBlackKing)
                GameResult = GameResult.WhiteWin;

            PlayerTurn = m_playerTurn == Color.White ? Color.Black : Color.White;
            RaisePropertyChanged(BoardProperty);
            return true;
        }
예제 #3
0
        private bool TryGetUserMove(string moveNotation, out Move move)
        {
            move = null;
            if (moveNotation.IsNullOrEmpty())
                return false;

            // castle kingside
            if (moveNotation.ToLowerInvariant() == "0-0")
            {
                move = Move.CastleKingside(m_gameState.PlayerTurn);
                return true;
            }

            // castle queenside
            if (moveNotation.ToLowerInvariant() == "0-0-0")
            {
                move = Move.CastleQueenside(m_gameState.PlayerTurn);
                return true;
            }

            if (moveNotation.Length != 5)
                return false;

            Coordinate from;
            if (!AlgebraicNotationUtility.TryConvertToCoordinate(moveNotation.Substring(0, 2), out from))
                return false;

            Coordinate to;
            if (!AlgebraicNotationUtility.TryConvertToCoordinate(moveNotation.Substring(3, 2), out to))
                return false;

            TryGetUserMove(from, to, out move);
            return move != null;
        }