예제 #1
0
        //Method to Gather moves
        List <int[]> gatherMoves()
        {
            int[] currPos = { PosX, PosY };

            //Single dimensional movement is free (so long as space is not occupied)
            for (int i = 1; i <= 8 - PosX; i++)
            {
                PossibleMoves.Add(new int[] { PosX + i, PosY });
            }
            for (int i = 1; i <= 8 - PosY; i++)
            {
                PossibleMoves.Add(new int[] { PosX, PosY + i });
            }

            //As is negative movement
            for (int i = -1; i >= PosX - 8; i--)
            {
                PossibleMoves.Add(new int[] { PosX + i, PosY });
            }
            for (int i = -1; i >= PosY - 8; i--)
            {
                PossibleMoves.Add(new int[] { PosX, PosY + i });
            }


            //Check
            WriteLine("Rook Moves total: " + PossibleMoves.Count);

            //Send Moves
            return(PossibleMoves);
        }
예제 #2
0
        //Method to Gather moves
        List <int[]> gatherMoves()
        {
            int[] currPos = { PosX, PosY };
            int   a       = 0;
            int   b       = 0;

            //2 spaces in one dimension and 1 space in another are allowed - never 2 in both or 1 in both
            while (b < 3)
            {
                //trip integer
                int trip = 0;

                if (a == 0)
                {
                    //a=1 b=2 First
                    a = 1;
                    b = 2;
                }
                else if (a == 1 || b == 2)
                {
                    //If this is 4th interval - set trip
                    if (a == -1)
                    {
                        trip = 1;
                    }

                    //a=-1 b=-2 Second OR a=1 b=-2 Fourth
                    a = -a;
                    b = -b;
                }
                else
                {
                    //a=-1 b=2 Third
                    b = -b;
                }

                //Adds values if they are within the board parameters
                int Xa = PosX + a;
                int Yb = PosY + b;
                if (Xa < 8 && Xa > 0 && Yb < 8 && Yb > 0)
                {
                    PossibleMoves.Add(new int[] { Xa, Yb });
                }

                int Xb = PosX + b;
                int Ya = PosY + a;
                if (Xb < 8 && Xb > 0 && Ya < 8 && Ya > 0)
                {
                    PossibleMoves.Add(new int[] { Xb, Ya });
                }

                b = b + trip;
            }

            //Check
            WriteLine("Rook Moves total: " + PossibleMoves.Count);

            //Send Moves
            return(PossibleMoves);
        }
예제 #3
0
 public void ShowPossibleMoves(Piece piece)
 {
     foreach (var move in CalcPossibleMoves(piece))
     {
         move.End.IsPossibileSquare = true;
         PossibleMoves.Add(move);
     }
 }
예제 #4
0
파일: Rook.cs 프로젝트: mwillia95/Projects
    public override void findPossibleMoves() //can move along file or rank, as long as it is not blocked
    {
        PossibleMoves.Clear();
        Coordinate   pTmp = new Coordinate(Position);
        string       color;
        Action <int> checkFile = (direction) =>
        {
            pTmp = new Coordinate(Position);
            for (; pTmp.changeFile(direction);)
            {
                color = board.CheckColor(pTmp);
                if (color == "none") //no piece, continue normally
                {
                    PossibleMoves.Add(pTmp);
                }
                else if (color != Color) //opposite piece, add its position and stop
                {
                    PossibleMoves.Add(pTmp);
                    break;
                }
                else //same color piece, don't add and stop
                {
                    break;
                }
                pTmp = new Coordinate(pTmp);
            }
            pTmp = new Coordinate(Position); //new coordinate object, resets position as well
        };
        Action <int> checkRank = (direction) =>
        {
            pTmp = new Coordinate(Position);
            for (; pTmp.changeRank(direction);)
            {
                color = board.CheckColor(pTmp);
                if (color == Color)
                {
                    break;
                }
                else if (color == "none")
                {
                    PossibleMoves.Add(pTmp);
                    pTmp = new Coordinate(pTmp);
                }
                else //opposite piece, add its position and stop
                { //bug occurs somewhere here, goes one position further than it should, or perhaps even past
                    PossibleMoves.Add(pTmp);
                    Console.WriteLine("Ocuured attempting position " + pTmp);
                    break;
                }
            }
            pTmp = new Coordinate(Position); //new coordinate object, resets position as well
        };

        checkFile(1);  //check right
        checkFile(-1); //check left
        checkRank(1);  //check up
        checkRank(-1); //check down
    }
