Exemplo n.º 1
0
        private Position[][] KingMoves(Position initialPos, int[,] board)
        {
            // return 2 arrays in an array. first one is possible moves on empty squares, second is possible captures

            List <Position> moves    = new List <Position>();
            List <Position> captures = new List <Position>();
            Position        pos      = initialPos.Copy(); // making a copy to not alter the input pos

            void process(Position inputPos)
            {
                int result = TryMove(initialPos, inputPos, board);

                if (result == 2)
                {
                    captures.Add(inputPos);
                }
                if (result == 1)
                {
                    moves.Add(inputPos);
                }
            }

            // check three squares above
            pos.y = initialPos.y - 1;
            pos.x = initialPos.x - 1;
            for (int i = 0; i < 3; i++)
            {
                process(pos); pos.x++;
            }
            ;
            pos = initialPos.Copy(); // reseting the position to the initial position

            // check one square to the right
            pos.x++;
            process(pos);
            pos = initialPos.Copy(); // reseting the position to the initial position

            // check three squares below
            pos.y = initialPos.y + 1;
            pos.x = initialPos.x - 1;
            for (int i = 0; i < 3; i++)
            {
                process(pos); pos.x++;
            }
            ;
            pos = initialPos.Copy(); // reseting the position to the initial position

            // check one square to the left
            pos.x--;
            process(pos);

            return(new Position[][] { moves.ToArray(), captures.ToArray() });
        }
Exemplo n.º 2
0
        private Position[][] KnightMoves(Position initialPos, int[,] board)
        {
            // return 2 arrays in an array. first one is possible moves on empty squares, second is possible captures
            List <Position> moves    = new List <Position>();
            List <Position> captures = new List <Position>();
            Position        pos      = initialPos.Copy(); // making a copy to not alter the input pos

            int[,] relativeMoves = new int[8, 2] {
                { -2, 1 }, { -1, 2 }, { 1, 2 }, { 2, 1 }, { 2, -1 }, { 1, -2 }, { -1, -2 }, { -2, -1 }
            };

            for (int i = 0; i < 8; i++)
            {
                pos.y = initialPos.y + relativeMoves[i, 0];
                pos.x = initialPos.x + relativeMoves[i, 1];
                int result = TryMove(initialPos, pos, board);
                if (result == 2)
                {
                    captures.Add(pos);
                }
                if (result == 1)
                {
                    moves.Add(pos);
                }
            }

            return(new Position[][] { moves.ToArray(), captures.ToArray() });
        }
Exemplo n.º 3
0
        private Position[][] BishopMoves(Position initialPos, int[,] board)
        {
            // return 2 arrays in an array. first one is possible moves on empty squares, second is possible captures
            List <Position> moves    = new List <Position>();
            List <Position> captures = new List <Position>();
            Position        pos      = initialPos.Copy(); // making a copy to not alter the input pos

            int process(Position inputPos)
            {
                int result = TryMove(initialPos, inputPos, board);

                if (result == 2)
                {
                    captures.Add(inputPos);
                }
                if (result == 1)
                {
                    moves.Add(inputPos);
                }
                if (result == 3 || result == 2 || result == 0)
                {
                    return(0); // stop checking
                }
                return(1);     // dont stop checking
            }

            // check to top/right
            while (pos.y > 1 && pos.x < 10)
            {
                pos.y--; pos.x++; if (process(pos) == 0)
                {
                    break;
                }
            }
            pos = initialPos.Copy(); // reseting the position to the initial position

            // check to bottom/right
            while (pos.y < 10 && pos.x < 10)
            {
                pos.y++; pos.x++; if (process(pos) == 0)
                {
                    break;
                }
            }
            pos = initialPos.Copy(); // reseting the position to the initial position

            // check to bottom/left
            while (pos.y < 10 && pos.x > 1)
            {
                pos.y++; pos.x--; if (process(pos) == 0)
                {
                    break;
                }
            }
            pos = initialPos.Copy(); // reseting the position to the initial position

            // check to top/left
            while (pos.y > 1 && pos.x > 1)
            {
                pos.y--; pos.x--; if (process(pos) == 0)
                {
                    break;
                }
            }

            return(new Position[][] { moves.ToArray(), captures.ToArray() });
        }
Exemplo n.º 4
0
        private Position[][] PawnMoves(Position initialPos, int[,] board)
        {
            // return 2 arrays in an array. first one is possible moves on empty squares, second is possible captures

            List <Position> moves    = new List <Position>();
            List <Position> captures = new List <Position>();
            Position        pos      = initialPos.Copy(); // making a copy to not alter the input pos

            int piece = board[initialPos.y, initialPos.x];
            int side  = piece / Math.Abs(piece); // -1 - black. 1 - white

            int justMove(Position inputPos)
            {
                int result = TryMove(initialPos, inputPos, board);

                if (result == 1)
                {
                    moves.Add(inputPos);
                }
                if (result == 3 || result == 2 || result == 0)
                {
                    return(0); // stop checking
                }
                return(1);     // dont stop checking
            }

            void justCapture(Position inputPos)
            {
                int result = TryMove(initialPos, inputPos, board);

                if (result == 2)
                {
                    captures.Add(inputPos);
                }
            }

            // check one square to the up/down (depending on the side)
            if (side == 1)
            {
                pos.y = initialPos.y + 1;
                if (justMove(pos) == 1 && pos.y == 4) // second white row
                {
                    pos.y++;
                    justMove(pos);
                    pos.y--;
                }
            }
            else
            {
                pos.y = initialPos.y - 1;
                justMove(pos);
                if (justMove(pos) == 1 && pos.y == 7) // second black row
                {
                    pos.y--;
                    justMove(pos);
                    pos.y++;
                }
            }

            // check diagonally (left)
            pos.x = initialPos.x - 1;
            justCapture(pos);

            // check diagonally (right)
            pos.x = initialPos.x + 1;
            justCapture(pos);

            return(new Position[][] { moves.ToArray(), captures.ToArray() });
        }