예제 #1
0
파일: Game.cs 프로젝트: stevenaw/chess-app
        private ErrorCondition Move(Move move)
        {
            if (!BitTranslator.IsValidSquare(move.StartFile, move.StartRank))
            {
                return(ErrorCondition.InvalidSquare);
            }
            if (!BitTranslator.IsValidSquare(move.EndFile, move.EndRank))
            {
                return(ErrorCondition.InvalidSquare);
            }

            ulong startSquare = BitTranslator.TranslateToBit(move.StartFile, move.StartRank);

            if ((CurrentTurn & startSquare) == 0)
            {
                return(ErrorCondition.MustMoveOwnPiece); // Can't move if not your turn
            }
            ulong endSquare = BitTranslator.TranslateToBit(move.EndFile, move.EndRank);

            if ((CurrentTurn & endSquare) != 0)
            {
                return(ErrorCondition.CantTakeOwnPiece); // Can't end move on own piece
            }
            ulong allMoves = MoveGenerator.GenerateValidMovesForPiece(CurrentState, startSquare);

            if ((endSquare & allMoves) == 0)
            {
                return(ErrorCondition.InvalidMovement); // End square is not a valid move
            }
            // The move is good, so update state
            // Update current state
            UpdateState(move, startSquare, endSquare);

            return(ErrorCondition.None);
        }
예제 #2
0
파일: Game.cs 프로젝트: stevenaw/chess-app
        public List <Square> GetValidMoves(char file, int rank)
        {
            if (!BitTranslator.IsValidSquare(file, rank))
            {
                throw new InvalidOperationException();
            }

            var   square   = BitTranslator.TranslateToBit(file, rank);
            ulong allMoves = MoveGenerator.GenerateValidMovesForPiece(CurrentState, square);

            return(BitTranslator.TranslateToSquares(allMoves));
        }