從你的要求,你要處理三個不同Professions
:
這些職業不抱任何狀態並且新實例不是動態構建的。因此,一個Enumeration
將是一個不錯的人選,以表示它們:
public enum Profession {
WARRIOR,
ROGUE,
MAGE;
}
接下來你要求玩家選擇哪個與1. Profession
和2相關的Answer
。用Token
,輸入你從鍵盤讀取:
public class Answer {
public static Answer of(Profession profession, String token, String description) {
return new Answer(description, profession, token);
}
private final String description;
private final Profession profession;
private final String token;
public Answer(String description, Profession profession, String token) {
this.description = requireNonNull(description);
this.profession = requireNonNull(profession);
this.token = requireNonNull(token);
}
public String getDescription() {
return description;
}
public Profession getProfession() {
return this.profession;
}
public String getToken() {
return token;
}
public boolean isMatch(String token) {
return this.token.equals(token);
}
}
你想與Answer
做的是:
- 它呈現給玩家。
- 檢查從玩家處獲得的令牌是否與
Answer
所關聯的令牌匹配。以可讀形式構建Answer
。
Answers
是Questions
的一部分,它們不保持狀態或動態創建。再一次的Enumeration
將代表他們很好:
public enum Question {
MAULED_DEER("While out hunting, you come across a deer which has been badly mauled by a wolf. What do you do?",
Answer.of(WARRIOR, "1", "Draw your dagger and end it's suffering."),
Answer.of(ROGUE, "2", "Attempt to heal it with a herbal concoction, knowing it may not work."),
Answer.of(MAGE, "3", "Leave it, and allow nature to take it's course.")),
GAME_DEVELOPMENT("While playing games you decided to develop your very own. Which language do you choose?",
Answer.of(MAGE, "a", "OOD with Java."),
Answer.of(WARRIOR, "b", "OOD and FP using Scala."),
Answer.of(ROGUE, "c", "Pure FP with Haskel."));
private final String description;
private final List<Answer> answers;
Question(String description, Answer... answers) {
this.description = description;
this.answers = Arrays.asList(answers);
}
public String getDescription() {
return description;
}
public List<Answer> getAnswers() {
return answers;
}
public Set<Answer> getMatches(String token) {
return answers.stream()
.filter(answer -> answer.isMatch(token))
.collect(Collectors.toSet());
}
}
Questions
必須是:
- 他們
Answers
一起渲染。
- 應該確定哪個
Answers
確實匹配給定的令牌(從玩家處獲得)。
但是你要問玩家很多Questions
並且想跟蹤他給出的Answers
。更準確地說,Profession
你與Answer
相關聯。你想跟蹤每個Profession
當前比分:
public class Scores {
private Map<Profession, AtomicInteger> scores = new HashMap<Profession, AtomicInteger>() {{
for (Profession profession : Profession.values()) {
put(profession, new AtomicInteger());
}
}};
private int max = 0;
public void incrementIf(boolean isTrue, Profession profession) {
if (isTrue) {
int score = scores.get(profession).incrementAndGet();
max = Math.max(max, score);
}
}
public Set<Profession> getWinners() {
return Arrays.stream(Profession.values())
.filter(profession -> scores.get(profession).get() == max)
.collect(Collectors.toSet());
}
}
類型Scores
的責任確定和哪個Profession
遞增的得分。最後,應當計算查詢的Winners
:
public class Inquiry {
public Set<Profession> obtainChoice(Scanner scanner) {
Scores scores = new Scores();
for (Question question : Question.values()) {
String token = ask(question, scanner);
question.getAnswers().forEach(answer ->
scores.incrementIf(answer.isMatch(token), answer.getProfession()));
}
return scores.getWinners();
}
private String ask(Question question, Scanner scanner) {
System.out.println(question.getDescription());
question.getAnswers().forEach(answer ->
System.out.println(answer.getToken() + ") " + answer.getDescription()));
do {
String token = scanner.nextLine();
for (Answer answer : question.getAnswers()) {
if (token.equals(answer.getToken())) {
return token;
}
}
System.out.println("Try again and select a valid answer please!");
} while (true);
}
}
的最後一塊拼圖缺少的是應用程序本身,這會觸發查詢:
public class Game {
public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
Set<Profession> choices = new Inquiry().obtainChoice(scanner);
if (choices.size() == 1) {
System.out.println("You are a true " + choices.iterator().next());
} else {
System.out.println("You havn't found your path yet and hence must choose between:");
choices.forEach(profession -> System.out.println(" - " + profession));
}
}
}
}
這種設計致力成爲readable
和extendable
等等你可以輕鬆地改變事物。
玩得開心!
這是一個實現flavory,不是技術問題,請google;) –
歡迎來到Stack Overflow!如果你還沒有參加[旅遊],請看[問]。無論如何,我認爲你應該更多地關注面向對象編程(OOP)。查看[classes](https://docs.oracle.com/javase/tutorial/java/concepts/class.html)和[collections](https://docs.oracle.com/javase/7/docs/api /java/util/Collections.html)。你可以用任何東西做一堂課。對於初學者來說,可能會嘗試創建一個名爲「Question」的類,其中包含與問題相關的所有信息。 – PJvG
另外,如果您有關於*軟件設計*的問題,那麼這些更適合http://softwareengineering.stackexchange.com/。如果您希望獲得有關代碼的反饋,則可以嘗試http://codereview.stackexchange.com/。 – PJvG