public String getUnitSource(chessPieces obj) { String returner = ""; switch (obj) { case chessPieces.BlackBishop: returner = "/Images/Black B.png"; break; case chessPieces.BlackKing: returner = "/Images/Black K.png"; break; case chessPieces.BlackKnight: returner = "/Images/Black N.png"; break; case chessPieces.BlackPawn: returner = "/Images/Black P.png"; break; case chessPieces.BlackQueen: returner = "/Images/Black Q.png"; break; case chessPieces.BlackRook: returner = "/Images/Black R.png"; break; case chessPieces.WhiteBishop: returner = "/Images/White B.png"; break; case chessPieces.WhiteKing: returner = "/Images/White K.png"; break; case chessPieces.WhiteKnight: returner = "/Images/White N.png"; break; case chessPieces.WhitePawn: returner = "/Images/White P.png"; break; case chessPieces.WhiteQueen: returner = "/Images/White Q.png"; break; case chessPieces.WhiteRook: returner = "/Images/White R.png"; break; case chessPieces.None: returner = "/Images/None.png"; break; } return(returner); }
//Moves chess piece from old to new location on the board public void movePiece(int oldCol, int oldRow, int newCol, int newRow) { //Finds the type and colour of piece that we're moving chessPieces selectedType = chessBoard[oldCol, oldRow].Type(); chessColour selectedColour = chessBoard[oldCol, oldRow].Colour; //You can't move a piece if it doesn't exist if (selectedType != chessPieces.CLEAR) { //Variables to update display after enPassent move bool enPassant = false; Pawn enPassantPawn = null; if (ChessPiece.DblMovePawn != null && chessBoard[oldCol, oldRow].Type() == chessPieces.PAWN) { enPassant = ChessPiece.isEnPassant(oldCol, oldRow, newCol, newRow, chessBoard); enPassantPawn = (Pawn)(ChessPiece.DblMovePawn).Clone(); } //Variables to update display after castling move bool castling = false; if (chessBoard[oldCol, oldRow].CanCastle && chessBoard[newCol, newRow].CanCastle) { castling = ChessPiece.isCastling(chessBoard[oldCol, oldRow], chessBoard[newCol, newRow], chessBoard); } //move the piece ChessPiece.Move(oldCol, oldRow, newCol, newRow, chessBoard); selectedType = chessBoard[newCol, newRow].Type(); //type may have changed (pawn->queen), so update //Every time a valid move is made, switch turns currentGame.changeTurns(); //DISPLAY - Special case: If we are castling, just update the whole row that the king is on if (castling) { for (int i = 0; i < MAX_CHESS_SQUARES; i++) { displayPieceAt(i, newRow, chessBoard[i, newRow].Type(), chessBoard[i, newRow].Colour); updateSelectionSquare(); } } else { //DISPLAY - Regular Move: Update only the piece that just moved: displayPieceAt(newCol, newRow, selectedType, selectedColour); //DISPLAY - Special case: If the move was enPassent, update the possible pawn locations (+/- one from the pawn that just moved if (enPassant) { displayPieceAt(enPassantPawn.PositionX, enPassantPawn.PositionY, chessBoard[enPassantPawn.PositionX, enPassantPawn.PositionY].Type(), chessBoard[enPassantPawn.PositionX, enPassantPawn.PositionY].Colour); } } } }
//Sets all chess pieces to display in the startup positions public void updateDisplay() { updateTitleBar(); for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { chessColour tmpColour = chessBoard[i, j].Colour; chessPieces tmpType = chessBoard[i, j].Type(); displayPieceAt(i, j, tmpType, tmpColour); } } updateSelectionSquare(); }
//Displays the chess piece at the given position private void displayPieceAt(int newPosX, int newPosY, chessPieces type, chessColour colour) { //update title bar whenever you move a piece updateTitleBar(); switch (type) { case chessPieces.KING: if (colour == chessColour.WHITE) //Display White King { if (squareIsWhite[newPosX, newPosY]) { dgv_chessBoard[newPosX, newPosY].Value = Chess.Properties.Resources.WhiteKingW; } else { dgv_chessBoard[newPosX, newPosY].Value = Chess.Properties.Resources.WhiteKingB; } } else //Display Black King { if (squareIsWhite[newPosX, newPosY]) { dgv_chessBoard[newPosX, newPosY].Value = Chess.Properties.Resources.BlackKingW; } else { dgv_chessBoard[newPosX, newPosY].Value = Chess.Properties.Resources.BlackKingB; } } break; case chessPieces.QUEEN: if (colour == chessColour.WHITE) //Display White Queen { if (squareIsWhite[newPosX, newPosY]) { dgv_chessBoard[newPosX, newPosY].Value = Chess.Properties.Resources.WhiteQueenW; } else { dgv_chessBoard[newPosX, newPosY].Value = Chess.Properties.Resources.WhiteQueenB; } } else //Display Black Queen { if (squareIsWhite[newPosX, newPosY]) { dgv_chessBoard[newPosX, newPosY].Value = Chess.Properties.Resources.BlackQueenW; } else { dgv_chessBoard[newPosX, newPosY].Value = Chess.Properties.Resources.BlackQueenB; } } break; case chessPieces.BISHOP: if (colour == chessColour.WHITE) //Display White Bishop { if (squareIsWhite[newPosX, newPosY]) { dgv_chessBoard[newPosX, newPosY].Value = Chess.Properties.Resources.WhiteBishopW; } else { dgv_chessBoard[newPosX, newPosY].Value = Chess.Properties.Resources.WhiteBishopB; } } else //Display Black Bishop { if (squareIsWhite[newPosX, newPosY]) { dgv_chessBoard[newPosX, newPosY].Value = Chess.Properties.Resources.BlackBishopW; } else { dgv_chessBoard[newPosX, newPosY].Value = Chess.Properties.Resources.BlackBishopB; } } break; case chessPieces.KNIGHT: if (colour == chessColour.WHITE) //Display White Knight { if (squareIsWhite[newPosX, newPosY]) { dgv_chessBoard[newPosX, newPosY].Value = Chess.Properties.Resources.WhiteKnightW; } else { dgv_chessBoard[newPosX, newPosY].Value = Chess.Properties.Resources.WhiteKnightB; } } else //Display Black Knight { if (squareIsWhite[newPosX, newPosY]) { dgv_chessBoard[newPosX, newPosY].Value = Chess.Properties.Resources.BlackKnightW; } else { dgv_chessBoard[newPosX, newPosY].Value = Chess.Properties.Resources.BlackKnightB; } } break; case chessPieces.ROOK: if (colour == chessColour.WHITE) //Display White Rook { if (squareIsWhite[newPosX, newPosY]) { dgv_chessBoard[newPosX, newPosY].Value = Chess.Properties.Resources.WhiteRookW; } else { dgv_chessBoard[newPosX, newPosY].Value = Chess.Properties.Resources.WhiteRookB; } } else //Display Black Rook { if (squareIsWhite[newPosX, newPosY]) { dgv_chessBoard[newPosX, newPosY].Value = Chess.Properties.Resources.BlackRookW; } else { dgv_chessBoard[newPosX, newPosY].Value = Chess.Properties.Resources.BlackRookB; } } break; case chessPieces.PAWN: if (colour == chessColour.WHITE) //Display White Pawn { if (squareIsWhite[newPosX, newPosY]) { dgv_chessBoard[newPosX, newPosY].Value = Chess.Properties.Resources.WhitePawnW; } else { dgv_chessBoard[newPosX, newPosY].Value = Chess.Properties.Resources.WhitePawnB; } } else //Display Black Pawn { if (squareIsWhite[newPosX, newPosY]) { dgv_chessBoard[newPosX, newPosY].Value = Chess.Properties.Resources.BlackPawnW; } else { dgv_chessBoard[newPosX, newPosY].Value = Chess.Properties.Resources.BlackPawnB; } } break; case chessPieces.CLEAR: blankSquareAt(newPosX, newPosY); break; } }