/** * This method resets the game engine. It sets the chess board and players to its default state */ public void resetGame() { _chessBoard.initialize(); _players[0].initialize(); _players[1].initialize(); _chessBoard.initializePlayers(_players[0].getPieces(), ChessConstants.TEAM_WHITE); _chessBoard.initializePlayers(_players[1].getPieces(), ChessConstants.TEAM_BLACK); }
/** Method to initialize the engine. It creates the players and prepares the chess board */ public void initialize() { _players = new ChessBoardPlayer[2]; _players[0] = new ChessBoardPlayer(ChessConstants.TEAM_WHITE); _players[1] = new ChessBoardPlayer(ChessConstants.TEAM_BLACK); _chessBoard = new ChessBoard(); _chessBoard.initialize(); _players[0].initialize(); _players[1].initialize(); _chessBoard.initializePlayers(_players[0].getPieces(), ChessConstants.TEAM_WHITE); _chessBoard.initializePlayers(_players[1].getPieces(), ChessConstants.TEAM_BLACK); }
/** * Provides a string representation of the state of the board * * @return String - representation of the board */ public String boardDump() { String ret = ""; for (int i = 8; i >= 1; i--) { ret += i; ret += " " + ((_chessBoard.getCell("a" + i).getPiece() != null) ? _chessBoard.getCell("a" + i).getPiece().drawPiece() : "-"); ret += " " + ((_chessBoard.getCell("b" + i).getPiece() != null) ? _chessBoard.getCell("b" + i).getPiece().drawPiece() : "-"); ret += " " + ((_chessBoard.getCell("c" + i).getPiece() != null) ? _chessBoard.getCell("c" + i).getPiece().drawPiece() : "-"); ret += " " + ((_chessBoard.getCell("d" + i).getPiece() != null) ? _chessBoard.getCell("d" + i).getPiece().drawPiece() : "-"); ret += " " + ((_chessBoard.getCell("e" + i).getPiece() != null) ? _chessBoard.getCell("e" + i).getPiece().drawPiece() : "-"); ret += " " + ((_chessBoard.getCell("f" + i).getPiece() != null) ? _chessBoard.getCell("f" + i).getPiece().drawPiece() : "-"); ret += " " + ((_chessBoard.getCell("g" + i).getPiece() != null) ? _chessBoard.getCell("g" + i).getPiece().drawPiece() : "-"); ret += " " + ((_chessBoard.getCell("h" + i).getPiece() != null) ? _chessBoard.getCell("h" + i).getPiece().drawPiece() : "-"); ret += "\n"; } ret += "\n a b c d e f g h"; return ret; }
/** * Returns the type of the piece at the specified location * * @param String - specified location * @return String - return message containing the type of the piece */ public String getPieceAt(String pos) { return ChessConstants.toString(_chessBoard.getPieceAt(pos)); }
/** * Removes the piece at the specified location * * @param String - specified location * @return String - return message indicating whether the piece was removed successfully or not */ public String removePieceAt(String pos) { int ret = _chessBoard.removePieceAt(pos); if (ret != 0) return ChessConstants.toString(ret); return "Piece Removed Successfully"; }
/** * This method moves a specified piece from src position to dst position * * @param String - source position * @param String - destination position * @return String - return message indicating whether the move was successful or not */ public String move(String src, String dst) { int ret = _chessBoard.movePiece(src, dst); if (ret != 0) return ChessConstants.toString(ret); return "Move Completed Successfully!"; }