//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(); } } } }
//Chessgame class that runs the GUI for the game public ChessGame() { InitializeComponent(); renderBoard(); StateOfPlay.setGameScore(); startButton.MouseClick += StartButton_MouseClick; }
//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; } } }
//Click event method that sets the board and displays intro message private void StartButton_MouseClick(object sender, MouseEventArgs e) { Button clickedButton = (Button)sender; //loops through each piece and puts them on the board StateOfPlay.setPieces(); fillBoard(StateOfPlay.activePieces); //Displays gamescore in the textbox setScore(); //Removes the click event on start button startButton.MouseClick -= StartButton_MouseClick; //Displays intro message MessageBox.Show("Welcome to my chess appliaction.\nClick your piece then a valid spot to see valid moves.\nValid moves are indicated by the color green. Invalid by red", "Chess Sim App"); for (int i = 0; i < ChessBoard.BoardSize; i++) { for (int j = 0; j < ChessBoard.BoardSize; j++) { guiGrid[i, j].Click += Board_Button_Click; } } }