2017-08-10 15 views
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ループについて考えていましたが、タイマーを実装する方法はわかりません。誰も答えなければ、ボットはとにかく動きますから。

+0

プレーヤーのデータのコレクションを作成する... –

答えて

0

1)プレーヤーに使用されるクラスはありません。プレーヤーに関するデータを集め、プレーヤーのコレクションを保持することができます。

2)Observer Design Patternを参照し、players(1)自身が登録し、登録済みの更新されたPlayerリスト全体を処理させる必要があります。

3)あなたの時間がゲームのスロットのために渡された後、あなたはプレーヤーのリストをループし、最も高い得点を持つプレーヤーを得ます。

関連する問題