public static List <Move> GetKingMoves(byte[] gamePosition, HelperVector2Int startPosition)
        {
            List <Move> PossibleMoves = new List <Move>();

            PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x + 1, startPosition.y + 1), PieceData.King, PieceData.King));
            PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x + 1, startPosition.y - 1), PieceData.King, PieceData.King));
            PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x - 1, startPosition.y + 1), PieceData.King, PieceData.King));
            PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x - 1, startPosition.y - 1), PieceData.King, PieceData.King));
            PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x + 1, startPosition.y), PieceData.King, PieceData.King));
            PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x - 1, startPosition.y), PieceData.King, PieceData.King));
            PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x, startPosition.y + 1), PieceData.King, PieceData.King));
            PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x, startPosition.y - 1), PieceData.King, PieceData.King));

            List <Move> ToBeRemovedMoves = GetOutOfBoundsMoves(gamePosition, PossibleMoves);

            foreach (Move move in ToBeRemovedMoves)
            {
                PossibleMoves.Remove(move);
            }

            ToBeRemovedMoves = GetBlockedMoves(gamePosition, PossibleMoves);

            foreach (Move move in ToBeRemovedMoves)
            {
                PossibleMoves.Remove(move);
            }

            return(PossibleMoves);
        }
        public static List <Move> GetRookMoves(byte[] gamePosition, HelperVector2Int startPosition)
        {
            List <Move> PossibleMoves = new List <Move>();

            for (int i = 1; i < 8; ++i)
            {
                PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x + i, startPosition.y), PieceData.Rook, PieceData.Rook));
                PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x - i, startPosition.y), PieceData.Rook, PieceData.Rook));
                PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x, startPosition.y + i), PieceData.Rook, PieceData.Rook));
                PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x, startPosition.y - i), PieceData.Rook, PieceData.Rook));
            }

            List <Move> ToBeRemovedMoves = GetOutOfBoundsMoves(gamePosition, PossibleMoves);

            foreach (Move move in ToBeRemovedMoves)
            {
                PossibleMoves.Remove(move);
            }

            ToBeRemovedMoves = GetInterceptedMoves(gamePosition, PossibleMoves, new HelperVector2Int(1, 0));
            ToBeRemovedMoves.AddRange(GetInterceptedMoves(gamePosition, PossibleMoves, new HelperVector2Int(-1, 0)));
            ToBeRemovedMoves.AddRange(GetInterceptedMoves(gamePosition, PossibleMoves, new HelperVector2Int(0, 1)));
            ToBeRemovedMoves.AddRange(GetInterceptedMoves(gamePosition, PossibleMoves, new HelperVector2Int(0, -1)));

            foreach (Move move in ToBeRemovedMoves)
            {
                PossibleMoves.Remove(move);
            }

            return(PossibleMoves);
        }
        public static List <Move> GetBishopMoves(byte[] gamePosition, HelperVector2Int startPosition)
        {
            byte        color         = ((gamePosition[startPosition.x + startPosition.y * 8] & PieceData.White) != 0) ? (PieceData.White) : (PieceData.Black);
            List <Move> PossibleMoves = new List <Move>();

            for (int i = 1; i < 8; ++i)
            {
                PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x + i, startPosition.y + i), PieceData.Bishop, PieceData.Bishop));
                PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x + i, startPosition.y - i), PieceData.Bishop, PieceData.Bishop));
                PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x - i, startPosition.y + i), PieceData.Bishop, PieceData.Bishop));
                PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x - i, startPosition.y - i), PieceData.Bishop, PieceData.Bishop));
            }

            List <Move> ToBeRemovedMoves = GetOutOfBoundsMoves(gamePosition, PossibleMoves);

            foreach (Move move in ToBeRemovedMoves)
            {
                PossibleMoves.Remove(move);
            }

            ToBeRemovedMoves = GetInterceptedMoves(gamePosition, PossibleMoves, new HelperVector2Int(1, 1));
            ToBeRemovedMoves.AddRange(GetInterceptedMoves(gamePosition, PossibleMoves, new HelperVector2Int(1, -1)));
            ToBeRemovedMoves.AddRange(GetInterceptedMoves(gamePosition, PossibleMoves, new HelperVector2Int(-1, 1)));
            ToBeRemovedMoves.AddRange(GetInterceptedMoves(gamePosition, PossibleMoves, new HelperVector2Int(-1, -1)));

            foreach (Move move in ToBeRemovedMoves)
            {
                PossibleMoves.Remove(move);
            }

            return(PossibleMoves);
        }
