Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

generator with Ruby

Tags:

ruby

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

like image 475
Tim Avatar asked Oct 18 '22 23:10

Tim


1 Answers

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

like image 98
illusionist Avatar answered Oct 27 '22 11:10

illusionist