Esempio n. 1
0
        public Panel bestMove(BoardGen board, double alpha, double beta)
        {
            Panel  bestMove     = null;
            double bestVal      = 100000;
            bool   removed      = false;
            Piece  removedPiece = null;

            board.setPanelsInUse();
            allPossibleBMoves = board.getAllPossibleBMoves(board);
            allPossibleWMoves = board.getAllPossibleWMoves(board);
            possibleWPieces   = board.getPossibleWPieces();
            possibleBPieces   = board.getPossibleBPieces();

            for (var i = 0; i < allPossibleBMoves.Count(); i++)
            {
                Panel temp = possibleBPieces[i].getPanel();
                if (board.getPanelsInUse().Contains(allPossibleBMoves[i]))
                {
                    foreach (Piece y in board.getPieces().ToList())
                    {
                        if (y.getPanel() == allPossibleBMoves[i])
                        {
                            removedPiece = y;
                            removed      = true;
                            board.RemovePiece(y);
                            //take piece if we're taking a piece
                        }
                    }
                }
                possibleBPieces[i].setPanel(allPossibleBMoves[i]);
                board.setPanelsInUse();
                board.getAllPossibleWMoves(board);
                board.getPossibleWPieces();
                double tempVal = minimax(board, true, 1, board.getAllPossibleWMoves(board),
                                         board.getPossibleWPieces(), alpha, beta);
                //start a search on this move to see where it leads
                if (tempVal < bestVal)
                {
                    bestVal  = tempVal;
                    beta     = tempVal;
                    bestMove = allPossibleBMoves[i];
                    piece    = possibleBPieces[i];
                    //if this move leads to a good board value then tempVal will be small/more
                    //negative.If it is then we update bestVal, update beta and update the best
                    //move and the piece it moves to get there
                }
                possibleBPieces[i].setPanel(temp);
                if (removed)
                {
                    removed = false;
                    board.AddPiece(removedPiece);
                    //return the removed piece to its position
                }
                board.setPanelsInUse();
                //update board
            }
            return(bestMove);
            //returns the best move we found
        }
Esempio n. 2
0
 public override double getValue(BoardGen board)
 {
     for (int x = 0; x < 8; x++)
     {
         for (int y = 0; y < 8; y++)
         {
             if (board.getPanels()[x, y] == getPanel())
             {
                 double val = (getType().Substring(0, 1) == "W") ? PawnTable[y, x] : PawnTable[y, x];
                 return(val + 10);
             }
         }
     }
     return(0);
 }
Esempio n. 3
0
        public double getBoardValue(BoardGen board)
        {
            double boardValue = 0; //boardValue starts as zero

            foreach (Piece p in board.getPieces().ToList())
            {
                if (p.getType().Substring(0, 1) == "B")
                {
                    boardValue = boardValue - p.getValue(board);
                    //when there's a black piece the board value becomes smaller/more negative
                }
                else
                {
                    boardValue = boardValue + p.getValue(board);
                    //otherwise the board value becomes bigger
                }
            }
            return(boardValue);
        }
Esempio n. 4
0
        public List <Panel> getAllPossibleWMoves(BoardGen board)
        {
            List <Panel> allPossibleWMoves = new List <Panel>();

            possibleWPieces = new List <Piece>();
            foreach (Piece x in getPieces().ToList())
            {
                x.setMoves(board, false, false);
                if (x.getType().Substring(0, 1) == "W")
                {
                    foreach (Panel p in x.getMoves())
                    {
                        allPossibleWMoves.Add(p);
                        possibleWPieces.Add(x);
                    }
                }
            }
            return(allPossibleWMoves);
        }
Esempio n. 5
0
        public bool checkCheck(BoardGen board)
        {
            bool check = false;

            Piece piece = null;

            //Need to check all pieces on board because discovered check exists
            foreach (Piece y in board.getPieces())
            {
                List <Panel> possibleMoves = new List <Panel>();
                possibleMoves = y.getMoves();
                if (y.getMoves() != null)
                {
                    foreach (Panel x in possibleMoves)
                    {
                        foreach (Piece c in board.getPieces())
                        {
                            if (c.getPanel() == x)
                            {
                                piece = c;
                                if (piece.getType().Contains("King") &&
                                    (y.getType().Substring(0, 1) !=
                                     piece.getType().Substring(0, 1)))
                                {
                                    typeOfCheck = (piece.getType().Substring(0, 1)
                                                   == "W") ? "W" : "B";
                                    return(true);
                                }
                            }
                        }
                    }
                }
                //Checks if any piece has a possible move that contains "King",
                //if it does it is check
            }

            return(check);
        }
