Im trying to create a random team generator based on the user's input of names and the number of teams evenly. Similar to this https://www.jamestease.co.uk/team-generator/
So far i've .split
and .shuffle
the string of input into an array of names
, but unsure how to proceed further.
names = gets.split(",").shuffle
names = ["Aaron", "Nick", "Ben", "Bob", "Ted"]
For Example:
lets say i want to have 2 teams (names do not have to be in any particular order/team):
team_1 = ["Nick", "Bob"]
team_2 = ["Aaron", "Ben", "Ted"]
Any help or tips would be greatly appreciated
names = ["Aaron", "Nick", "Ben", "Bob", "Ted", 'shiva', 'hari', 'subash']
number_of_teams = 4
players_per_team = (names.count / number_of_teams.to_f).ceil
teams = []
(1..number_of_teams).each do |num|
teams[num - 1] = names.sample(players_per_team)
names = names - teams[num - 1]
end
> p teams
=> [["hari", "Ben"], ["Bob", "subash"], ["shiva", "Ted"], ["Nick", "Aaron"]]
and if
names = ["Aaron", "Nick", "Ben", "Bob", "Ted", 'hari', 'subash']
then
> p teams
[["hari", "subash"], ["Bob", "Aaron"], ["Ben", "Nick"], ["Ted"]]
Note: this will result random players every time you shuffle
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With