我在Discord中为一个机器人制作一个简单的游戏。打印一个单词,人们必须猜测单词的读数,如果他们说得对,他们就会得到一个观点。在机器人继续前,他们会说5秒钟回答。我想做到这一点,以便我不必在游戏开始时指定玩家的数量,人们可以跳进去。
然后,一旦有人达到一定数量的积分,他们就会赢得比赛。
public static void vocabGame (MessageReceivedEvent event, String level, int total) {
int points = 0;
//Pause the program for 10000 milliseconds, or 10 seconds, before starting.
event.getTextChannel().sendMessage("`"+level+" vocab quiz starting in 10 seconds. \nFirst to reach "+total+" points wins.`").queue();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Not sure what to do with this while loop condition
while (points < total) {
//Store random line from text file and store in text variable.
String text = Reader.fileReader(level);
//Using patterns and matchers, going to break apart the text from the text file.
//The question first
Pattern question = Pattern.compile("\\{ \"question\": \"(.*?)\"");
Matcher questionMatch = question.matcher(text);
questionMatch.find();
String word = questionMatch.group(1);
//The answer second
Pattern answer = Pattern.compile("\"answers\": [ \"(.*?)\"");
Matcher answerMatch = answer.matcher(text);
answerMatch.find();
String ans = answerMatch.group(1);
//Get image of word from dummyimage and then print. Picture size can be changed via the URL
try {
final BufferedImage image = ImageIO.read(new URL("https://dummyimage.com/300x100/000/fff.png&text="+word));
ImageIO.write(image, "png", new File("word.png"));
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//Print picture with word on it
Message message = new MessageBuilder().append("Word:").build();
event.getTextChannel().sendFile(new File("word.png"), message).queue();
//TODO figure out a way to make the program wait for input, not just putting it to sleep
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
points++; //this is temp
}
}
我的问题是如何解决这些问题?我不是要求你为我编写代码,而是让我走上正轨,因为我不确定并且之前没有做过这样的事情。
我在考虑一个while循环,它在定时器处于活动状态时不断抓取用户输入,而不是我知道如何实现定时器。因为如果没有人回答,机器人仍会继续前进。
答案 0 :(得分:0)
1)我没有看到任何用于播放器的课程,你可以收集与播放器相关的数据并保留一组播放器。
2)你应该参考Observer Design Pattern并让玩家(1)自己注册,然后你处理已注册的整个更新的玩家列表。
3)在游戏时间过后,你会循环播放器列表,让玩家得分最高。