0
我正在爲Discord中的機器人做一個簡單的遊戲。一個單詞被打印出來,人們必須猜測單詞的閱讀情況,如果他們正確的話,他們會得到一個答案。在機器人移動之前,他們會有5秒的時間回答。我想這樣做以便我不必在遊戲開始時指定玩家的數量,人們可以跳進去。跟蹤遊戲中的玩家(Java)
然後一旦有人達到一定數量的分數,他們就贏得比賽。
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循環,在定時器處於活動狀態時不斷捕獲用戶輸入,而不是我知道如何實現定時器。因爲如果沒有人回答,機器人就會移動。
創建一個玩家數據集合... –