Esempio n. 6
0
        public override void setMoves(BoardGen board, bool checkUp, bool suicide)
        {
            possibleMoves = new List <Panel>();
            Panel tempPan = null;

            capturePossible = false;
            List <Panel> panelsRemove = new List <Panel>();

            /*A knight moves in a L shape pattern, any "L" shape (3 squares in a
             * straight line and then one to the left or right).*/

            foreach (Panel x in board.getPanels())
            {
                if ((getPanel().Location.X == x.Location.X + 40 ||
                     getPanel().Location.X == x.Location.X - 40) &&
                    (getPanel().Location.Y == x.Location.Y + 80 ||
                     getPanel().Location.Y == x.Location.Y - 80))
                {
                    if (board.getPanelsInUse().Contains(x))
                    {
                        foreach (Piece y in board.getPieces())
                        {
                            if (y.getPanel() == x)
                            {
                                if (getType().Substring(0, 1) !=
                                    y.getType().Substring(0, 1))
                                {
                                    possibleMoves.Add(x);
                                }
                            }
                        }
                    }
                    else
                    {
                        possibleMoves.Add(x);
                    }
                }
                if ((getPanel().Location.X == x.Location.X + 80 ||
                     getPanel().Location.X == x.Location.X - 80) &&
                    (getPanel().Location.Y == x.Location.Y + 40 ||
                     getPanel().Location.Y == x.Location.Y - 40))
                {
                    if (board.getPanelsInUse().Contains(x))
                    {
                        foreach (Piece y in board.getPieces())
                        {
                            if (y.getPanel() == x)
                            {
                                if (getType().Substring(0, 1) !=
                                    y.getType().Substring(0, 1))
                                {
                                    possibleMoves.Add(x);
                                }
                            }
                        }
                    }
                    else
                    {
                        possibleMoves.Add(x);
                    }
                }
            }

            foreach (Panel x in possibleMoves)
            {
                foreach (Piece p in board.getPieces())
                {
                    if (x == p.getPanel())
                    {
                        capturePossible = true;
                    }
                }
            }


            /*This part gets all the possible moves, no need to remove panels being
             * blocked as a knight can jump over pieces. The only thing that would
             * make it not possible is if a same coloured piece is on the panel, this
             * was easy enough to add to the if statements*/

            if (checkUp && !suicide) //if checking for check and this is not suicide chess (as chess doesn't exist in suicide chess)
            {
                Piece toRem   = null;
                bool  removed = false;
                tempPan = getPanel();
                foreach (Panel x in possibleMoves)
                {
                    if (board.getPanelsInUse().Contains(x))
                    {
                        foreach (Piece z in board.getPieces().ToList())
                        {
                            if (z.getPanel() == x)
                            {
                                toRem   = z;
                                removed = true;
                                board.RemovePiece(z);
                            }
                        }
                    }

                    setPanel(x);

                    board.setPanelsInUse();

                    foreach (Piece y in board.getPieces().ToList())
                    {
                        if (!y.getType().Contains(getType()))
                        {
                            y.setMoves(board, false, suicide);
                        }
                    }

                    if (checkCheck(board) && checkType() ==
                        getType().Substring(0, 1))
                    {
                        panelsRemove.Add(x);
                    }

                    setPanel(tempPan);

                    if (removed)
                    {
                        board.AddPiece(toRem);
                        removed = false;
                    }

                    board.setPanelsInUse();
                }

                foreach (Panel x in panelsRemove)
                {
                    possibleMoves.Remove(x);
                }
            }

            /* Exactly like all other pieces, this part checks all of the current
             * possible moves for the the bisop to see if any would leave the player
             * in check, if it would, it is not a valid move and is removed. */

            if (capturePossible && suicide) //if it is suicide chess and there is a capture possible
            {
                List <Panel> newPossibleMoves = new List <Panel>();
                foreach (Panel x in possibleMoves)
                {
                    foreach (Piece y in board.getPieces())
                    {
                        if (x == y.getPanel())
                        {
                            newPossibleMoves.Add(x);
                        }
                    }
                }
                possibleMoves = newPossibleMoves;
                //captures are the only possible moves if a capture is possible
            }
            else if (!capturePossible && suicide) //if there no captures possible in suicide chess
            {
                foreach (Piece x in board.getPieces())
                {
                    if (x.capture() && x.getType().Substring(0, 1) == getType().Substring(0, 1))
                    {
                        possibleMoves = new List <Panel>();
                        break;
                        //if there are no possible captures, but there are possible captures from other pieces, this piece cannot move
                    }
                }
            }
        }
