Exemplo n.º 1
0
  private List<IAMatch> getMatches(List<IAPlayer> userList) {
    List<IAMatch> matches = new ArrayList<IAMatch>();

    List<IAPlayer> tempList = new ArrayList<IAPlayer>();
    tempList.addAll(userList);
    Collections.sort(tempList, new IAComparator(this, IAComparator.pairingCompare));

    IAMatch byeMatch = null;
    // Setup the bye match if necessary
    // The player to get the bye is the lowest ranked player who has not had
    // a bye yet or who has the fewest byes
    if (tempList.size() % 2 == 1) {
      IAPlayer byeUser = null;
      int byUserCounter = 1;
      int minByes = 0;
      try {
        while (byeUser == null
            || byeUser.getByes(this) > minByes
            || (byeUser.getMatches(this) != null
                && byeUser.getMatches(this).get(byeUser.getMatches(this).size() - 1).isBye())) {
          if (byUserCounter > tempList.size()) {
            minByes++;
            byUserCounter = 1;
          }
          byeUser = tempList.get(tempList.size() - byUserCounter);

          byUserCounter++;
        }
      } catch (ArrayIndexOutOfBoundsException e) {
        byeUser = tempList.get(tempList.size() - 1);
      }
      byeMatch = new IAMatch(byeUser, null);
      tempList.remove(byeUser);
    }

    matches = new IARandomMatchGeneration(this, tempList).generateMatches();

    if (IAMatch.hasDuplicate(matches)) {
      JOptionPane.showMessageDialog(
          Main.getInstance(),
          "Unable to resolve duplicate matches. Please review for best course of action.");
    }

    // Add the bye match at the end
    if (byeMatch != null) {
      matches.add(byeMatch);
    }

    return matches;
  }