/********************************************************************* * IsCheckMate * Loop through the board and for each piece we check if they can kill * the king if they can we end and return true. *********************************************************************/ public bool IsCheckMate() { for (int r = 0; r < 8; r++) { for (int c = 0; c < 8; c++) { Piece piece = board[r, c].GetPiece(); if (piece != null && piece.GetTeam() != turn && piece.GetPieceType() == "King") { Piece king = board[r, c].GetPiece(); Piece killer = null; king.CalcPossMoves(board); bool Check = false; int checkCount = 0; int vulnerableChecks = 0; if (IsCheck(r, c, ref killer)) { Check = true; checkCount++; if (IsKillable(killer.GetRow(), killer.GetColumn(), turn)) { vulnerableChecks++; } } if (Check) { for (int i = 0; i < king.GetPossMoves().Count; i++) { if (IsCheck(king.GetPossMoves()[i].row, king.GetPossMoves()[i].col, ref killer)) { checkCount++; if (IsKillable(killer.GetRow(), killer.GetColumn(), turn)) { vulnerableChecks++; } } } } if (checkCount > 0 && checkCount == king.GetPossMoves().Count) { checkText.Text = "CHECKMATE"; return(true); } else if (checkCount == 0 || checkCount == vulnerableChecks) { checkText.Text = ""; } else if (Check) { MessageBox.Show("king is checked"); checkText.Text = "CHECK"; } } } } return(false); }
/********************************************************************* * ButtonClick * Handles the button click for each space of the board *********************************************************************/ public void ButtonClick(object sender, MouseEventArgs e) { Space thisButton = ((Space)sender); //MessageBox.Show(whiteTeam[0].GetRow().ToString() + " " + whiteTeam[0].GetColumn().ToString()); if (!gameOver) { // Check if it was a right mouse click if (e.Button == MouseButtons.Right) { // if this was the selected space then we unselect it if (selected != null && thisButton.GetPiece() == selected) { selected = null; thisButton.ForeColor = thisButton.GetPiece().GetTextColor(); ClearPossible(); return; } } // left mouse click else { // if there isn't a piece selected if (selected == null) { // if the place we just clicked on has a piece there if (thisButton.GetPiece() != null) { // if the piece is on our team if (turn == thisButton.GetPiece().GetTeam()) { // set this piece as the selected one selected = thisButton.GetPiece(); if (e.Button == MouseButtons.Right) { } else { // calculate all the spots this piece could move to selected.CalcPossMoves(board); // if it can't move then we unselect it if (selected.GetPossMoves().Count == 0) { selected = null; } // we set the text color to yellow to show which piece is selected else { thisButton.ForeColor = Color.Yellow; ShowPossibleMoves(); } } } } } // if we already had a selected piece else { int row = thisButton.GetRow(); int col = thisButton.GetCol(); // if the selected piece can be moved here then we move it if (IsPossible(row, col)) { int fromRow = selected.GetRow(); int fromCol = selected.GetColumn(); MoveTo(fromRow, fromCol, row, col); ClearPossible(); gameOver = IsCheckMate(); SwitchTurn(); } } } } }