Esempio n. 7
0
        public override void setMoves(BoardGen board, bool checkUp, bool suicide)
        {
            possibleMoves = new List <Panel>();
            List <Panel> panelsRemove = new List <Panel>();
            Piece        piece        = null;
            Panel        tempPan      = null;

            capturePossible = false;


            //A king can move 1 sqaure in any direction.



            foreach (Panel x in board.getPanels())
            {
                foreach (Piece c in board.getPieces())
                {
                    if (c.getPanel() == x)
                    {
                        piece = c;
                    }
                }
                if (x.Location.X == getPanel().Location.X)
                {
                    if (x.Location.Y == getPanel().Location.Y + 40 ||
                        x.Location.Y == getPanel().Location.Y - 40)
                    {
                        possibleMoves.Add(x);
                    }
                }
                if (x.Location.X == getPanel().Location.X + 40)
                {
                    if (x.Location.Y == getPanel().Location.Y + 40 ||
                        x.Location.Y == getPanel().Location.Y - 40 ||
                        x.Location.Y == getPanel().Location.Y)
                    {
                        possibleMoves.Add(x);
                    }
                }
                if (x.Location.X == getPanel().Location.X - 40)
                {
                    if (x.Location.Y == getPanel().Location.Y + 40 ||
                        x.Location.Y == getPanel().Location.Y - 40 ||
                        x.Location.Y == getPanel().Location.Y)
                    {
                        possibleMoves.Add(x);
                    }
                }

                /*Similarly to other pieces this section gets all moves that would
                 * be possible for the king on an empty board*/


                if (piece != null)
                {
                    if (piece.getType().Substring(0, 1) == getType().Substring(0, 1) &&
                        board.getPanelsInUse().Contains(x))
                    {
                        panelsRemove.Add(x);
                    }
                    else
                    {
                    }
                }

                /*And this just adds all the blocked sqaures to a list of not
                 * possible moves*/
            }

            foreach (Panel x in panelsRemove)
            {
                possibleMoves.Remove(x);
            }
            bool castle1 = false;
            bool castle2 = false;

            if (!getMoved())
            {
                if (!board.getPanelsInUse().Contains(board.getPanels()[5, getType().Substring(0, 1) == "W" ? 7 : 0]) && !board.getPanelsInUse().Contains(board.getPanels()[6, getType().Substring(0, 1) == "W" ? 7 : 0]))
                {
                    possibleMoves.Add(board.getPanels()[6, getType().Substring(0, 1) == "W" ? 7 : 0]);

                    castle1 = true;
                }

                if (!board.getPanelsInUse().Contains(board.getPanels()[1, getType().Substring(0, 1) == "W" ? 7 : 0]) && !board.getPanelsInUse().Contains(board.getPanels()[2, getType().Substring(0, 1) == "W" ? 7 : 0]) && !board.getPanelsInUse().Contains(board.getPanels()[3, getType().Substring(0, 1) == "W" ? 7 : 0]))
                {
                    possibleMoves.Add(board.getPanels()[2, getType().Substring(0, 1) == "W" ? 7 : 0]);

                    castle2 = true;
                }
            }

            if (checkUp && !suicide) //if checking for check and this is not suicide chess (as chess doesn't exist in suicide chess)
            {
                Piece toRem   = null;
                bool  removed = false;
                tempPan = getPanel();
                foreach (Panel x in possibleMoves)
                {
                    if (board.getPanelsInUse().Contains(x))
                    {
                        foreach (Piece z in board.getPieces().ToList())
                        {
                            if (z.getPanel() == x)
                            {
                                toRem   = z;
                                removed = true;
                                board.RemovePiece(z);
                            }
                        }
                    }
                    setPanel(x);
                    board.setPanelsInUse();

                    foreach (Piece y in board.getPieces().ToList())
                    {
                        if (!y.getType().Contains(getType()))
                        {
                            y.setMoves(board, false, suicide);
                        }
                    }

                    if (checkCheck(board))
                    {
                        if (castle1 && x == board.getPanels()[5, getType().Substring(0, 1) == "W" ? 7 : 0])
                        {
                            panelsRemove.Add(board.getPanels()[6, getType().Substring(0, 1) == "W" ? 7 : 0]);
                        }
                        if (castle2 && x == board.getPanels()[3, getType().Substring(0, 1) == "W" ? 7 : 0])
                        {
                            panelsRemove.Add(board.getPanels()[2, getType().Substring(0, 1) == "W" ? 7 : 0]);
                        }
                        panelsRemove.Add(x);
                    }

                    setPanel(tempPan);

                    if (removed)
                    {
                        board.AddPiece(toRem);
                        removed = false;
                    }

                    board.setPanelsInUse();
                }

                foreach (Panel x in panelsRemove)
                {
                    possibleMoves.Remove(x);
                }

                foreach (Panel x in possibleMoves)
                {
                    foreach (Piece p in board.getPieces())
                    {
                        if (x == p.getPanel())
                        {
                            capturePossible = true;
                        }
                    }
                }
            }

            /* Exactly like all other pieces, this part checks all of the current
             * possible moves for the queen to see if any would leave the player
             * in check, if it would, it is not a valid move and is removed. */

            if (capturePossible && suicide) //if it is suicide chess and there is a capture possible
            {
                List <Panel> newPossibleMoves = new List <Panel>();
                foreach (Panel x in possibleMoves)
                {
                    foreach (Piece y in board.getPieces())
                    {
                        if (x == y.getPanel())
                        {
                            newPossibleMoves.Add(x);
                        }
                    }
                }
                possibleMoves = newPossibleMoves;
                //captures are the only possible moves if a capture is possible
            }
            else if (!capturePossible && suicide) //if there no captures possible in suicide chess
            {
                foreach (Piece x in board.getPieces())
                {
                    if (x.capture() && x.getType().Substring(0, 1) == getType().Substring(0, 1))
                    {
                        possibleMoves = new List <Panel>();
                        break;
                        //if there are no possible captures, but there are possible captures from other pieces, this piece cannot move
                    }
                }
            }
        }
