Ejemplo n.º 1
0
        public override bool[,] PossibleMoviments()
        {
            // It will save all possible moves for the root.
            bool[,] possibleMovimentsMatrix = new bool[Board.Rows, Board.Columns];

            PositionBoard position = new PositionBoard(0, 0);

            // Above
            position.SetPositionValues(PositionBoard.Row - 1, PositionBoard.Column);
            while (Board.PositionIsValid(position) && youCanMove(position))
            {
                possibleMovimentsMatrix[position.Row, position.Column] = true;
                if (Board.Piece(position) != null && Board.Piece(position).Color != Color)
                {
                    break;
                }
                position.Row = position.Row - 1;
            }

            // Below
            position.SetPositionValues(PositionBoard.Row + 1, PositionBoard.Column);
            while (Board.PositionIsValid(position) && youCanMove(position))
            {
                possibleMovimentsMatrix[position.Row, position.Column] = true;
                if (Board.Piece(position) != null && Board.Piece(position).Color != Color)
                {
                    break;
                }
                position.Row = position.Row + 1;
            }

            // Right
            position.SetPositionValues(PositionBoard.Row, PositionBoard.Column + 1);
            while (Board.PositionIsValid(position) && youCanMove(position))
            {
                possibleMovimentsMatrix[position.Row, position.Column] = true;
                if (Board.Piece(position) != null && Board.Piece(position).Color != Color)
                {
                    break;
                }
                position.Column = position.Column + 1;
            }

            // Left
            position.SetPositionValues(PositionBoard.Row, PositionBoard.Column - 1);
            while (Board.PositionIsValid(position) && youCanMove(position))
            {
                possibleMovimentsMatrix[position.Row, position.Column] = true;
                if (Board.Piece(position) != null && Board.Piece(position).Color != Color)
                {
                    break;
                }
                position.Column = position.Column - 1;
            }
            return(possibleMovimentsMatrix);
        }
Ejemplo n.º 2
0
        public override bool[,] PossibleMoviments()
        {
            // It will save all possible moves for the knight.
            bool[,] possibleMovimentsMatrix = new bool[Board.Rows, Board.Columns];

            PositionBoard position = new PositionBoard(0, 0);

            // Above
            position.SetPositionValues(PositionBoard.Row - 1, PositionBoard.Column - 2);
            if (Board.PositionIsValid(position) && youCanMove(position))
            {
                possibleMovimentsMatrix[position.Row, position.Column] = true;
            }

            // NorthEast
            position.SetPositionValues(PositionBoard.Row - 2, PositionBoard.Column - 1);
            if (Board.PositionIsValid(position) && youCanMove(position))
            {
                possibleMovimentsMatrix[position.Row, position.Column] = true;
            }

            // Right
            position.SetPositionValues(PositionBoard.Row - 2, PositionBoard.Column + 1);
            if (Board.PositionIsValid(position) && youCanMove(position))
            {
                possibleMovimentsMatrix[position.Row, position.Column] = true;
            }

            // South-west
            position.SetPositionValues(PositionBoard.Row - 1, PositionBoard.Column + 2);
            if (Board.PositionIsValid(position) && youCanMove(position))
            {
                possibleMovimentsMatrix[position.Row, position.Column] = true;
            }

            // Below
            position.SetPositionValues(PositionBoard.Row + 1, PositionBoard.Column + 2);
            if (Board.PositionIsValid(position) && youCanMove(position))
            {
                possibleMovimentsMatrix[position.Row, position.Column] = true;
            }

            // South-west
            position.SetPositionValues(PositionBoard.Row + 2, PositionBoard.Column + 1);
            if (Board.PositionIsValid(position) && youCanMove(position))
            {
                possibleMovimentsMatrix[position.Row, position.Column] = true;
            }

            // Left
            position.SetPositionValues(PositionBoard.Row + 2, PositionBoard.Column - 1);
            if (Board.PositionIsValid(position) && youCanMove(position))
            {
                possibleMovimentsMatrix[position.Row, position.Column] = true;
            }

            // Northwest
            position.SetPositionValues(PositionBoard.Row + 1, PositionBoard.Column - 2);
            if (Board.PositionIsValid(position) && youCanMove(position))
            {
                possibleMovimentsMatrix[position.Row, position.Column] = true;
            }
            return(possibleMovimentsMatrix);
        }
