/**
   * Gets a question from the database not currently in the HashMap
   *
   * @return - a new question
   */
  private MazeQuestion getNewQuestion() {
    maxQuestionID = getMaxQuestionID();
    Random rng = new Random(System.currentTimeMillis() + System.nanoTime());
    int modValue = uniqueInstance.getMaxQuestionID() + 1;

    MazeQuestion newQuestion = null;

    while (newQuestion == null) {
      int toTry = rng.nextInt(modValue);

      if (questionHashMap.containsKey(new Integer(toTry))) {
        continue;
      }

      try {
        Class.forName("org.sqlite.JDBC");
      } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
        System.exit(0);
      }

      try (Connection connection = DriverManager.getConnection(MazeDB.getDBPath()); ) {
        connection.setAutoCommit(false);
        try (Statement statement = connection.createStatement();
            ResultSet rs =
                statement.executeQuery("SELECT * FROM QUESTION WHERE ID = " + toTry + ";"); ) {
          if (rs.next()) {

            int newQuestionID = rs.getInt(1);
            char newQuestionType = rs.getString(2).charAt(0);
            char newFileType = rs.getString(3).charAt(0);
            String newQuestionText = rs.getString(4);
            String newAnswerText = rs.getString(5);
            String newFilePath = rs.getString(6);

            // unescape SQL '
            newQuestionText = newQuestionText.replaceAll("''", "'");

            newQuestion =
                new MazeQuestion(
                    newQuestionID,
                    newQuestionType,
                    newFileType,
                    newQuestionText,
                    newAnswerText,
                    newFilePath);

          } else {
            continue;
          }
        }
      } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
        System.exit(0);
      }
    }
    return newQuestion;
  }
  /**
   * Called before getQuestionIterator to set its size and initialize it
   *
   * @param numberOfDoors - the number of questions needed for the current game
   */
  public void initializeQuestionHashMap(int numberOfDoors) {

    questionCount = numberOfDoors;
    int i = 0;

    if (questionHashMap != null) {
      questionHashMap.clear();
    }

    questionHashMap = new HashMap<Integer, MazeQuestion>(numberOfDoors);

    MazeQuestion newQuestionToAdd;
    Integer newQuestionKey;

    if (TriviaMaze.DEMO) {
      if (numberOfDoors < 4) {
        throw new RuntimeException("there must be at least 4 doors to run the demo");
      }
      i = 3;
      newQuestionToAdd =
          new MazeQuestion(
              196,
              'm',
              'v',
              "Who is dodging punches in the video? a. Muhammad Ali b. George Foreman c. Mike Tyson d. Raging Bull",
              "a",
              "srs/maze/mp4/0001.mp4");
      newQuestionKey = newQuestionToAdd.getQuestionID();
      questionHashMap.put(newQuestionKey, newQuestionToAdd);
      newQuestionToAdd =
          new MazeQuestion(
              192,
              'm',
              's',
              "What sort of bird makes this call? a. Ostrich b. Albatross c. Raven d. Robin",
              "d",
              "src/maze/mp3/0001.mp3");
      newQuestionKey = newQuestionToAdd.getQuestionID();
      questionHashMap.put(newQuestionKey, newQuestionToAdd);
      newQuestionToAdd =
          new MazeQuestion(
              194,
              'm',
              's',
              "Who composed this piece? a. Wolfgang Amadeus Mozart b. Bob Dylan c. Johann Sebastian Bach d. Madonna",
              "c",
              "src/maze/mp3/0002.mp3");
      newQuestionKey = newQuestionToAdd.getQuestionID();
      questionHashMap.put(newQuestionKey, newQuestionToAdd);
      newQuestionToAdd =
          new MazeQuestion(
              195,
              'm',
              's',
              "What school's fight song is this from? a. MIT b. Yale c. Notre Dame d. USC",
              "c",
              "src/maze/mp3/0003.mp3");
      newQuestionKey = newQuestionToAdd.getQuestionID();
      questionHashMap.put(newQuestionKey, newQuestionToAdd);
    }

    for (; i < questionCount; i++) {
      newQuestionToAdd = uniqueInstance.getNewQuestion();
      newQuestionKey = newQuestionToAdd.getQuestionID();
      questionHashMap.put(newQuestionKey, newQuestionToAdd);
    } // end for loop i
    Set<Integer> setOfKeys = questionHashMap.keySet();
    List<MazeQuestion> collectionOfQuestions =
        new ArrayList<MazeQuestion>(questionHashMap.values());
    Collections.shuffle(collectionOfQuestions);

    HashMap<Integer, MazeQuestion> shuffledQuestionHashMap =
        new HashMap<Integer, MazeQuestion>(numberOfDoors);
    Iterator<MazeQuestion> questionIterator = collectionOfQuestions.iterator();
    for (Integer integer : setOfKeys) {
      shuffledQuestionHashMap.put(integer, questionIterator.next());
    }
    questionHashMap = shuffledQuestionHashMap;
  }