Esempio n. 8
0
        public double minimax(BoardGen board, bool max, int depth, List <Panel>
                              possibleMoves, List <Piece> possiblePieces, double alpha,
                              double beta)
        {
            double bestValue;
            bool   removed      = false;
            Piece  removedPiece = null;

            board.setPanelsInUse();

            if (depth == 0)
            {
                return(getBoardValue(board));
                //We have searched as far as we need to and can return what the board value
                //would be in this position
            }
            if (max)                                            //if the player is trying to maximise score
            {
                double bestVal = -100000;                       //set bestVal to some very large negative number so
                                                                //that it easily beaten

                for (var i = 0; i < possibleMoves.Count(); i++) //for every move possible from
                                                                //this position
                {
                    Panel temp = possiblePieces[i].getPanel();
                    if (board.getPanelsInUse().Contains(possibleMoves[i]))
                    {
                        foreach (Piece y in board.getPieces().ToList())
                        {
                            if (y.getPanel() == possibleMoves[i])
                            {
                                removedPiece = y;
                                removed      = true;
                                board.RemovePiece(y);
                                //take piece if we're taking a piece
                            }
                        }
                    }
                    possiblePieces[i].setPanel(possibleMoves[i]);
                    board.setPanelsInUse();
                    board.getAllPossibleWMoves(board);
                    board.getPossibleWPieces();
                    //update the board for one of the possible moves
                    double tempVal = minimax(board, !max, depth - 1,
                                             board.getAllPossibleBMoves(board),
                                             board.getPossibleBPieces(), alpha, beta);
                    //dive deeper into a search looking ahead by 1 furhter witht tempVal
                    if (tempVal > bestVal)
                    {
                        bestVal = tempVal;
                        alpha   = tempVal;

                        //if tempVal is better than the current bestVal then this is the new
                        //tempVal and alpha is set to tempVal
                    }
                    possiblePieces[i].setPanel(temp);
                    if (removed)
                    {
                        removed = false;
                        board.AddPiece(removedPiece);
                        //put taken piece back
                    }
                    board.setPanelsInUse();

                    if (alpha >= beta)
                    {
                        return(bestVal);
                        //If alpha is greater or equal to beta we can prune the search
                    }
                }
                bestValue = bestVal;
            }
            else
            {
                double bestVal = 100000; //We're minimising so some large value will be easily
                                         //beaten here
                for (var i = 0; i < possibleMoves.Count(); i++)
                {
                    Panel temp = possiblePieces[i].getPanel();
                    if (board.getPanelsInUse().Contains(possibleMoves[i]))
                    {
                        foreach (Piece y in board.getPieces().ToList())
                        {
                            if (y.getPanel() == possibleMoves[i])
                            {
                                removedPiece = y;
                                removed      = true;
                                board.RemovePiece(y);
                                //take piece if we're taking a piece
                            }
                        }
                    }
                    possiblePieces[i].setPanel(possibleMoves[i]);
                    board.setPanelsInUse();
                    board.getAllPossibleBMoves(board);
                    board.getPossibleBPieces();
                    //update the board to do this move
                    double tempVal = minimax(board, !max, depth - 1,
                                             board.getAllPossibleWMoves(board),
                                             board.getPossibleWPieces(), alpha, beta);
                    //go deeper to see if this move will lead anywhere good
                    beta = Math.Min(beta, tempVal);
                    if (tempVal < bestVal)
                    {
                        bestVal = tempVal;
                        beta    = tempVal;
                        //if this move leads somewhere good then tempVal will be less than bestVal
                        //as black tries to minimise, so this val is set to bestVal
                    }
                    possiblePieces[i].setPanel(temp);
                    if (removed)
                    {
                        removed = false;
                        board.AddPiece(removedPiece);
                        //return the piece that was taken
                    }
                    board.setPanelsInUse();
                    if (alpha >= beta)
                    {
                        return(bestVal);
                        //if alpha is greater than beta then we can prune the search a bit
                    }
                }
                bestValue = bestVal;
            }
            return(bestValue);
            //return the best value we have found
        }
Esempio n. 9
0
        public override void setMoves(BoardGen board, bool checkUp, bool suicide)
        {
            possibleMoves = new List <Panel>();
            Piece piece   = null;
            Panel tempPan = null;

            capturePossible = false;

            //A bishop may move along the diagnol it is on

            foreach (Panel x in board.getPanels())
            {
                for (int i = 0; i < 8; i++)
                {
                    foreach (Piece c in board.getPieces())
                    {
                        if (c.getPanel() == x)
                        {
                            piece = c;
                        }
                    }

                    if ((getPanel().Location.X == x.Location.X + (40 * i) ||
                         getPanel().Location.X == x.Location.X - (40 * i)) &&
                        (getPanel().Location.Y == x.Location.Y + (40 * i) ||
                         getPanel().Location.Y == x.Location.Y - (40 * i)))
                    {
                        if (board.getPanelsInUse().Contains(x))
                        {
                            foreach (Piece y in board.getPieces())
                            {
                                if (y.getPanel() == x)
                                {
                                    if (getType().Substring(0, 1) !=
                                        y.getType().Substring(0, 1))
                                    {
                                        possibleMoves.Add(x);
                                    }
                                }
                            }
                        }
                        else
                        {
                            possibleMoves.Add(x);
                        }
                    }
                }
            }

            /* This bit just gets all of the panels in a diagonal direction from the
             * bishop in use */

            List <Panel> panelsRemove = new List <Panel>();

            foreach (Panel x in board.getPanels())
            {
                for (int i = 0; i < 8; i++)
                {
                    if ((getPanel().Location.X == x.Location.X + (40 * i) ||
                         getPanel().Location.X == x.Location.X - (40 * i)) &&
                        (getPanel().Location.Y == x.Location.Y + (40 * i) ||
                         getPanel().Location.Y == x.Location.Y - (40 * i)) &&
                        board.getPanelsInUse().Contains(x))
                    {
                        if (x.Location.Y < getPanel().Location.Y&&
                            x.Location.X < getPanel().Location.X)
                        {
                            foreach (Panel g in possibleMoves)
                            {
                                if (g.Location.Y < x.Location.Y && g.Location.X <
                                    x.Location.X)
                                {
                                    panelsRemove.Add(g);
                                }
                            }
                        }
                        else if (x.Location.Y < getPanel().Location.Y&&
                                 x.Location.X > getPanel().Location.X)
                        {
                            foreach (Panel g in possibleMoves)
                            {
                                if (g.Location.Y < x.Location.Y && g.Location.X >
                                    x.Location.X)
                                {
                                    panelsRemove.Add(g);
                                }
                            }
                        }
                        else if (x.Location.Y > getPanel().Location.Y&&
                                 x.Location.X < getPanel().Location.X)
                        {
                            foreach (Panel g in possibleMoves)
                            {
                                if (g.Location.Y > x.Location.Y && g.Location.X <
                                    x.Location.X)
                                {
                                    panelsRemove.Add(g);
                                }
                            }
                        }
                        else if (x.Location.Y > getPanel().Location.Y&&
                                 x.Location.X > getPanel().Location.X)
                        {
                            foreach (Panel g in possibleMoves)
                            {
                                if (g.Location.Y > x.Location.Y &&
                                    g.Location.X > x.Location.X)
                                {
                                    panelsRemove.Add(g);
                                }
                            }
                        }
                    }
                }
            }

            /* This part adds all of the panels that are blocked in some way to an
             * array that removes all of those panels from possible moves (the next
             * part */

            foreach (Panel x in possibleMoves)
            {
                foreach (Piece p in board.getPieces())
                {
                    if (x == p.getPanel())
                    {
                        capturePossible = true;
                    }
                }
            }


            foreach (Panel x in panelsRemove)
            {
                possibleMoves.Remove(x);
            }

            if (checkUp && !suicide) //if checking for check and this is not suicide chess (as chess doesn't exist in suicide chess)
            {
                Piece toRem   = null;
                bool  removed = false;
                tempPan = getPanel();
                foreach (Panel x in possibleMoves)
                {
                    if (board.getPanelsInUse().Contains(x))
                    {
                        foreach (Piece z in board.getPieces().ToList())
                        {
                            if (z.getPanel() == x)
                            {
                                toRem   = z;
                                removed = true;
                                board.RemovePiece(z);
                            }
                        }
                    }

                    setPanel(x);

                    board.setPanelsInUse();

                    foreach (Piece y in board.getPieces().ToList())
                    {
                        if (!y.getType().Contains(getType()))
                        {
                            y.setMoves(board, false, suicide);
                        }
                    }

                    if (checkCheck(board) && checkType() ==
                        getType().Substring(0, 1))
                    {
                        panelsRemove.Add(x);
                    }

                    setPanel(tempPan);

                    if (removed)
                    {
                        board.AddPiece(toRem);
                        removed = false;
                    }

                    board.setPanelsInUse();
                }

                /* This part checks all of the current possible moves for the
                 * the bisop to see if any would leave the player in check, if it
                 * would, it is not a valid move and is removed. */

                foreach (Panel x in panelsRemove)
                {
                    possibleMoves.Remove(x);
                }
            }

            if (capturePossible && suicide) //if it is suicide chess and there is a capture possible
            {
                List <Panel> newPossibleMoves = new List <Panel>();
                foreach (Panel x in possibleMoves)
                {
                    foreach (Piece y in board.getPieces())
                    {
                        if (x == y.getPanel())
                        {
                            newPossibleMoves.Add(x);
                        }
                    }
                }
                possibleMoves = newPossibleMoves;
                //captures are the only possible moves if a capture is possible
            }
            else if (!capturePossible && suicide) //if there no captures possible in suicide chess
            {
                foreach (Piece x in board.getPieces())
                {
                    if (x.capture() && x.getType().Substring(0, 1) == getType().Substring(0, 1))
                    {
                        possibleMoves = new List <Panel>();
                        break;
                        //if there are no possible captures, but there are possible captures from other pieces, this piece cannot move
                    }
                }
            }
        }
