import java.util.*;
import java.time.*;

public class PadelTournamentCreator {

    public static List<Pair<List<String>, List<String>>> generateSchedule(List<String> players, String matchType) {
        List<Pair<List<String>, List<String>>> schedule = new ArrayList<>();
        if (matchType.equals("Doubles")) {
            List<List<String>> teams = new ArrayList<>();
            for (int i = 0; i < players.size(); i++) {
                for (int j = i + 1; j < players.size(); j++) {
                    teams.add(Arrays.asList(players.get(i), players.get(j)));
                }
            }
            List<Pair<List<String>, List<String>>> matches = new ArrayList<>();
            for (int i = 0; i < teams.size(); i++) {
                for (int j = i + 1; j < teams.size(); j++) {
                    matches.add(new Pair<>(teams.get(i), teams.get(j)));
                }
            }
            Collections.shuffle(matches);
            for (Pair<List<String>, List<String>> match : matches) {
                List<String> team1 = match.getKey();
                List<String> team2 = match.getValue();
                if (Collections.disjoint(team1, team2)) {
                    schedule.add(match);
                }
            }
            Collections.shuffle(schedule);
        } else {
            List<Pair<String, String>> matches = new ArrayList<>();
            for (int i = 0; i < players.size(); i++) {
                for (int j = i + 1; j < players.size(); j++) {
                    matches.add(new Pair<>(players.get(i), players.get(j)));
                }
            }
            Collections.shuffle(matches);
            for (Pair<String, String> match : matches) {
                schedule.add(new Pair<>(Arrays.asList(match.getKey()), Arrays.asList(match.getValue())));
            }
        }
        return schedule;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Padel Tournament Creator");

        System.out.print("Enter the number of players: ");
        int numPlayers = scanner.nextInt();
        scanner.nextLine(); // consume newline

        System.out.print("Select match type (Singles/Doubles): ");
        String matchType = scanner.nextLine();

        System.out.print("Select start time (HH:mm): ");
        String[] timeParts = scanner.nextLine().split(":");
        LocalTime startTime = LocalTime.of(Integer.parseInt(timeParts[0]), Integer.parseInt(timeParts[1]));

        System.out.print("Enter total play time in minutes: ");
        int playDuration = scanner.nextInt();

        if (numPlayers > 0) {
            List<String> players = new ArrayList<>();
            for (int i = 0; i < numPlayers; i++) {
                System.out.print("Enter name for Player " + (i + 1) + ": ");
                players.add(scanner.nextLine());
            }

            System.out.print("Create Schedule? (yes/no): ");
            String createSchedule = scanner.nextLine();
            if (createSchedule.equalsIgnoreCase("yes")) {
                List<Pair<List<String>, List<String>>> schedule = generateSchedule(players, matchType);
                int matchDuration = playDuration / schedule.size();

                System.out.println("Tournament Schedule:");
                Set<String> allPlayers = new HashSet<>(players);
                LocalDateTime currentTime = LocalDateTime.now().withHour(startTime.getHour()).withMinute(startTime.getMinute()).withSecond(0).withNano(0);
                for (Pair<List<String>, List<String>> match : schedule) {
                    if (matchType.equals("Doubles")) {
                        List<String> team1 = match.getKey();
                        List<String> team2 = match.getValue();
                        Set<String> scheduledPlayers = new HashSet<>(team1);
                        scheduledPlayers.addAll(team2);
                        Set<String> sittingOut = new HashSet<>(allPlayers);
                        sittingOut.removeAll(scheduledPlayers);
                        System.out.println(currentTime.toLocalTime() + " - " + team1 + " vs " + team2 + " Bye " + String.join(", ", sittingOut));
                    } else {
                        String player1 = match.getKey().get(0);
                        String player2 = match.getValue().get(0);
                        Set<String> scheduledPlayers = new HashSet<>(Arrays.asList(player1, player2));
                        Set<String> sittingOut = new HashSet<>(allPlayers);
                        sittingOut.removeAll(scheduledPlayers);
                        System.out.println(currentTime.toLocalTime() + " - " + player1 + " vs " + player2 + " Bye " + String.join(", ", sittingOut));
                    }
                    currentTime = currentTime.plusMinutes(matchDuration);
                }
            }
        }
        scanner.close();
    }

    static class Pair<K, V> {
        private K key;
        private V value;

        public Pair(K key, V value) {
            this.key = key;
            this.value = value;
        }

        public K getKey() {
            return key;
        }

        public V getValue() {
            return value;
        }
    }
}
Scroll to Top