Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Need help to setup matches in this knockout tournament

  1. #1
    Junior Member
    Join Date
    Apr 2022
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Need help to setup matches in this knockout tournament

    I'm currently working on a knock-out tournament

    Overall Requirements:
    -UserInput for 8 teams (teamName and playerNames) stored in a file (MY test is currently with 2 teams)
    -Setup for matches (who vs. who) stored in a file
    -Results of the matches with points to the team(s) stored in a file
    -Ranking so we know who is out/in of the tournament

    *****CURRENT PROBLEM IS THIS*******
    I need a method which takes a team from my ArrayList<Team> or file (teamData.txt) and put it against another team - BOTH chosen from the user
    e.g. with 8 teams (team1-team8)
    team1 vs. team5

    Afterwards saves the match data in a file

    I HAVE NO CLUE ON HOW MY APPROACH SHOULD BE IN THIS... Any inputs? Thanks in advance


    public class Main {
        public static void main(String[] args) {
     
                Tournament tournament = IO.input(2);
                IO.writeTeamFile(tournament);
                FileIO.readGameData();
        }
    }
    import java.util.ArrayList;
     
    public class Tournament {
     
        private ArrayList<Team> teams;
     
        public Tournament() {
            this.teams = new ArrayList<Team>();
        }
        public ArrayList<Team> getTeams() {
            return teams;
        }
    }
    import java.util.ArrayList;
     
    public class Team {
     
        private String name;
        private ArrayList<Player> players;
     
        public Team(String name) {
            this.name = name;
            this.players = new ArrayList<Player>();
        }
     
        public String getName() {
            return name;
        }
     
        public ArrayList<Player> getPlayers() {
            return players;
        }
    }

    public class Player {
     
        private String name;
     
        public Player(String name) {
            this.name = name;
        }
     
        public String getName() {
            return name;
        }
    }
    import java.io.FileWriter;
    import java.util.Scanner;
     
    public class IO {
        private static Scanner scan = new Scanner(System.in);
        public static int teamCount;
     
        private static String readUserInput(String msg) {
            System.out.print(msg);
            return scan.nextLine();
        }
     
        public static Tournament input(int numTeams) {
            Tournament tournament = new Tournament();
            for (int i = 0; i < numTeams; i++) {
                String teamName = readUserInput("Team name: ");
                Team team = new Team(teamName);
                tournament.getTeams().add(team);
     
                int numPlayersPerTeam = Integer.parseInt(readUserInput("Hvor mange spillere er der på holdet?"));
     
                for (int j = 0; j < numPlayersPerTeam; j++) {
                    String playerName = readUserInput("Player name: ");
                    Player player = new Player(playerName);
                    team.getPlayers().add(player);
                }
            }
            return tournament;
        }
     
        public static void writeTeamFile(Tournament tournament) {
            try{
            FileWriter fileWriterDefault = new FileWriter("src/teamData.txt");
            fileWriterDefault.write("teamName, teamPlayers");
            fileWriterDefault.close();
            } catch (Exception e) {
                e.getStackTrace();
            }
     
            for (Team team : tournament.getTeams()) {
                String fullPlayerNames = "";
                teamCount ++; //Later use to numMatches
     
                for (Player player : team.getPlayers()) {
                    String tmpPlayer = player.getName() + ", ";
                    fullPlayerNames = fullPlayerNames + tmpPlayer;
                }
     
                try {
                    FileWriter fileWriter = new FileWriter("src/teamData.txt", true);
                    fileWriter.write("\n"+ team.getName() + ", " + fullPlayerNames);
                    fileWriter.close();
                } catch (Exception e) {
                    e.getStackTrace();
                }
            }
            System.out.println(teamCount);
        }
    }

    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
     
    public class FileIO {
     
        public static void readGameData() {
     
                String path = "src/teamData.txt";
                String teamNamesFile = "";
     
                try{
                    BufferedReader br = new BufferedReader(new FileReader(path));
                    br.readLine(); //Reads first line, so it skips the header
                    while((teamNamesFile = br.readLine()) != null){
                        String [] teamNames = teamNamesFile.split(",");
                        System.out.println("Team fra fil :" + teamNames[0]);
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }

    teamData.txt under here:
    teamName, teamPlayers
    teamA, A1, A2, A3, A4,
    teamB, B1, B2, B3, B4


    public class Match {
     
        public static void Match() {
     
     
        }
    }

  2. #2
    Member
    Join Date
    Jun 2022
    Posts
    41
    Thanks
    1
    Thanked 3 Times in 2 Posts

    Default Re: Need help to setup matches in this knockout tournament

    This looks like a problem best solved with a binary tree

Similar Threads

  1. Need help to setup matches in this knockout tournament
    By DinoKing in forum Object Oriented Programming
    Replies: 2
    Last Post: June 26th, 2024, 05:43 AM
  2. Tournament issue - I'm stuck and need a push in the right direction
    By DinoKing in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 30th, 2022, 10:05 AM
  3. Java Tournament Program
    By michaeldoubleyou in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 18th, 2012, 11:10 AM
  4. Replies: 2
    Last Post: March 4th, 2009, 06:32 AM
  5. Replies: 1
    Last Post: December 30th, 2008, 07:30 AM

Tags for this Thread