Example #1
0
        //Click event that occurs after the player clicks on a piece
        private void ChessGame_Click(object sender, EventArgs e)
        {
            Button clickedButton = (Button)sender;
            Point  location      = (Point)clickedButton.Tag;

            //check validity of move
            if (StateOfPlay.isValidMove(piece, StateOfPlay.gameBoard.board[location.X, location.Y]))
            {
                //use move method
                playerMovePiece(piece, location.X, location.Y, player);
                for (int j = 0; j < StateOfPlay.activePieces.Count; j++)
                {
                    if (StateOfPlay.activePieces[j].xPos == location.X && StateOfPlay.activePieces[j].yPos == location.Y)
                    {
                        StateOfPlay.takePiece(StateOfPlay.activePieces[j]);
                    }
                }
                //traverse cells and remove second level click events and add initial click event
                for (int i = 0; i < ChessBoard.BoardSize; i++)
                {
                    for (int z = 0; z < ChessBoard.BoardSize; z++)
                    {
                        StateOfPlay.gameBoard.board[i, z].isOccupied = true;
                        guiGrid[i, z].MouseMove  -= Form1_MouseMove;
                        guiGrid[i, z].Click      -= ChessGame_Click;
                        guiGrid[i, z].MouseLeave -= Form1_MouseLeave;
                        guiGrid[i, z].Click      += Board_Button_Click;
                        player.isPlayerTurn       = true;
                        //runCpuTurn();
                    }
                }
            }
        }
Example #2
0
        //sets color to green for valid move and red for invalid
        private void Form1_MouseMove(object sender, EventArgs e)
        {
            Button hoveredButton = (Button)sender;
            Point  location      = (Point)hoveredButton.Tag;

            if (player.isPlayerTurn)
            {
                if (StateOfPlay.isValidMove(piece, StateOfPlay.gameBoard.board[location.X, location.Y]))
                {
                    hoveredButton.BackColor = Color.Green;
                }
                else
                {
                    hoveredButton.BackColor = Color.Red;
                }
            }
        }