예제 #5
0
파일: Pawn.cs 프로젝트: mwillia95/Projects
    public override void findPossibleMoves() //move forward one, direction dependent on color. can also capture diagonally
    {
        PossibleMoves.Clear();
        int direction = 0;

        if (Color == "white")
        {
            direction = 1;
        }
        else if (Color == "black")
        {
            direction = -1;
        }
        Coordinate mPosition = new Coordinate(Position); //creates a duplicate of pawn's position

        mPosition.changeRank(direction);                 //to check one in front of the pawn

        string front = board.CheckColor(mPosition);

        if (front == "none") //has opposite colored piece or no piece
        {
            PossibleMoves.Add(mPosition);
            if (hasMoved == false) //can move two spaces up if it has not yet moved and there is no piece already in front of it
            {
                mPosition = new Coordinate(mPosition);
                mPosition.changeRank(direction);
                if (board.CheckColor(mPosition) == "none")
                {
                    PossibleMoves.Add(mPosition);
                }
            }
        }
        mPosition = new Coordinate(Position);


        mPosition.changeRank(direction);
        if (mPosition.changeFile(1))        //checks in front and to the right of the pawn
        {
            if (isOppositeColor(mPosition)) //must have an opposite colored piece
            {
                PossibleMoves.Add(mPosition);
            }
        }

        mPosition = new Coordinate(Position);
        mPosition.changeRank(direction);
        if (mPosition.changeFile(-1)) //checks in front and to the left of the pawn
        {
            if (isOppositeColor(mPosition))
            {
                PossibleMoves.Add(mPosition);
            }
        }
    }
예제 #6
0
파일: Piece.cs 프로젝트: Robertk92/ChessAI
        private void CheckAndSetPossibleMoves(MoveDirection direction, int stepsX, int stepsY, bool inBetweenSteps, int iterationX = 1, int iterationY = 1)
        {
            Board board = Scene.GetObject <Board>();

            if (board == null)
            {
                throw new NullReferenceException("No board found");
            }

            if (!inBetweenSteps)
            {
                iterationX = stepsX;
                iterationY = stepsY;
            }

            int side = ControllingUnit == ControllingUnit.Human ? -1 : 1;

            int left  = Convert.ToInt32(((direction & MoveDirection.Left) == MoveDirection.Left)) * iterationX;
            int right = Convert.ToInt32(((direction & MoveDirection.Right) == MoveDirection.Right)) * iterationX * -1;
            int up    = Convert.ToInt32(((direction & MoveDirection.Up) == MoveDirection.Up)) * iterationY * side;
            int down  = Convert.ToInt32(((direction & MoveDirection.Down) == MoveDirection.Down)) * iterationY * side * -1;

            int  x          = (CurrentNode.Xindex + left + right);
            int  y          = (CurrentNode.Yindex + up + down);
            bool markForAdd = false;

            if (board.Nodes.IsInBounds(x, y))
            {
                Node newNode = board.Nodes[x, y];
                if (newNode.IsFree)
                {
                    markForAdd = true;
                }
                else
                {
                    if (newNode.Piece.ControllingUnit != ControllingUnit)
                    {
                        markForAdd = true;
                    }
                }
            }

            if (markForAdd)
            {
                PossibleMoves.Add(board.Nodes[x, y]);
                if ((iterationX < stepsX || stepsX == 0) && (iterationY < stepsY || stepsY == 0))
                {
                    CheckAndSetPossibleMoves(direction, stepsX, stepsY, inBetweenSteps, ++iterationX, ++iterationY);
                }
            }
        }
예제 #7
0
        public void RecursiveMovesGet(Board board, int position, int direction)
        {
            int pieceColor = 0;

            if (Color == "white")
            {
                pieceColor = 1;
            }
            else
            {
                pieceColor = -1;
            }
            //first handle out of bounds
            if (!(board.OutOfBoundsArea.ToList().IndexOf(position + direction * pieceColor * -1) != -1))
            {
                //now that we know not out of bounds let's try each way the piece moves over and over until we are either out of bounds or blocked or a capture is possible
                //Are we blocked?
                if (board.Pieces[position + direction * pieceColor * -1] == null)
                {
                    //okay we're not blocked
                    if (isLegalMove((direction * pieceColor * -1) + position, board, pieceColor))
                    {
                        PossibleMoves.Add((direction * pieceColor * -1) + position);
                        RecursiveMovesGet(board, -1 * pieceColor * (direction) + position, (direction));
                    }
                    else
                    {
                        return;
                    }
                }
                else if (board.Pieces[position + direction * pieceColor * -1] != null)
                {
                    //okay we're blocked, but is the block an enemy piece?
                    if (board.Pieces[position + direction * pieceColor * -1].Color != Color && board.Pieces[position + direction * pieceColor * -1].Color != null)
                    {
                        if (isLegalMove((direction * pieceColor * -1) + position, board, pieceColor))
                        {
                            PossibleMoves.Add((direction * pieceColor * -1) + position);
                        }
                    }
                    return;
                }
            }
            else
            {
                return;
            }

            //return new List<int>();
        }