Esempio n. 10
0
 public abstract double getValue(BoardGen board);
Esempio n. 11
0
 public abstract void setMoves(BoardGen board, bool checkUp, bool suicide);
Esempio n. 12
0
        public override void setMoves(BoardGen board, bool checkUp, bool suicide)
        {
            Piece piece   = null;
            Panel tempPan = null;

            possibleMoves = new List <Panel>();
            List <Panel> panelsRemove = new List <Panel>();

            capturePossible = false;
            bool noMoves = false;

            /*White's pawns can move up the board, Black's move down. If it is
             * the first move then a pawn can move two squares, otherwise only
             * one. Pawns also take other pieces diagonaly.*/

            foreach (Panel x in board.getPanels())
            {
                foreach (Piece c in board.getPieces())
                {
                    if (c.getPanel() == x)
                    {
                        piece = c;
                    }
                }
                if (piece != null)
                {
                    if (getType().Substring(0, 1).Equals("W"))
                    {
                        if (board.getPanelsInUse().Contains(x) &&
                            (x.Location.Y == getPanel().Location.Y - 40) &&
                            (x.Location.X == getPanel().Location.X))
                        {
                            noMoves = true;
                            foreach (Panel z in board.getPanels())
                            {
                                if ((((getPanel().Location.Y == z.Location.Y + 40) &&
                                      (z.Location.X == getPanel().Location.X - 40)) ||
                                     (z.Location.X == getPanel().Location.X + 40)))
                                {
                                    noMoves = false;
                                }
                            }
                        }
                        else if (!(board.getPanelsInUse().Contains(x)) &&
                                 (getPanel().Location.Y == 240) &&
                                 ((x.Location.Y == getPanel().Location.Y - 40) ||
                                  (x.Location.Y == getPanel().Location.Y - 80)) &&
                                 (x.Location.X == getPanel().Location.X))
                        {
                            possibleMoves.Add(x);
                        }
                        else if (!(board.getPanelsInUse().Contains(x)) &&
                                 (x.Location.Y == getPanel().Location.Y - 40) &&
                                 (x.Location.X == getPanel().Location.X))
                        {
                            possibleMoves.Add(x);
                        }
                        else if (board.getPanelsInUse().Contains(x) &&
                                 (getType().Substring(0, 1) !=
                                  piece.getType().Substring(0, 1)))
                        {
                            if ((getPanel().Location.Y == x.Location.Y + 40 &&
                                 (x.Location.X == getPanel().Location.X - 40 ||
                                  x.Location.X == getPanel().Location.X + 40)))
                            {
                                possibleMoves.Add(x);
                            }
                        }
                    }
                    else if (getType().Substring(0, 1).Equals("B"))
                    {
                        if (board.getPanelsInUse().Contains(x) &&
                            (x.Location.Y == getPanel().Location.Y + 40) &&
                            (x.Location.X == getPanel().Location.X))
                        {
                            noMoves = true;
                            foreach (Panel z in board.getPanels())
                            {
                                if ((((getPanel().Location.Y == z.Location.Y - 40) &&
                                      (z.Location.X == getPanel().Location.X - 40)) ||
                                     (z.Location.X == getPanel().Location.X + 40)))
                                {
                                    noMoves = false;
                                }
                            }
                        }
                        else if (!(board.getPanelsInUse().Contains(x)) &&
                                 (getPanel().Location.Y == 40) &&
                                 ((x.Location.Y == getPanel().Location.Y + 40) ||
                                  (x.Location.Y == getPanel().Location.Y + 80)) &&
                                 (x.Location.X == getPanel().Location.X))
                        {
                            possibleMoves.Add(x);
                        }
                        else if (!(board.getPanelsInUse().Contains(x)) &&
                                 (x.Location.Y == getPanel().Location.Y + 40) &&
                                 (x.Location.X == getPanel().Location.X))
                        {
                            possibleMoves.Add(x);
                        }
                        else if (board.getPanelsInUse().Contains(x) &&
                                 (getType().Substring(0, 1) !=
                                  piece.getType().Substring(0, 1)))
                        {
                            if ((getPanel().Location.Y == x.Location.Y - 40 &&
                                 (x.Location.X == getPanel().Location.X - 40 ||
                                  x.Location.X == getPanel().Location.X + 40)))
                            {
                                possibleMoves.Add(x);
                            }
                        }
                    }
                }
            }

            /*This section adds all the moves the pawn could make if the board were
             * empty to a list called possibleMoves and then all the ones that are
             * made invalid due to other pieces are added to the list called
             * panelsRemove*/

            foreach (Panel x in panelsRemove)
            {
                possibleMoves.Remove(x);
            }

            foreach (Panel x in possibleMoves)
            {
                foreach (Piece p in board.getPieces())
                {
                    if (x == p.getPanel())
                    {
                        capturePossible = true;
                    }
                }
            }


            if (checkUp && !suicide) //if checking for check and this is not suicide chess (as chess doesn't exist in suicide chess)
            {
                Piece toRem   = null;
                bool  removed = false;
                tempPan = getPanel();
                foreach (Panel x in possibleMoves)
                {
                    if (board.getPanelsInUse().Contains(x))
                    {
                        foreach (Piece z in board.getPieces().ToList())
                        {
                            if (z.getPanel() == x)
                            {
                                toRem   = z;
                                removed = true;
                                board.RemovePiece(z);
                            }
                        }
                    }

                    setPanel(x);

                    board.setPanelsInUse();

                    foreach (Piece y in board.getPieces().ToList())
                    {
                        if (!y.getType().Contains(getType()))
                        {
                            y.setMoves(board, false, suicide);
                        }
                    }

                    if (checkCheck(board) && checkType() == getType().Substring(0, 1))
                    {
                        panelsRemove.Add(x);
                    }

                    setPanel(tempPan);

                    if (removed)
                    {
                        board.AddPiece(toRem);
                        removed = false;
                    }

                    board.setPanelsInUse();
                }

                foreach (Panel x in panelsRemove)
                {
                    possibleMoves.Remove(x);
                }

                if (noMoves)
                {
                    possibleMoves = new List <Panel>();
                }
            }

            /* Exactly like all other pieces, this part checks all of the current
             * possible moves for the pawn to see if any would leave the player
             * in check, if it would, it is not a valid move and is removed. */

            if (capturePossible && suicide) //if it is suicide chess and there is a capture possible
            {
                List <Panel> newPossibleMoves = new List <Panel>();
                foreach (Panel x in possibleMoves)
                {
                    foreach (Piece y in board.getPieces())
                    {
                        if (x == y.getPanel())
                        {
                            newPossibleMoves.Add(x);
                        }
                    }
                }
                possibleMoves = newPossibleMoves;
                //captures are the only possible moves if a capture is possible
            }
            else if (!capturePossible && suicide) //if there no captures possible in suicide chess
            {
                foreach (Piece x in board.getPieces())
                {
                    if (x.capture() && x.getType().Substring(0, 1) == getType().Substring(0, 1))
                    {
                        possibleMoves = new List <Panel>();
                        break;
                        //if there are no possible captures, but there are possible captures from other pieces, this piece cannot move
                    }
                }
            }
        }
