Esempio n. 1
0
        //Function that handles the click on the buttons and implements the process of showing each time the modifications made by the game
        private void Grid_Button_Click(object sender, EventArgs e)
        {
            if (myGame.GameStatus == Const.GAME_ON)
            {
                if (myGame.Turn.Player != myGame.Human)
                {
                    myGame.AI();   //Let AI play

                    AddMove();     //Add move played into the listView

                    ReDrawBoard(); //Refresh GUI

                    return;
                }

                //Get button pressed
                Button clickedButton = (Button)sender;
                Point  clickedSquare = (Point)clickedButton.Tag;

                if (
                    PreviousClickedCell == new Point(-1, -1) &&                                                //No piece has been clicked yet
                    myGame.Board.Grid[clickedSquare.X, clickedSquare.Y] != 0 &&                                //The square clicked contains a piece
                    Utils.PieceToColor(myGame.Board.Grid[clickedSquare.X, clickedSquare.Y]) == myGame.Human && //The piece selected it the same color as the one of the human player
                    myGame.Human == myGame.Turn.Player)                                                        //It's the turn of the human player
                {
                    PreviousClickedCell = myGame.GetLegalMoves(clickedSquare);
                }
                else if (PreviousClickedCell != new Point(-1, -1) && myGame.Human == myGame.Turn.Player)
                {
                    if (!(myGame.LegalMoves[clickedSquare.X, clickedSquare.Y] == Const.CAPTURE_MOVE) && !(myGame.LegalMoves[clickedSquare.X, clickedSquare.Y] == Const.LEGAL_MOVE)) //The player makes a non-legal move
                    {
                        if (myGame.Board.Grid[clickedSquare.X, clickedSquare.Y] != 0 && Utils.PieceToColor(myGame.Board.Grid[clickedSquare.X, clickedSquare.Y]) == myGame.Human)
                        {
                            myGame.CleanLegalMoves();
                            PreviousClickedCell = myGame.GetLegalMoves(clickedSquare);
                        }
                        else
                        {
                            myGame.CleanLegalMoves();
                            PreviousClickedCell = new Point(-1, -1); //go back to unclicked state for this ply
                        }
                    }
                    else   //The player makes a legal move
                    {
                        myGame.MakeMove(new Move(PreviousClickedCell, clickedSquare, myGame.Board.Grid[PreviousClickedCell.X, PreviousClickedCell.Y], myGame.Board.Grid[clickedSquare.X, clickedSquare.Y]));
                        AddMove();
                        button1.Enabled     = false;
                        PreviousClickedCell = new Point(-1, -1);
                    }
                }
                else
                {
                    PreviousClickedCell = new Point(-1, -1);
                }

                //redraw colors to make changes visible
                ReDrawBoard();
            }
        }