1

Struggling to word this one but I have a list of players in a league. A league has a list of rounds, which has a list of games. I need to create games using two players from the list of players in the league

How can i ensure that when I create a game it does not use a player that has already been selected, as they are selected randomly from the list.

If i remove them from the list once they are selected then they are removed from the league, which I do not want. If I copy the list of players (and remove from that) then the ones being used to create the games are not the original player objects

 public Player(int Id, String eMail, String forename, String lastname,League league) {
    this.msaId = Id;
    this.eMail = eMail;
    this.firstName = forename;
    this.lastName = lastname;
    this.league = league;
}

   public League(int Id, String leagueName, List<Player> players, List<Round> rounds, int numberOfPlayers, Tournament t) {
    this.Id = Id;
    this.name = leagueName;
    this.players = players;
    this.rounds = rounds;
    this.numberOfPlayers = numberOfPlayers;
    this.tournament = t;
}

  public Round(int Id, int numberOfPlayers, int roundNumber, League league) {
    this.Id = Id;
    this.roundType = roundType;
    this.roundNumber = roundNumber;
    this.league = league;
    this.currentRound = false;
    this.roundStarted = false;
}
    public Game(int Id, int gameNumber, int roundId, Player playerOne, Player playerTwo, int tableNumber, Round r) {
    this.Id = Id;
    this.gameNumber = gameNumber;
    this.roundId = roundId;
    this.playerOne = playerOne;
    this.playerTwo = playerTwo;
    this.tableNumber = tableNumber;
    this.playerOneScore = 0;
    this.playerTwoScore = 0;
    this.round = r;
}
3
  • We can't really help without seeing your player class, the the lists dealing with them. In general, one of your problems seems to be proper modelling: you see, it should really not matter to your player objects if they are in one, two, or twelve million lists. Each "player object" represents exactly one human being, and I guess you would agree: for that human being, it doesn't really matter if his names shows up in one, two or 5 different phone books. That one human being is still the same single person. Commented Jun 3, 2016 at 14:01
  • Ive added my classes involved Commented Jun 3, 2016 at 14:05
  • 1
    You could add an attribute to the Player class, isSelected as a boolean and when you use the random selection and the player.isSelected is true, pick another Player. Commented Jun 3, 2016 at 14:21

1 Answer 1

1

You could create an ArrayList with a List of players by that round. I am going to try to explain better myself with a piece of code.

private List<Player> leaguePlayers;
private Map<Round, List<Player>> playersByRound;

With this structure before to add a player to one Round, you can verify if the new player to add is into the list like this.

public boolean checkIsSelectablePlayerByRound(Player player, Round round){
    for(Game game : gamesByRound.get(round)){
        if(game.playerOne == player || game.playerTwo == player){
            return false;
        }
    }
    return true;
}

Hope this can help you

Sign up to request clarification or add additional context in comments.

6 Comments

So for each round you would add a list of players, duplicating the list?
@user2190867 for each round i would add all players who play some game, would be a reference to the original object, no need to duplicate, you only need have the reference of what players played on what round. If i understand wrongly please let me know
Sorry, I dont understand. My problem is that a game uses two players from the league. Similar to the premier league. How can I pair them all randomly, ensuring that one has not already been chosen for another game?
then to clarify you have a list of players, with all league players, and every game is a random selection of 11 players for each team right?
No sorry,my premier league example may have confused you. It is actually for creating a chess tournament. So a game is 1 v 1
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.