Esempio n. 13
0
        public override void setMoves(BoardGen board, bool checkUp, bool suicide)
        {
            possibleMoves   = new List <Panel>();
            capturePossible = false;
            //Rooks can move in a straigh line, up, down, left and right.
            foreach (Panel x in board.getPanels())
            {
                if ((getPanel().Location.Y == x.Location.Y ||
                     getPanel().Location.X == x.Location.X))
                {
                    if (board.getPanelsInUse().Contains(x))
                    {
                        foreach (Piece y in board.getPieces())
                        {
                            if (y.getPanel() == x)
                            {
                                if (getType().Substring(0, 1) !=
                                    y.getType().Substring(0, 1))
                                {
                                    possibleMoves.Add(x);
                                }
                            }
                        }
                    }
                    else
                    {
                        possibleMoves.Add(x);
                    }
                }
            }

            /*This part just gets all of the panels with the same x or y value as
             * the rook in use*/

            List <Panel> panelsRemove = new List <Panel>();

            foreach (Panel x in board.getPanels())
            {
                if (getPanel().Location.X == x.Location.X &&
                    board.getPanelsInUse().Contains(x))
                {
                    if (x.Location.Y > getPanel().Location.Y)
                    {
                        foreach (Panel g in possibleMoves)
                        {
                            if (g.Location.Y > x.Location.Y)
                            {
                                panelsRemove.Add(g);
                            }
                        }
                    }
                    else if (x.Location.Y < getPanel().Location.Y)
                    {
                        foreach (Panel g in possibleMoves)
                        {
                            if (g.Location.Y < x.Location.Y)
                            {
                                panelsRemove.Add(g);
                            }
                        }
                    }
                }
                if (getPanel().Location.Y == x.Location.Y &&
                    board.getPanelsInUse().Contains(x))
                {
                    if (x.Location.X > getPanel().Location.X)
                    {
                        foreach (Panel g in possibleMoves)
                        {
                            if (g.Location.X > x.Location.X)
                            {
                                panelsRemove.Add(g);
                            }
                        }
                    }
                    else if (x.Location.X < getPanel().Location.X)
                    {
                        foreach (Panel g in possibleMoves)
                        {
                            if (g.Location.X < x.Location.X)
                            {
                                panelsRemove.Add(g);
                            }
                        }
                    }
                }
            }

            /*This part adds any moves that aren't possible due to a piece blocking
             * the move to an array that is removed from possible moves*/


            foreach (Panel x in panelsRemove)
            {
                possibleMoves.Remove(x);
            }

            foreach (Panel x in possibleMoves)
            {
                foreach (Piece p in board.getPieces())
                {
                    if (x == p.getPanel())
                    {
                        capturePossible = true;
                    }
                }
            }

            if (checkUp && !suicide) //if checking for check and this is not suicide chess (as chess doesn't exist in suicide chess)
            {
                Piece toRem   = null;
                bool  removed = false;
                tempPan = getPanel();
                foreach (Panel x in possibleMoves)
                {
                    if (board.getPanelsInUse().Contains(x))
                    {
                        foreach (Piece z in board.getPieces().ToList())
                        {
                            if (z.getPanel() == x)
                            {
                                toRem   = z;
                                removed = true;
                                board.RemovePiece(z);
                            }
                        }
                    }

                    setPanel(x);

                    board.setPanelsInUse();

                    foreach (Piece y in board.getPieces().ToList())
                    {
                        if (!y.getType().Contains(getType()))
                        {
                            y.setMoves(board, false, suicide);
                        }
                    }

                    if (checkCheck(board) && checkType() ==
                        getType().Substring(0, 1))
                    {
                        panelsRemove.Add(x);
                    }

                    setPanel(tempPan);

                    if (removed)
                    {
                        board.AddPiece(toRem);
                        removed = false;
                    }

                    board.setPanelsInUse();
                }

                foreach (Panel x in panelsRemove)
                {
                    possibleMoves.Remove(x);
                }
            }

            /* Exactly like all other pieces, this part checks all of the current
             * possible moves for the the bisop to see if any would leave the player
             * in check, if it would, it is not a valid move and is removed. */

            if (capturePossible && suicide) //if it is suicide chess and there is a capture possible
            {
                List <Panel> newPossibleMoves = new List <Panel>();
                foreach (Panel x in possibleMoves)
                {
                    foreach (Piece y in board.getPieces())
                    {
                        if (x == y.getPanel())
                        {
                            newPossibleMoves.Add(x);
                        }
                    }
                }
                possibleMoves = newPossibleMoves;
                //captures are the only possible moves if a capture is possible
            }
            else if (!capturePossible && suicide) //if there no captures possible in suicide chess
            {
                foreach (Piece x in board.getPieces())
                {
                    if (x.capture() && x.getType().Substring(0, 1) == getType().Substring(0, 1))
                    {
                        possibleMoves = new List <Panel>();
                        break;
                        //if there are no possible captures, but there are possible captures from other pieces, this piece cannot move
                    }
                }
            }
        }
