Created
November 27, 2013 16:52
-
-
Save jmoon90/7679099 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Implement your solution here. You may reference any additional files from here as well. | |
| TEAMS = (1..2).to_a | |
| def questioner(question) | |
| puts question + "?" | |
| input = gets.chomp | |
| end | |
| def team_checker(name, hash) | |
| hash.keys.include?(name) | |
| end | |
| def more_scores? | |
| input = questioner("Would you like to provide another game") | |
| if input.downcase.include?('y') | |
| false | |
| else | |
| true | |
| end | |
| end | |
| def team_getter | |
| team_names = {} | |
| TEAMS.each do |team_num| | |
| team_name = questioner("What was team #{team_num}'s name") | |
| if team_checker(team_name, team_names) | |
| puts "That name already exists" | |
| redo | |
| else | |
| team_names[team_num] = {} | |
| team_names[team_num] = {team_name: team_name, | |
| team_score: 0} | |
| team_names[team_num] = score_getter(team_num, team_names) | |
| end | |
| end | |
| team_names | |
| end | |
| def score_getter(team_num, hash) | |
| hash[team_num][:team_score]+= questioner("What was team #{team_num}'s score").to_i | |
| hash[team_num] | |
| end | |
| scores = [] | |
| while true | |
| scores << team_getter | |
| break if more_scores? | |
| end | |
| @standings = {} | |
| scores.each_with_index do |game, index| | |
| game.each do |key, value| | |
| @standings[value[:team_name]] = {wins: 0, losses: 0, game: 0, team_name: value[:team_name]} | |
| end | |
| end | |
| scores.each_with_index do |game, index| | |
| game.each do |key, value| | |
| game = game.sort_by{|key, value| value[:team_score]}.reverse | |
| @winner = game.first | |
| @loser = game.last | |
| # print "In game #{index + 1}, " | |
| # puts "#{@winner.last[:team_name]} is the victor" | |
| index += 1 | |
| end | |
| @standings[@winner.last[:team_name]][:wins] += 1 | |
| @standings[@loser.last[:team_name]][:losses] += 1 | |
| @standings[@winner.last[:team_name]][:game] += 1 | |
| @standings[@loser.last[:team_name]][:game] += 1 | |
| print "In game #{index -1}, " | |
| puts "#{@winner.last[:team_name]} is the victor" | |
| end | |
| @standings = @standings.sort_by{|key, value| value[:wins]}.reverse | |
| puts "" | |
| puts "========League Standings =======" | |
| rank = 1 | |
| @standings.each do |key, value| | |
| value[:win_percent] = value[:wins] / value[:game] * 100 | |
| value[:rank] = rank | |
| print "#{key}: " | |
| puts "#{value[:wins]}W, #{value[:losses]}L" | |
| rank += 1 | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment