Ich mache ein einfaches Spiel für einen Bot in Discord. Ein Wort wird gedruckt und die Leute müssen das Lesen des Wortes erraten und wenn sie es richtig machen, bekommen sie einen Punkt. Sie werden sagen, 5 Sekunden zu beantworten, bevor der Bot weitergeht. Ich möchte das so machen, dass ich zu Beginn des Spiels nicht die Anzahl der Spieler angeben muss, die Leute können einfach reinspringen.Überblick über die Spieler in einem Spiel (Java)
Sobald jemand eine bestimmte Anzahl von Punkten erreicht hat, gewinnt er das Spiel.
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
}
}
Meine Frage ist, wie nehme ich an, die Punkte zu verfolgen? Ich bitte dich nicht, Code für mich zu schreiben, sondern bring mich einfach auf den richtigen Weg, weil ich unsicher bin und so etwas noch nie gemacht habe.
Ich habe über eine While-Schleife nachgedacht, die Benutzereingaben erfasst hat, während ein Timer aktiv war, nicht dass ich weiß, wie man einen Timer implementiert. Denn wenn niemand antwortet, geht der Bot trotzdem weiter.
Erstelle eine Sammlung der Daten der Spieler ... –