예제 #8
0
        //Method to Gather moves
        List <int[]> gatherMoves()
        {
            int[] currPos = { PosX, PosY };

            //If first move, Pawn can move X + 2 and X + 1
            if (currPos == InitialPos)
            {
                PossibleMoves.Add(new int[] { (PosX + 2), PosY });
                PossibleMoves.Add(new int[] { (PosX + 1), PosY });
            }
            else
            {
                PossibleMoves.Add(new int[] { (PosX + 1), PosY });
            }

            //If enemy is at X + 1 and Y + 1
            //We'll do that later

            WriteLine("Pawn Moves total: " + PossibleMoves.Count);

            //Send Moves
            return(PossibleMoves);
        }
예제 #9
0
    public override void findPossibleMoves() //bishop can move along any diagonal as long as it is not blocked
    {
        PossibleMoves.Clear();
        Coordinate        mPosition = new Coordinate(Position);
        string            color;
        Action <int, int> checkDiagonal = (x, y) =>
        {
            for (; mPosition.changeDiagonal(x, y);)
            {
                color = board.CheckColor(mPosition);
                if (color == "none")
                {
                    //if (!IntoCheck(mPosition))
                    PossibleMoves.Add(mPosition);
                    mPosition = new Coordinate(mPosition);
                }

                else if (color != Color)
                {
                    //if (!IntoCheck(mPosition))
                    PossibleMoves.Add(mPosition);
                    break;
                }
                else
                {
                    break;
                }
            }
            mPosition = new Coordinate(Position);
        };

        checkDiagonal(1, 1);   //up right
        checkDiagonal(1, -1);  //up left
        checkDiagonal(-1, 1);  //down right
        checkDiagonal(-1, -1); //down left
    }
예제 #10
0
    public override void findPossibleMoves() //can move in L shapes, can also move over pieces
    {
        PossibleMoves.Clear();
        Coordinate mPosition = new Coordinate(Position);

        if (mPosition.changeDiagonal(2, 1))
        {
            if (!isSameColor(mPosition))
            {
                PossibleMoves.Add(mPosition);
            }
        }

        mPosition = new Coordinate(Position);
        if (mPosition.changeDiagonal(1, 2))
        {
            if (!isSameColor(mPosition))
            {
                PossibleMoves.Add(mPosition);
            }
        }

        mPosition = new Coordinate(Position);
        if (mPosition.changeDiagonal(-2, 1))
        {
            if (!isSameColor(mPosition))
            {
                PossibleMoves.Add(mPosition);
            }
        }

        mPosition = new Coordinate(Position);
        if (mPosition.changeDiagonal(-1, 2))
        {
            if (!isSameColor(mPosition))
            {
                PossibleMoves.Add(mPosition);
            }
        }

        mPosition = new Coordinate(Position);
        if (mPosition.changeDiagonal(2, -1))
        {
            if (!isSameColor(mPosition))
            {
                PossibleMoves.Add(mPosition);
            }
        }

        mPosition = new Coordinate(Position);
        if (mPosition.changeDiagonal(1, -2))
        {
            if (!isSameColor(mPosition))
            {
                PossibleMoves.Add(mPosition);
            }
        }

        mPosition = new Coordinate(Position);
        if (mPosition.changeDiagonal(-2, -1))
        {
            if (!isSameColor(mPosition))
            {
                PossibleMoves.Add(mPosition);
            }
        }

        mPosition = new Coordinate(Position);
        if (mPosition.changeDiagonal(-1, -2))
        {
            if (!isSameColor(mPosition))
            {
                PossibleMoves.Add(mPosition);
            }
        }
    }