示例#4
0
 public Move(HelperVector2Int startPosition, HelperVector2Int targetPosition, byte startPiece, byte endPiece)
 {
     StartPosition  = startPosition;
     TargetPosition = targetPosition;
     StartPiece     = startPiece;
     EndPiece       = endPiece;
 }
        public static List <Move> GetChecks(byte[] gamePosition, byte kingColor, List <Move> possibleEnemyMoves)
        {
            HelperVector2Int tempKing = PieceData.GetKingPosition(gamePosition, kingColor);
            HelperVector2Int KingPosition;

            if (tempKing != null)
            {
                KingPosition = tempKing;
            }
            else
            {
                return(new List <Move>());
            }

            return(possibleEnemyMoves.Where(x => x.TargetPosition.x.Equals(KingPosition.x) && x.TargetPosition.y.Equals(KingPosition.y)).ToList());
        }
        public static List <Move> GetPawnMoves(byte[] gamePosition, HelperVector2Int startPosition)
        {
            List <Move> PossibleMoves = new List <Move>();

            if ((gamePosition[startPosition.x + startPosition.y * 8] & PieceData.White) != 0)
            {
                if (startPosition.y < 6)
                {
                    //move one forward
                    if (gamePosition[startPosition.x + (startPosition.y + 1) * 8].Equals(PieceData.None))
                    {
                        PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x, startPosition.y + 1), PieceData.Pawn, PieceData.Pawn));
                    }

                    //capture to the right
                    if (startPosition.x + 1 <= 7 && (gamePosition[startPosition.x + 1 + (startPosition.y + 1) * 8] & PieceData.Black) != 0)
                    {
                        PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x + 1, startPosition.y + 1), PieceData.Pawn, PieceData.Pawn));
                    }

                    //capture to the left
                    if (startPosition.x - 1 >= 0 && (gamePosition[startPosition.x - 1 + (startPosition.y + 1) * 8] & PieceData.Black) != 0)
                    {
                        PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x - 1, startPosition.y + 1), PieceData.Pawn, PieceData.Pawn));
                    }

                    //move two forward
                    if (startPosition.y == 1 && gamePosition[startPosition.x + (startPosition.y + 2) * 8].Equals(PieceData.None) && gamePosition[startPosition.x + (startPosition.y + 1) * 8].Equals(PieceData.None))
                    {
                        PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x, startPosition.y + 2), PieceData.Pawn, PieceData.Pawn));
                    }
                }
                else if (startPosition.y == 6)
                {
                    //move one forward
                    if (gamePosition[startPosition.x + (startPosition.y + 1) * 8].Equals(PieceData.None))
                    {
                        PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x, startPosition.y + 1), PieceData.Pawn, PieceData.Knight));
                        PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x, startPosition.y + 1), PieceData.Pawn, PieceData.Bishop));
                        PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x, startPosition.y + 1), PieceData.Pawn, PieceData.Rook));
                        PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x, startPosition.y + 1), PieceData.Pawn, PieceData.Queen));
                    }

                    //capture to the right
                    if (startPosition.x + 1 <= 7 && (gamePosition[startPosition.x + 1 + (startPosition.y + 1) * 8] & PieceData.Black) != 0)
                    {
                        PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x + 1, startPosition.y + 1), PieceData.Pawn, PieceData.Knight));
                        PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x + 1, startPosition.y + 1), PieceData.Pawn, PieceData.Bishop));
                        PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x + 1, startPosition.y + 1), PieceData.Pawn, PieceData.Rook));
                        PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x + 1, startPosition.y + 1), PieceData.Pawn, PieceData.Queen));
                    }

                    //capture to the left
                    if (startPosition.x - 1 >= 0 && (gamePosition[startPosition.x - 1 + (startPosition.y + 1) * 8] & PieceData.Black) != 0)
                    {
                        PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x - 1, startPosition.y + 1), PieceData.Pawn, PieceData.Knight));
                        PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x - 1, startPosition.y + 1), PieceData.Pawn, PieceData.Bishop));
                        PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x - 1, startPosition.y + 1), PieceData.Pawn, PieceData.Rook));
                        PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x - 1, startPosition.y + 1), PieceData.Pawn, PieceData.Queen));
                    }
                }
            }
            else if ((gamePosition[startPosition.x + startPosition.y * 8] & PieceData.Black) != 0)
            {
                if (startPosition.y > 1)
                {
                    //move one forward
                    if (gamePosition[startPosition.x + (startPosition.y - 1) * 8].Equals(PieceData.None))
                    {
                        PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x, startPosition.y - 1), PieceData.Pawn, PieceData.Pawn));
                    }

                    //capture to the rigth
                    if (startPosition.x + 1 <= 7 && (gamePosition[startPosition.x + 1 + (startPosition.y - 1) * 8] & PieceData.White) != 0)
                    {
                        PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x + 1, startPosition.y - 1), PieceData.Pawn, PieceData.Pawn));
                    }

                    //capture to the left
                    if (startPosition.x - 1 >= 0 && (gamePosition[startPosition.x - 1 + (startPosition.y - 1) * 8] & PieceData.White) != 0)
                    {
                        PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x - 1, startPosition.y - 1), PieceData.Pawn, PieceData.Pawn));
                    }

                    //move two forward
                    if (startPosition.y == 6 && gamePosition[startPosition.x + (startPosition.y - 2) * 8].Equals(PieceData.None) && gamePosition[startPosition.x + (startPosition.y - 1) * 8].Equals(PieceData.None))
                    {
                        PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x, startPosition.y - 2), PieceData.Pawn, PieceData.Pawn));
                    }
                }
                else if (startPosition.y == 1)
                {
                    //move one forward
                    if (gamePosition[startPosition.x + (startPosition.y - 1) * 8].Equals(PieceData.None))
                    {
                        PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x, startPosition.y - 1), PieceData.Pawn, PieceData.Knight));
                        PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x, startPosition.y - 1), PieceData.Pawn, PieceData.Bishop));
                        PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x, startPosition.y - 1), PieceData.Pawn, PieceData.Rook));
                        PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x, startPosition.y - 1), PieceData.Pawn, PieceData.Queen));
                    }

                    //capture to the rigth
                    if (startPosition.x + 1 <= 7 && (gamePosition[startPosition.x + 1 + (startPosition.y - 1) * 8] & PieceData.White) != 0)
                    {
                        PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x + 1, startPosition.y - 1), PieceData.Pawn, PieceData.Knight));
                        PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x + 1, startPosition.y - 1), PieceData.Pawn, PieceData.Bishop));
                        PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x + 1, startPosition.y - 1), PieceData.Pawn, PieceData.Rook));
                        PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x + 1, startPosition.y - 1), PieceData.Pawn, PieceData.Queen));
                    }

                    //capture to the left
                    if (startPosition.x - 1 >= 0 && (gamePosition[startPosition.x - 1 + (startPosition.y - 1) * 8] & PieceData.White) != 0)
                    {
                        PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x - 1, startPosition.y - 1), PieceData.Pawn, PieceData.Knight));
                        PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x - 1, startPosition.y - 1), PieceData.Pawn, PieceData.Bishop));
                        PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x - 1, startPosition.y - 1), PieceData.Pawn, PieceData.Rook));
                        PossibleMoves.Add(new Move(startPosition, new HelperVector2Int(startPosition.x - 1, startPosition.y - 1), PieceData.Pawn, PieceData.Queen));
                    }
                }
            }

            return(PossibleMoves);
        }
        /// <summary>
        /// Returns moves intercepted by own or enemy pieces
        /// </summary>
        private static List <Move> GetInterceptedMoves(byte[] gamePosition, List <Move> possibleMoves, HelperVector2Int directionVector)
        {
            IOrderedEnumerable <Move> tempCollection;

            if (directionVector.x >= 0)
            {
                tempCollection = possibleMoves.OrderBy(x => x.TargetPosition.x);
            }
            else
            {
                tempCollection = possibleMoves.OrderByDescending(x => x.TargetPosition.x);
            }

            if (directionVector.y >= 0)
            {
                possibleMoves = tempCollection.ThenBy(x => x.TargetPosition.y).ToList();
            }
            else
            {
                possibleMoves = tempCollection.ThenByDescending(x => x.TargetPosition.x).ToList();
            }

            HelperVector2Int startPosition = possibleMoves.First().StartPosition;
            byte             color         = ((gamePosition[startPosition.x + startPosition.y * 8] & PieceData.White) != 0) ? (PieceData.White) : (PieceData.Black);

            List <Move> InvalidMoves    = new List <Move>();
            List <Move> ToBeTestedMoves = new List <Move>();

            List <Move> DirectionMoves = new List <Move>();

            int x = startPosition.x;
            int y = startPosition.y;

            while (x < 8 && x >= 0 && y < 8 && y >= 0)
            {
                DirectionMoves.Add(new Move(startPosition, new HelperVector2Int(x, y), PieceData.None, PieceData.None));
                x += directionVector.x;
                y += directionVector.y;
            }

            ToBeTestedMoves = possibleMoves.Where(x => DirectionMoves.Any(y => y.TargetPosition.x.Equals(x.TargetPosition.x) && y.TargetPosition.y.Equals(x.TargetPosition.y))).ToList();

            bool blocked = false;

            foreach (Move move in ToBeTestedMoves)
            {
                if (blocked)
                {
                    InvalidMoves.Add(move);
                }
                else
                {
                    if (gamePosition[move.TargetPosition.x + move.TargetPosition.y * 8].Equals(PieceData.None))
                    {
                        continue;
                    }
                    else if ((gamePosition[move.TargetPosition.x + move.TargetPosition.y * 8] & color) != 0)
                    {
                        InvalidMoves.Add(move);
                        blocked = true;
                    }
                    else if (!((gamePosition[move.TargetPosition.x + move.TargetPosition.y * 8] & color) != 0))
                    {
                        blocked = true;
                    }
                }
            }

            return(InvalidMoves);
        }