I'm just wondering if its possible to implement another class once an object has been selected from an object array? I'm creating a chess game with 3 different levels of difficulty and once the user chooses a difficulty,then the game state would change based on their selection. The code I have for selection is:
Object[] options={"Easy","Medium", "Hard"};
human=JOptionPane.showOptionDialog(null, "Please Select Difficulty", "Please Select Difficulty", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, options, options[1]);
I have 3 classes made for the difficulties
Could I use an if statement to make this possible?
Thanks
You could probably do it like:
class ChessGame
class DifficltChessGame extends ChessGame
class MediumChessGame extends ChessGame
class SimpleChessGame extends ChessGame
class ChessGameFactory {
public static ChessGame getChessGame(String gameType) {
switch(gameType) { //jdk7
case "easy":
return new SimpleChessGame();
....
}
}
}
And from your code you could do something like:
String[] options={"Easy","Medium", "Hard"};//use enum instead
humanInput=...
ChessGame game = ChessGameFactory.getChessGame(humanInput);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.