Esempio n. 14
0
        public override void setMoves(BoardGen board, bool checkUp, bool suicide)
        {
            possibleMoves = new List <Panel>();
            Panel tempPan = null;

            capturePossible = false;

            /*Queens move like a combination of a rook and bishop. Along a diagnol or
             * in a straight line*/

            foreach (Panel x in board.getPanels())
            {
                if ((getPanel().Location.Y == x.Location.Y ||
                     getPanel().Location.X == x.Location.X))
                {
                    if (board.getPanelsInUse().Contains(x))
                    {
                        foreach (Piece y in board.getPieces())
                        {
                            if (y.getPanel() == x)
                            {
                                if (getType().Substring(0, 1) !=
                                    y.getType().Substring(0, 1))
                                {
                                    possibleMoves.Add(x);
                                }
                            }
                        }
                    }
                    else
                    {
                        possibleMoves.Add(x);
                    }
                }
            }

            //Gets all the moves a queen could make if the board were empty

            List <Panel> panelsRemove = new List <Panel>();

            foreach (Panel x in board.getPanels())
            {
                if (getPanel().Location.X == x.Location.X &&
                    board.getPanelsInUse().Contains(x))
                {
                    if (x.Location.Y > getPanel().Location.Y)
                    {
                        foreach (Panel g in possibleMoves)
                        {
                            if (g.Location.Y > x.Location.Y)
                            {
                                panelsRemove.Add(g);
                            }
                        }
                    }
                    else if (x.Location.Y < getPanel().Location.Y)
                    {
                        foreach (Panel g in possibleMoves)
                        {
                            if (g.Location.Y < x.Location.Y)
                            {
                                panelsRemove.Add(g);
                            }
                        }
                    }
                }
                if (getPanel().Location.Y == x.Location.Y &&
                    board.getPanelsInUse().Contains(x))
                {
                    if (x.Location.X > getPanel().Location.X)
                    {
                        foreach (Panel g in possibleMoves)
                        {
                            if (g.Location.X > x.Location.X)
                            {
                                panelsRemove.Add(g);
                            }
                        }
                    }
                    else if (x.Location.X < getPanel().Location.X)
                    {
                        foreach (Panel g in possibleMoves)
                        {
                            if (g.Location.X < x.Location.X)
                            {
                                panelsRemove.Add(g);
                            }
                        }
                    }
                }
            }
            Piece piece = null;

            foreach (Panel x in board.getPanels())
            {
                for (int i = 0; i < 8; i++)
                {
                    foreach (Piece c in board.getPieces())
                    {
                        if (c.getPanel() == x)
                        {
                            piece = c;
                        }
                    }

                    if ((getPanel().Location.X == x.Location.X + (40 * i) ||
                         getPanel().Location.X == x.Location.X - (40 * i)) &&
                        (getPanel().Location.Y == x.Location.Y + (40 * i) ||
                         getPanel().Location.Y == x.Location.Y - (40 * i)))
                    {
                        if (board.getPanelsInUse().Contains(x))
                        {
                            foreach (Piece y in board.getPieces())
                            {
                                if (y.getPanel() == x)
                                {
                                    if (getType().Substring(0, 1) !=
                                        y.getType().Substring(0, 1))
                                    {
                                        possibleMoves.Add(x);
                                    }
                                }
                            }
                        }
                        else
                        {
                            possibleMoves.Add(x);
                        }
                    }
                }
            }

            foreach (Panel x in board.getPanels())
            {
                for (int i = 0; i < 8; i++)
                {
                    if ((getPanel().Location.X == x.Location.X + (40 * i) ||
                         getPanel().Location.X == x.Location.X - (40 * i)) &&
                        (getPanel().Location.Y == x.Location.Y + (40 * i) ||
                         getPanel().Location.Y == x.Location.Y - (40 * i)) &&
                        board.getPanelsInUse().Contains(x))
                    {
                        if (x.Location.Y < getPanel().Location.Y&&
                            x.Location.X < getPanel().Location.X)
                        {
                            foreach (Panel g in possibleMoves)
                            {
                                if (g.Location.Y < x.Location.Y &&
                                    g.Location.X < x.Location.X)
                                {
                                    panelsRemove.Add(g);
                                }
                            }
                        }
                        else if (x.Location.Y < getPanel().Location.Y&&
                                 x.Location.X > getPanel().Location.X)
                        {
                            foreach (Panel g in possibleMoves)
                            {
                                if (g.Location.Y < x.Location.Y &&
                                    g.Location.X > x.Location.X)
                                {
                                    panelsRemove.Add(g);
                                }
                            }
                        }
                        else if (x.Location.Y > getPanel().Location.Y&&
                                 x.Location.X < getPanel().Location.X)
                        {
                            foreach (Panel g in possibleMoves)
                            {
                                if (g.Location.Y > x.Location.Y &&
                                    g.Location.X < x.Location.X)
                                {
                                    panelsRemove.Add(g);
                                }
                            }
                        }
                        else if (x.Location.Y > getPanel().Location.Y&&
                                 x.Location.X > getPanel().Location.X)
                        {
                            foreach (Panel g in possibleMoves)
                            {
                                if (g.Location.Y > x.Location.Y &&
                                    g.Location.X > x.Location.X)
                                {
                                    panelsRemove.Add(g);
                                }
                            }
                        }
                    }
                }
            }

            foreach (Panel x in panelsRemove)
            {
                possibleMoves.Remove(x);
            }

            foreach (Panel x in possibleMoves)
            {
                foreach (Piece p in board.getPieces())
                {
                    if (x == p.getPanel())
                    {
                        capturePossible = true;
                    }
                }
            }


            //Removes any moves blocked by another piece.

            if (checkUp && !suicide) //if checking for check and this is not suicide chess (as chess doesn't exist in suicide chess)
            {
                Piece toRem   = null;
                bool  removed = false;
                tempPan = getPanel();
                foreach (Panel x in possibleMoves)
                {
                    if (board.getPanelsInUse().Contains(x))
                    {
                        foreach (Piece z in board.getPieces().ToList())
                        {
                            if (z.getPanel() == x)
                            {
                                toRem   = z;
                                removed = true;
                                board.RemovePiece(z);
                            }
                        }
                    }

                    setPanel(x);

                    board.setPanelsInUse();

                    foreach (Piece y in board.getPieces().ToList())
                    {
                        if (!y.getType().Contains(getType()))
                        {
                            y.setMoves(board, false, suicide);
                        }
                    }

                    if (checkCheck(board) && checkType() == getType().Substring(0, 1))
                    {
                        panelsRemove.Add(x);
                    }

                    setPanel(tempPan);

                    if (removed)
                    {
                        board.AddPiece(toRem);
                        removed = false;
                    }

                    board.setPanelsInUse();
                }

                foreach (Panel x in panelsRemove)
                {
                    possibleMoves.Remove(x);
                }
            }

            /* This part checks all of the current possible moves for the
             * the bisop to see if any would leave the player in check, if it
             * would, it is not a valid move and is removed. */

            if (capturePossible && suicide) //if it is suicide chess and there is a capture possible
            {
                List <Panel> newPossibleMoves = new List <Panel>();
                foreach (Panel x in possibleMoves)
                {
                    foreach (Piece y in board.getPieces())
                    {
                        if (x == y.getPanel())
                        {
                            newPossibleMoves.Add(x);
                        }
                    }
                }
                possibleMoves = newPossibleMoves;
                //captures are the only possible moves if a capture is possible
            }
            else if (!capturePossible && suicide) //if there no captures possible in suicide chess
            {
                foreach (Piece x in board.getPieces())
                {
                    if (x.capture() && x.getType().Substring(0, 1) == getType().Substring(0, 1))
                    {
                        possibleMoves = new List <Panel>();
                        break;
                        //if there are no possible captures, but there are possible captures from other pieces, this piece cannot move
                    }
                }
            }
        }