Exemplo n.º 1
0
        private void button4_Click(object sender, EventArgs e)
        {
            bool taken = false;

            try
            {
                for (int i = 0; i < undos[undos.Count - 1]; i++)
                {
                    movingPiece = undoPiece[undoPiece.Count - (i + 1)];
                    if (!generation.getPieces().Contains(movingPiece))
                    {
                        movingPiece.getPanel().BackgroundImage = movingPiece.getType().Contains("W") ? pictureBoxesB[bPiecesTaken - 1].BackgroundImage : pictureBoxesW[wPiecesTaken - 1].BackgroundImage;
                        generation.AddPiece(movingPiece);
                        if (movingPiece.getType().Contains("W"))
                        {
                            pictureBoxesB[bPiecesTaken - 1].BackgroundImage = null;
                            bPiecesTaken--;
                        }
                        else
                        {
                            pictureBoxesB[wPiecesTaken - 1].BackgroundImage = null;
                            wPiecesTaken--;
                        }
                        //If there was a piece taken before the undo, remove it from the GUI
                        taken = true;
                    }
                    if (movingPiece == updatePiece)
                    {
                        piecePosRemove.getPanel().BackgroundImage = null;
                        piecePosRemove.setPanel(null);
                        generation.RemovePiece(piecePosRemove);
                        toUpdate    = null;
                        updatePiece = null;
                        movingPiece.getPanel().BackgroundImage = movingPiece.getType().Contains("W") ? Resources.WPawn : Resources.BPawn;
                        //Prepare the sqaure the piece needs to return to if necesary
                    }

                    movingPiecePanel            = undoPiece[undoPiece.Count - (i + 1)].getPanel();
                    actionPanel                 = undoPanel[undoPanel.Count - (i + 1)];
                    actionPanel.BackgroundImage = movingPiece.getPanel().BackgroundImage;

                    movingPiece.setPanel(actionPanel);
                    generation.setPanelsInUse();
                    movingPiecePanel.BackgroundImage = null;
                    movingPiece.decreaseNoMoves();
                    if (movingPiece.numberMoves() <= 0)
                    {
                        movingPiece.setMoved(false);
                    }
                    //return everything to the way it was before the move occured
                }
                for (int x = 0; x < undos[undos.Count - 1]; x++)
                {
                    undoPiece.Remove(undoPiece[undoPiece.Count - 1]);
                    undoPanel.Remove(undoPanel[undoPanel.Count - 1]);
                }

                undos.Remove(undos[undos.Count - 1]);

                if (updateReq || taken)
                {
                    panel_MouseDown(null, new MouseEventArgs(MouseButtons.Left, 1, 0, 0, 0));
                    updateReq = false;
                }
                //update the undos so that it wont be the same undo over and over again
            }
            catch (System.ArgumentOutOfRangeException)
            {
                MessageBox.Show("You can't go back any further.");
                //if there aren't any undos in the list, you can't go back more
            }
            noUndos = 0;
        }
