从你的要求,你要处理三个不同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