/** * Creates a game instance and creates a multisocket. * * @param portNumber port number for multisocket to use. * @param questionDatabase the database to get questions from * @param gameServer the server that started this game instance */ public GameInstance(int portNumber, QuestionDatabase questionDatabase, GameServer gameServer) { this.portNumber = portNumber; this.gameServer = gameServer; this.questionDatabase = questionDatabase; numberOfQuestions = questionDatabase.getDatabaseSize(); try { // setup incoming multicast multiSocket = new MulticastSocket(portNumber); address = InetAddress.getByName(GROUP_IP); multiSocket.joinGroup(address); } catch (IOException e) { } }
/** * Get a random question form the database. * * @return A question that has not been asked since the last question reset. */ public String getNewQuestion() { int questionNumber = 0; boolean numberFound = false; while (numberFound == false) { // get a random question number questionNumber = (int) (Math.random() * numberOfQuestions); // check if question already asked if (!questionsAsked.contains(questionNumber)) { numberFound = true; questionsAsked.add(questionNumber); // reset questions asked if all have been asked if (questionsAsked.size() == numberOfQuestions) { questionsAsked.clear(); } } } return questionDatabase.getQuestion(questionNumber); }