Ejemplo n.º 3
0
        public override bool[,] PossibleMoviments()
        {
            // It will save all possible moves for the king.
            // It can move all around him.
            bool[,] possibleMovimentsMatrix = new bool[Board.Rows, Board.Columns];

            PositionBoard position = new PositionBoard(0, 0);

            // Above
            position.SetPositionValues(PositionBoard.Row - 1, PositionBoard.Column);
            if (Board.PositionIsValid(position) && youCanMove(position))
            {
                possibleMovimentsMatrix[position.Row, position.Column] = true;
            }

            // NorthEast
            position.SetPositionValues(PositionBoard.Row - 1, PositionBoard.Column + 1);
            if (Board.PositionIsValid(position) && youCanMove(position))
            {
                possibleMovimentsMatrix[position.Row, position.Column] = true;
            }

            // Right
            position.SetPositionValues(PositionBoard.Row, PositionBoard.Column + 1);
            if (Board.PositionIsValid(position) && youCanMove(position))
            {
                possibleMovimentsMatrix[position.Row, position.Column] = true;
            }

            // Southeast
            position.SetPositionValues(PositionBoard.Row + 1, PositionBoard.Column + 1);
            if (Board.PositionIsValid(position) && youCanMove(position))
            {
                possibleMovimentsMatrix[position.Row, position.Column] = true;
            }

            // Below
            position.SetPositionValues(PositionBoard.Row + 1, PositionBoard.Column);
            if (Board.PositionIsValid(position) && youCanMove(position))
            {
                possibleMovimentsMatrix[position.Row, position.Column] = true;
            }

            // South-west
            position.SetPositionValues(PositionBoard.Row + 1, PositionBoard.Column - 1);
            if (Board.PositionIsValid(position) && youCanMove(position))
            {
                possibleMovimentsMatrix[position.Row, position.Column] = true;
            }

            // Left
            position.SetPositionValues(PositionBoard.Row, PositionBoard.Column - 1);
            if (Board.PositionIsValid(position) && youCanMove(position))
            {
                possibleMovimentsMatrix[position.Row, position.Column] = true;
            }

            // Northwest
            position.SetPositionValues(PositionBoard.Row - 1, PositionBoard.Column - 1);
            if (Board.PositionIsValid(position) && youCanMove(position))
            {
                possibleMovimentsMatrix[position.Row, position.Column] = true;
            }

            // #SPECIAL MOVE : Castling
            if (MovementCount == 0 && !match.MatchInCheck)
            {
                // Small castling
                // The king’s position must be three columns to the right relative to the rook
                PositionBoard rookPositionSmall = new PositionBoard(PositionBoard.Row, PositionBoard.Column + 3);
                if (testRookForCastling(rookPositionSmall))
                {
                    // The two houses between the king and the rook must be free
                    PositionBoard nextPosition  = new PositionBoard(PositionBoard.Row, PositionBoard.Column + 1);
                    PositionBoard nextPosition2 = new PositionBoard(PositionBoard.Row, PositionBoard.Column + 2);
                    // The king can move to the second free house (next to the rook)
                    if (Board.Piece(nextPosition) == null && Board.Piece(nextPosition2) == null)
                    {
                        possibleMovimentsMatrix[PositionBoard.Row, PositionBoard.Column + 2] = true;
                    }
                }

                // Big castling
                PositionBoard rookPositionBig = new PositionBoard(PositionBoard.Row, PositionBoard.Column - 4);
                if (testRookForCastling(rookPositionBig))
                {
                    // The three houses between the king and the rook must be free
                    PositionBoard nextPosition  = new PositionBoard(PositionBoard.Row, PositionBoard.Column - 1);
                    PositionBoard nextPosition2 = new PositionBoard(PositionBoard.Row, PositionBoard.Column - 2);
                    PositionBoard nextPosition3 = new PositionBoard(PositionBoard.Row, PositionBoard.Column - 2);

                    // The king can move to the second free house
                    if (Board.Piece(nextPosition) == null && Board.Piece(nextPosition2) == null && Board.Piece(nextPosition3) == null)
                    {
                        possibleMovimentsMatrix[PositionBoard.Row, PositionBoard.Column - 2] = true;
                    }
                }
            }

            return(possibleMovimentsMatrix);
        }