Exemplo n.º 2
0
        private void panel_MouseDown(object sender, MouseEventArgs e)
        {
            Panel actionPanel = (Panel)sender;
            bool  castle      = false;

            //This section is for when no piece has been clicked yet
            if (timer1.Enabled) //if the game hasn't ended
            {
                if (movingPiece == null)
                {
                    pieces = generation.getPieces();

                    //Gets the piece that was clicked
                    foreach (Piece p in pieces)
                    {
                        if (p.getPanel() == actionPanel)
                        {
                            movingPiece      = p;
                            movingPiecePanel = actionPanel;
                            break;
                        }
                    }

                    //If the panel that was clicked actually contains a piece
                    try
                    {
                        //And it's that peice's colours turn
                        if ((WhiteMove &&
                             movingPiece.getType().Substring(0, 1).Equals("W")) ||
                            (!WhiteMove &&
                             movingPiece.getType().Substring(0, 1).Equals("B")))
                        {
                            movingPiece.setMoves(generation, true, suicide);
                            //Get that piece's moves

                            foreach (Panel x in movingPiece.getMoves())
                            {
                                x.BackColor = ColorTranslator.FromHtml("#FFFC7F");
                                //And display them
                            }
                        }
                    }
                    catch (NullReferenceException)
                    {
                        //Could notify the player they clicked an empty sqaure here
                    }
                }

                //This section is for when a piece has already been clicked
                else
                {
                    movingPiece.setMoves(generation, true, suicide);
                    //Update the moves of that piece for their new position

                    //Get the colours of the board back to normal
                    for (int k = 0; k <= 7; k++)
                    {
                        for (int s = 0; s <= 7; s++)
                        {
                            if (k % 2 == 0)
                            {
                                generation.getPanels()[k, s].BackColor =
                                    s % 2 != 0 ? ColorTranslator.FromHtml("#80A83E") : ColorTranslator.FromHtml("#D9DD76");
                            }
                            else
                            {
                                generation.getPanels()[k, s].BackColor =
                                    s % 2 != 0 ? ColorTranslator.FromHtml("#D9DD76") : ColorTranslator.FromHtml("#80A83E");
                            }
                        }
                    }

                    //If that panel clicked was not the same as the first panel the person clicked
                    if (actionPanel != movingPiecePanel && movingPiece.getType().Substring(0, 1) == (WhiteMove ? "W" : "B"))
                    {
                        //And the panel is valid
                        try
                        {
                            if (movingPiece.getMoves().Contains(actionPanel))
                            {
                                if (generation.getPanelsInUse().Contains(actionPanel))
                                {
                                    foreach (Piece z in generation.getPieces().ToList())
                                    {
                                        if (z.getPanel() == actionPanel)
                                        {
                                            generation.RemovePiece(z);
                                            if (z.getType().Substring(0, 1) == "W")
                                            {
                                                pictureBoxesB[bPiecesTaken].BackgroundImage = z.getPanel().BackgroundImage;
                                                bPiecesTaken++;
                                            }
                                            else
                                            {
                                                pictureBoxesW[wPiecesTaken].BackgroundImage = z.getPanel().BackgroundImage;
                                                wPiecesTaken++;
                                            }
                                        }
                                    }
                                    actionPanel.BackgroundImage = null;

                                    /*If the panel was a possible move and contains a
                                     * piece, the piece that it already contains needs
                                     * to be disposed of and stored in the interface where
                                     * taken pieces are displayed*/
                                }
                                if (movingPiece.getType().Contains("King"))
                                {
                                    if ((actionPanel == generation.getPanels()[6, movingPiece.getType().Substring(0, 1) == "W" ? 7 : 0] || actionPanel == generation.getPanels()[2, movingPiece.getType().Substring(0, 1) == "W" ? 7 : 0]) && !movingPiece.getMoved())
                                    {
                                        castle = true;
                                    }
                                }
                                //Recognises when the king is trying to castle


                                if ((WhiteMove &&
                                     movingPiece.getType().Substring(0, 1).Equals("W")) ||
                                    (!WhiteMove &&
                                     movingPiece.getType().Substring(0, 1).Equals("B")))
                                {
                                    actionPanel.BackgroundImage =
                                        movingPiece.getPanel().BackgroundImage;

                                    undoPiece.Add(movingPiece);
                                    undoPanel.Add(movingPiece.getPanel());
                                    noUndos++;

                                    movingPiece.setPanel(actionPanel);

                                    if (updateReq && movingPiece.getPanel() != updatePiece.getPanel())
                                    {
                                        updatePiece.setPanel(toUpdate);
                                        generation.RemovePiece(piecePosRemove);
                                        updateReq = false;
                                        //if a pawn has moved foward two last move, it can no longer be taken as if it were on the first sqaure, so don't allow that to happen.
                                    }
                                    else if (updateReq)
                                    {
                                        if (movingPiece.getType().Substring(0, 1) == "W")
                                        {
                                            pictureBoxesW[wPiecesTaken - 1].BackgroundImage = Resources.BPawn;
                                        }
                                        else
                                        {
                                            pictureBoxesB[bPiecesTaken - 1].BackgroundImage = Resources.WPawn;
                                        }
                                        updatePiece.getPanel().BackgroundImage    = movingPiece.getPanel().BackgroundImage;
                                        piecePosRemove.getPanel().BackgroundImage = null;
                                        generation.RemovePiece(piecePosRemove);
                                        updatePiece.setPanel(null);

                                        updateReq = false;

                                        //If a pawn has moved foward two last move, and a pawn has taken it as if it were on the first square, this allows it to be taken.
                                    }
                                    if (movingPiece.getType().Contains("Pawn"))
                                    {
                                        if (!movingPiece.getMoved() && movingPiece.getPanel().Location.Y == movingPiecePanel.Location.Y + ((movingPiece.getType().Contains("W")) ? -80 : 80))
                                        {
                                            foreach (Panel x in generation.getPanels())
                                            {
                                                if (x.Location.Y + (movingPiece.getType().Contains("W") ? -40 : 40) == actionPanel.Location.Y && x.Location.X == actionPanel.Location.X)
                                                {
                                                    toUpdate    = actionPanel;
                                                    updatePiece = movingPiece;
                                                    movingPiece.setPanel(x);
                                                    updateReq      = true;
                                                    piecePosRemove = new Pawn(updatePiece.getType(), actionPanel, true);
                                                    generation.AddPiece(piecePosRemove);
                                                    break;
                                                }
                                            }
                                        }
                                    }

                                    generation.setPanelsInUse();
                                    movingPiecePanel.BackgroundImage = null;
                                    movingPiece.setMoved(true);
                                    movingPiece.increaseNoMoves();

                                    //As long as the right person (whose turn it is) is
                                    //moving then this bit above moves it

                                    if (castle) //if the king is attempting to castle
                                    {
                                        int action = -1;
                                        if (actionPanel == generation.getPanels()[6, movingPiece.getType().Substring(0, 1) == "W" ? 7 : 0])
                                        {
                                            foreach (Piece x in generation.getPieces())
                                            {
                                                if (x.getPanel() == generation.getPanels()[7, movingPiece.getType().Substring(0, 1) == "W" ? 7 : 0])
                                                {
                                                    movingPiece = x;
                                                    action      = 5;
                                                }
                                            }
                                        }
                                        if (actionPanel == generation.getPanels()[2, movingPiece.getType().Substring(0, 1) == "W" ? 7 : 0])
                                        {
                                            foreach (Piece x in generation.getPieces())
                                            {
                                                if (x.getPanel() == generation.getPanels()[0, movingPiece.getType().Substring(0, 1) == "W" ? 7 : 0])
                                                {
                                                    movingPiece = x;
                                                    action      = 3;
                                                }
                                            }
                                        }
                                        actionPanel                 = generation.getPanels()[action, movingPiece.getType().Substring(0, 1) == "W" ? 7 : 0];
                                        movingPiecePanel            = movingPiece.getPanel();
                                        actionPanel.BackgroundImage =
                                            movingPiece.getPanel().BackgroundImage;

                                        undoPiece.Add(movingPiece);
                                        undoPanel.Add(movingPiece.getPanel());
                                        noUndos++;


                                        movingPiece.setPanel(actionPanel);

                                        generation.setPanelsInUse();
                                        movingPiecePanel.BackgroundImage = null;
                                        movingPiece.setMoved(true);
                                        movingPiece.increaseNoMoves();

                                        //moves the castle to thing other side of the king
                                    }



                                    if (movingPiece.getType() == "WPawn" ^
                                        movingPiece.getType() == "BPawn")
                                    {
                                        if ((movingPiece.getPanel().Location.Y == 0 &&
                                             movingPiece.getType() == "WPawn") ^
                                            (movingPiece.getPanel().Location.Y == 280 &&
                                             movingPiece.getType() == "BPawn"))
                                        {
                                            generation.RemovePiece(movingPiece);
                                            newQueen = new Queen
                                                           (movingPiece.getType().Substring(0, 1) +
                                                           "Queen", movingPiece.getPanel(), true);
                                            generation.AddPiece(newQueen);
                                        }
                                    }
                                    //If a pawn reaches the end, it becoms a queen

                                    int totalPossibleMoves = 0;
                                    foreach (Piece y in generation.getPieces().ToList())
                                    {
                                        y.setMoves(generation, true, suicide);
                                        if ((WhiteMove &&
                                             y.getType().Substring(0, 1) == "B") ^
                                            (!WhiteMove &&
                                             y.getType().Substring(0, 1) == "W"))
                                        {
                                            totalPossibleMoves += y.getMoves().Count;
                                        }
                                    }
                                    //Make sure there are some possible moves

                                    if (!(gameModes == "M3S") && movingPiece.checkCheck(generation))
                                    {
                                        MessageBox.Show("Check!");
                                        //Check check, if it is check, tell the players
                                        if (totalPossibleMoves == 0)
                                        {
                                            //There are no possible moves, it's checkemate
                                            timer1.Stop();
                                            MessageBox.Show("Check mate! " + (movingPiece.checkType() == "B" ? "White" : "Black") + " wins!");
                                            //EndGame()
                                        }
                                    }

                                    double val = moveCalc.getBoardValue(generation);
                                    WhiteMove   = !WhiteMove;
                                    movingPiece = null;
                                    //Next players move and a piece has not been clicked
                                    if (gameModes == "S1S")
                                    {
                                        generation.setPanelsInUse();
                                        actionPanel      = moveCalc.bestMove(generation, -10000, 10000);
                                        movingPiece      = moveCalc.getMovingPiece();
                                        movingPiecePanel = moveCalc.getMovingPiecePanel();

                                        if (generation.getPanelsInUse().Contains(actionPanel))
                                        {
                                            foreach (Piece z in generation.getPieces().ToList())
                                            {
                                                if (z.getPanel() == actionPanel)
                                                {
                                                    generation.RemovePiece(z);
                                                    if (z.getType().Substring(0, 1) == "W")
                                                    {
                                                        pictureBoxesB[bPiecesTaken].BackgroundImage = z.getPanel().BackgroundImage;
                                                        bPiecesTaken++;
                                                    }
                                                    else
                                                    {
                                                        pictureBoxesW[wPiecesTaken].BackgroundImage = z.getPanel().BackgroundImage;
                                                        wPiecesTaken++;
                                                    }
                                                }
                                            }
                                            actionPanel.BackgroundImage = null;

                                            /*If the panel was a possible move and contains a
                                             * piece, the piece that it already contains needs
                                             * to be disposed of and stored in the interface where
                                             * taken pieces are displayed*/
                                        }
                                        if (movingPiece.getType().Contains("King"))
                                        {
                                            if ((actionPanel == generation.getPanels()[6, movingPiece.getType().Substring(0, 1) == "W" ? 7 : 0] || actionPanel == generation.getPanels()[2, movingPiece.getType().Substring(0, 1) == "W" ? 7 : 0]) && !movingPiece.getMoved())
                                            {
                                                castle = true;
                                            }
                                        }
                                        //Recognises when the king is trying to castle


                                        if ((WhiteMove &&
                                             movingPiece.getType().Substring(0, 1).Equals("W")) ||
                                            (!WhiteMove &&
                                             movingPiece.getType().Substring(0, 1).Equals("B")))
                                        {
                                            actionPanel.BackgroundImage =
                                                movingPiece.getPanel().BackgroundImage;

                                            undoPiece.Add(movingPiece);
                                            undoPanel.Add(movingPiece.getPanel());
                                            noUndos++;

                                            movingPiece.setPanel(actionPanel);

                                            if (updateReq && movingPiece.getPanel() != updatePiece.getPanel())
                                            {
                                                updatePiece.setPanel(toUpdate);
                                                generation.RemovePiece(piecePosRemove);
                                                updateReq = false;
                                                //if a pawn has moved foward two last move, it can no longer be taken as if it were on the first sqaure, so don't allow that to happen.
                                            }
                                            else if (updateReq)
                                            {
                                                if (movingPiece.getType().Substring(0, 1) == "W")
                                                {
                                                    pictureBoxesW[wPiecesTaken - 1].BackgroundImage = Resources.BPawn;
                                                }
                                                else
                                                {
                                                    pictureBoxesB[bPiecesTaken - 1].BackgroundImage = Resources.WPawn;
                                                }
                                                updatePiece.getPanel().BackgroundImage    = movingPiece.getPanel().BackgroundImage;
                                                piecePosRemove.getPanel().BackgroundImage = null;
                                                generation.RemovePiece(piecePosRemove);
                                                updatePiece.setPanel(null);

                                                updateReq = false;

                                                //If a pawn has moved foward two last move, and a pawn has taken it as if it were on the first square, this allows it to be taken.
                                            }
                                            if (movingPiece.getType().Contains("Pawn"))
                                            {
                                                if (!movingPiece.getMoved() && movingPiece.getPanel().Location.Y == movingPiecePanel.Location.Y + ((movingPiece.getType().Contains("W")) ? -80 : 80))
                                                {
                                                    foreach (Panel x in generation.getPanels())
                                                    {
                                                        if (x.Location.Y + (movingPiece.getType().Contains("W") ? -40 : 40) == actionPanel.Location.Y && x.Location.X == actionPanel.Location.X)
                                                        {
                                                            toUpdate    = actionPanel;
                                                            updatePiece = movingPiece;
                                                            movingPiece.setPanel(x);
                                                            updateReq      = true;
                                                            piecePosRemove = new Pawn(updatePiece.getType(), actionPanel, true);
                                                            generation.AddPiece(piecePosRemove);
                                                            break;
                                                        }
                                                    }
                                                }
                                            }

                                            generation.setPanelsInUse();
                                            movingPiecePanel.BackgroundImage = null;
                                            movingPiece.setMoved(true);
                                            movingPiece.increaseNoMoves();

                                            //As long as the right person (whose turn it is) is
                                            //moving then this bit above moves it

                                            if (castle) //if the king is attempting to castle
                                            {
                                                int action = -1;
                                                if (actionPanel == generation.getPanels()[6, movingPiece.getType().Substring(0, 1) == "W" ? 7 : 0])
                                                {
                                                    foreach (Piece x in generation.getPieces())
                                                    {
                                                        if (x.getPanel() == generation.getPanels()[7, movingPiece.getType().Substring(0, 1) == "W" ? 7 : 0])
                                                        {
                                                            movingPiece = x;
                                                            action      = 5;
                                                        }
                                                    }
                                                }
                                                if (actionPanel == generation.getPanels()[2, movingPiece.getType().Substring(0, 1) == "W" ? 7 : 0])
                                                {
                                                    foreach (Piece x in generation.getPieces())
                                                    {
                                                        if (x.getPanel() == generation.getPanels()[0, movingPiece.getType().Substring(0, 1) == "W" ? 7 : 0])
                                                        {
                                                            movingPiece = x;
                                                            action      = 3;
                                                        }
                                                    }
                                                }
                                                actionPanel                 = generation.getPanels()[action, movingPiece.getType().Substring(0, 1) == "W" ? 7 : 0];
                                                movingPiecePanel            = movingPiece.getPanel();
                                                actionPanel.BackgroundImage =
                                                    movingPiece.getPanel().BackgroundImage;

                                                undoPiece.Add(movingPiece);
                                                undoPanel.Add(movingPiece.getPanel());
                                                noUndos++;


                                                movingPiece.setPanel(actionPanel);

                                                generation.setPanelsInUse();
                                                movingPiecePanel.BackgroundImage = null;
                                                movingPiece.setMoved(true);
                                                movingPiece.increaseNoMoves();

                                                //moves the castle to thing other side of the king
                                            }



                                            if (movingPiece.getType() == "WPawn" ^
                                                movingPiece.getType() == "BPawn")
                                            {
                                                if ((movingPiece.getPanel().Location.Y == 0 &&
                                                     movingPiece.getType() == "WPawn") ^
                                                    (movingPiece.getPanel().Location.Y == 280 &&
                                                     movingPiece.getType() == "BPawn"))
                                                {
                                                    generation.RemovePiece(movingPiece);
                                                    newQueen = new Queen
                                                                   (movingPiece.getType().Substring(0, 1) +
                                                                   "Queen", movingPiece.getPanel(), true);
                                                    generation.AddPiece(newQueen);
                                                }
                                            }
                                            //If a pawn reaches the end, it becoms a queen

                                            totalPossibleMoves = 0;
                                            foreach (Piece y in generation.getPieces().ToList())
                                            {
                                                y.setMoves(generation, true, suicide);
                                                if ((WhiteMove &&
                                                     y.getType().Substring(0, 1) == "B") ^
                                                    (!WhiteMove &&
                                                     y.getType().Substring(0, 1) == "W"))
                                                {
                                                    totalPossibleMoves += y.getMoves().Count;
                                                }
                                            }
                                            //Make sure there are some possible moves

                                            if (!(gameModes == "M3S") && movingPiece.checkCheck(generation))
                                            {
                                                MessageBox.Show("Check!");
                                                //Check check, if it is check, tell the players
                                                if (totalPossibleMoves == 0)
                                                {
                                                    //There are no possible moves, it's checkemate
                                                    timer1.Stop();
                                                    MessageBox.Show("Check mate! " + (movingPiece.checkType() == "B" ? "White" : "Black") + " wins!");
                                                    //EndGame()
                                                }
                                            }

                                            WhiteMove   = !WhiteMove;
                                            movingPiece = null;
                                        }
                                        else
                                        {
                                            movingPiece = null;
                                        }
                                    }
                                }
                            }
                            else
                            {
                                movingPiece = null;
                            }
                        }
                        catch (NullReferenceException)
                        {
                            movingPiece = null;
                        }
                        undos.Add(noUndos);
                        noUndos = 0;
                    }
                }
            }
        }