예제 #11
0
파일: Queen.cs 프로젝트: mwillia95/Projects
    public override void findPossibleMoves() //combines the moves of a rook and bishop, along files, ranks, and diagonals
    {
        PossibleMoves.Clear();
        Coordinate        mPosition = new Coordinate(Position);
        string            color;
        Action <int, int> checkDiagonal = (x, y) =>
        {
            for (; mPosition.changeDiagonal(x, y);)
            {
                color = board.CheckColor(mPosition);
                if (color == "none")
                {
                    PossibleMoves.Add(mPosition);
                    mPosition = new Coordinate(mPosition);
                }

                else if (color != Color)
                {
                    // if (!IntoCheck(mPosition))
                    PossibleMoves.Add(mPosition);
                    break;
                }
                else
                {
                    break;
                }
            }
            mPosition = new Coordinate(Position);
        };
        Action <int> checkFile = (direction) =>
        {
            mPosition = new Coordinate(Position);
            for (; mPosition.changeFile(direction);)
            {
                color = board.CheckColor(mPosition);
                if (color == "none") //no piece, continue normally
                {
                    PossibleMoves.Add(mPosition);
                }
                else if (color != Color) //opposite piece, add its position and stop
                {
                    PossibleMoves.Add(mPosition);
                    break;
                }
                else //same color piece, don't add and stop
                {
                    break;
                }
                mPosition = new Coordinate(mPosition);
            }
            mPosition = new Coordinate(Position); //new coordinate object, resets position as well
        };
        Action <int> checkRank = (direction) =>
        {
            mPosition = new Coordinate(Position);
            for (; mPosition.changeRank(direction);)
            {
                color = board.CheckColor(mPosition);
                if (color == Color)
                {
                    break;
                }
                else if (color == "none")
                {
                    PossibleMoves.Add(mPosition);
                    mPosition = new Coordinate(mPosition);
                }
                else //opposite piece, add its position and stop
                { //bug occurs somewhere here, goes one position further than it should, or perhaps even past
                    PossibleMoves.Add(mPosition);
                    break;
                }
            }
            mPosition = new Coordinate(Position); //new coordinate object, resets position as well
        };

        checkFile(1);          //check right
        checkFile(-1);         //check left
        checkRank(1);          //check up
        checkRank(-1);         //check down

        checkDiagonal(1, 1);   //up right
        checkDiagonal(1, -1);  //up left
        checkDiagonal(-1, 1);  //down right
        checkDiagonal(-1, -1); //down left
    }
예제 #12
0
파일: King.cs 프로젝트: mwillia95/Projects
    public override void findPossibleMoves() //king can move in any direction by one square
    {
        PossibleMoves.Clear();
        //check up
        Coordinate mPosition = new Coordinate(Position);

        if (mPosition.changeRank(1))
        {
            if (!isSameColor(mPosition))
            {
                PossibleMoves.Add(mPosition);
            }
        }
        //check down
        mPosition = new Coordinate(Position);
        if (mPosition.changeRank(-1))
        {
            if (!isSameColor(mPosition))
            {
                PossibleMoves.Add(mPosition);
            }
        }
        //check right
        mPosition = new Coordinate(Position);
        if (mPosition.changeFile(1))
        {
            if (board.CheckColor(mPosition) == "none")
            {
                PossibleMoves.Add(mPosition);
            }
        }

        //check left
        mPosition = new Coordinate(Position);
        if (mPosition.changeFile(-1))
        {
            if (!isSameColor(mPosition))
            {
                PossibleMoves.Add(mPosition);
            }
        }
        //check up right
        mPosition = new Coordinate(Position);
        if (mPosition.changeDiagonal(1, 1))
        {
            if (!isSameColor(mPosition))
            {
                PossibleMoves.Add(mPosition);
            }
        }
        //check up left
        mPosition = new Coordinate(Position);
        if (mPosition.changeDiagonal(1, -1))
        {
            if (!isSameColor(mPosition))
            {
                PossibleMoves.Add(mPosition);
            }
        }
        //check down right
        mPosition = new Coordinate(Position);
        if (mPosition.changeDiagonal(-1, 1))
        {
            if (!isSameColor(mPosition))
            {
                PossibleMoves.Add(mPosition);
            }
        }
        //check down left
        mPosition = new Coordinate(Position);
        if (mPosition.changeDiagonal(-1, -1))
        {
            if (!isSameColor(mPosition))
            {
                PossibleMoves.Add(mPosition);
            }
        }
    }