Ejemplo n.º 4
0
        public override bool[,] PossibleMoviments()
        {
            // It will save all possible moves for the king.
            // It can move all around him.
            bool[,] possibleMovimentsMatrix = new bool[Board.Rows, Board.Columns];

            PositionBoard position = new PositionBoard(0, 0);

            if (Color == Color.White)
            {
                // Can move up one house if the house is free.
                position.SetPositionValues(PositionBoard.Row - 1, PositionBoard.Column);
                if (Board.PositionIsValid(position) && free(position))
                {
                    possibleMovimentsMatrix[position.Row, position.Column] = true;
                }

                // Can move two houses up if the house is free and if it is the first move of the pawn.
                position.SetPositionValues(PositionBoard.Row - 2, PositionBoard.Column);
                if (Board.PositionIsValid(position) && free(position) && MovementCount == 0)
                {
                    possibleMovimentsMatrix[position.Row, position.Column] = true;
                }

                // Can walk on diagonals if there is an enemy.
                position.SetPositionValues(PositionBoard.Row - 1, PositionBoard.Column - 1);
                if (Board.PositionIsValid(position) && thereIsEnemy(position))
                {
                    possibleMovimentsMatrix[position.Row, position.Column] = true;
                }

                position.SetPositionValues(PositionBoard.Row - 1, PositionBoard.Column + 1);
                if (Board.PositionIsValid(position) && thereIsEnemy(position))
                {
                    possibleMovimentsMatrix[position.Row, position.Column] = true;
                }

                // #SPECIAL MOVES : En Passant
                // It only happens on this line for the white pieces
                if (PositionBoard.Row == 3)
                {   // left pawn position
                    PositionBoard leftPosition = new PositionBoard(PositionBoard.Row, PositionBoard.Column - 1);
                    if (Board.PositionIsValid(leftPosition) && thereIsEnemy(leftPosition) && Board.Piece(leftPosition) == match.VulnerablePieceEnPassant)
                    {
                        possibleMovimentsMatrix[leftPosition.Row - 1, leftPosition.Column] = true;
                    }

                    // Right pawn position
                    PositionBoard rightPosition = new PositionBoard(PositionBoard.Row, PositionBoard.Column + 1);
                    if (Board.PositionIsValid(rightPosition) && thereIsEnemy(rightPosition) && Board.Piece(rightPosition) == match.VulnerablePieceEnPassant)
                    {
                        possibleMovimentsMatrix[rightPosition.Row - 1, rightPosition.Column] = true;
                    }
                }
            }
            else
            {
                // Can move down one house if the house is free.
                position.SetPositionValues(PositionBoard.Row + 1, PositionBoard.Column);
                if (Board.PositionIsValid(position) && free(position))
                {
                    possibleMovimentsMatrix[position.Row, position.Column] = true;
                }

                // Can move two houses down if the house is free and if it is the first move of the pawn.
                position.SetPositionValues(PositionBoard.Row + 2, PositionBoard.Column);
                //PositionBoard secondPosition = new PositionBoard(position.Row + 1, position.Column);
                if (Board.PositionIsValid(position) && free(position) && MovementCount == 0)
                {
                    possibleMovimentsMatrix[position.Row, position.Column] = true;
                }

                // Can walk on diagonals if there is an enemy.
                position.SetPositionValues(PositionBoard.Row + 1, PositionBoard.Column - 1);
                if (Board.PositionIsValid(position) && thereIsEnemy(position))
                {
                    possibleMovimentsMatrix[position.Row, position.Column] = true;
                }

                position.SetPositionValues(PositionBoard.Row + 1, PositionBoard.Column + 1);
                if (Board.PositionIsValid(position) && thereIsEnemy(position))
                {
                    possibleMovimentsMatrix[position.Row, position.Column] = true;
                }

                // #SPECIAL MOVES : En Passant
                // It only happens on this line for the black pieces
                if (PositionBoard.Row == 4)
                {   // left pawn position
                    PositionBoard leftPosition = new PositionBoard(PositionBoard.Row, PositionBoard.Column - 1);
                    if (Board.PositionIsValid(leftPosition) && thereIsEnemy(leftPosition) && Board.Piece(leftPosition) == match.VulnerablePieceEnPassant)
                    {
                        possibleMovimentsMatrix[leftPosition.Row + 1, leftPosition.Column] = true;
                    }

                    // Right pawn position
                    PositionBoard rightPosition = new PositionBoard(PositionBoard.Row, PositionBoard.Column + 1);
                    if (Board.PositionIsValid(rightPosition) && thereIsEnemy(rightPosition) && Board.Piece(rightPosition) == match.VulnerablePieceEnPassant)
                    {
                        possibleMovimentsMatrix[rightPosition.Row + 1, rightPosition.Column] = true;
                    }
                }
            }
            return(possibleMovimentsMatrix);
        }