private void checkGameState() //checks if the game is over via checkmate or stalemate { int[] kingLoc = game.findKing(turn); if (((King)game.getBoard()[kingLoc[0], kingLoc[1]]).isCheck(kingLoc, game.getBoard(), colorSel)) { if (((King)game.getBoard()[kingLoc[0], kingLoc[1]]).isCheckMate(colorSel, game.getPieces(), game.getBoard(), turn)) //king is in check and no moves can be made "checkmate" { for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { boardButtons[i, j].Enabled = false; //game is over disable all board buttons } } Label gameOverLabel = new Label(); gameOverLabel.Size = new System.Drawing.Size(250, 13); gameOverLabel.Text = "Game Over: Checkmate "; if (turn == 0) //if it was whites turn next and white is checkmated black wins { gameOverLabel.Text += "Black Wins"; } else //the opposite is true so white wins { gameOverLabel.Text += "White Wins"; } gameOverLabel.Location = new Point(150, 420); Controls.Add(gameOverLabel); //display that the one side has been checkmated and tell who the winner is } } else { if (((King)game.getBoard()[kingLoc[0], kingLoc[1]]).isCheckMate(colorSel, game.getPieces(), game.getBoard(), turn)) //king is not in check but no moves can be made "stalemate" { for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { boardButtons[i, j].Enabled = false; //game is over disable all board buttons } } Label gameOverLabel = new Label(); gameOverLabel.Size = new System.Drawing.Size(250, 13); gameOverLabel.Text = "Game Over: Stalemate"; gameOverLabel.Location = new Point(150, 420); Controls.Add(gameOverLabel); //display that the game is a stalemate } } }
public bool isMoveSafe(int colorSel, ChessPiece[] pcs, int pNum, int[] moveLoc) //returns whether a move is safe or not //if this list is empty it is checkmate { Board b; ChessPiece[] p = new ChessPiece[32]; for (int i = 0; i < pcs.Length; i++) //create a set of new pieces to be passed to the new board for checking { if (pcs[i].getType() == 1) { p[i] = new Pawn(i, pcs[i].getPColor()); p[i].setLocation(pcs[i].getLocation()[0], pcs[i].getLocation()[1]); } if (pcs[i].getType() == 2) { p[i] = new Knight(i, pcs[i].getPColor()); p[i].setLocation(pcs[i].getLocation()[0], pcs[i].getLocation()[1]); } if (pcs[i].getType() == 3) { p[i] = new Bishop(i, pcs[i].getPColor()); p[i].setLocation(pcs[i].getLocation()[0], pcs[i].getLocation()[1]); } if (pcs[i].getType() == 4) { p[i] = new Rook(i, pcs[i].getPColor()); p[i].setLocation(pcs[i].getLocation()[0], pcs[i].getLocation()[1]); } if (pcs[i].getType() == 5) { p[i] = new Queen(i, pcs[i].getPColor()); p[i].setLocation(pcs[i].getLocation()[0], pcs[i].getLocation()[1]); } if (pcs[i].getType() == 6) { p[i] = new King(i, pcs[i].getPColor()); p[i].setLocation(pcs[i].getLocation()[0], pcs[i].getLocation()[1]); } if (p[i].getLocation()[0] == moveLoc[0] && p[i].getLocation()[1] == moveLoc[1] && p[i].getType() != 6) //no piece can hold the same location { p[i].setLocation(-1, -1); } } p[pNum].setLocation(moveLoc[0], moveLoc[1]); b = new Board(colorSel, p); //create a new board with the new move location to check if it puts the king in check int[] kingLoc = b.findKing(pcs[pNum].getPColor()); //the kings location in the new board if (((King)b.getBoard()[kingLoc[0], kingLoc[1]]).isCheck(kingLoc, b.getBoard(), colorSel)) //if the move is check return false { return(false); } else //if the move is safe return true { return(true); } }