Created
February 27, 2013 18:52
-
-
Save jordanpoulton/5050510 to your computer and use it in GitHub Desktop.
Give it a few weeks and we'll be selling this to Premier League clubs ;)
This file contains 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
#!/usr/bin/env ruby | |
class Manager | |
def initialize | |
puts "Manager initialised" | |
end | |
def pick_a_team(squad, d, m, a) | |
team = Team.new | |
team << squad.best_player(:goalkeeper) | |
d.times do |i| | |
team << squad.best_player(:defender) | |
end | |
m.times do |i| | |
team << squad.best_player(:midfielder) | |
end | |
a.times do |i| | |
team << squad.best_player(:attacker) | |
end | |
team | |
end | |
end | |
class Coach | |
def calculate_form(player) | |
player.form = player.skill * (rand(10) +1) | |
end | |
end | |
class Player | |
attr_accessor :form | |
attr_reader :skill, :position | |
def initialize(name, position, skill, injured=false) | |
@name, @position, @skill, @injured = name, position, skill, injured | |
end | |
def to_s | |
#player_array = [@name, @position, @skill, @injured] | |
"My name is #{@name}, my position is #{@position}, my skill level is #{@skill} and I am injured? #{@injured} and my form is #{@form}" | |
end | |
end | |
class Team | |
attr_reader :players | |
def initialize | |
@players = [] | |
end | |
def <<(player) # how to call it: team << player | |
# raise an error if we can't add the player, | |
# e.g. if we already have the position filled | |
# raise "Can't add #{position}" | |
@players << player | |
end | |
def to_s | |
"I'm playing as #{position} because my form is #{form}" | |
end | |
end | |
class Squad | |
attr_reader :players | |
def initialize | |
@players = [] | |
@coach = Coach.new | |
end | |
def <<(player) | |
@coach.calculate_form(player) | |
@players << player | |
end | |
def best_player(position) | |
player = @players.select{|p| p.position == position}.sort_by{|p| p.form}.last | |
@players.delete_if {|x| x == player} # remove the player from @players | |
# what if player.nil? maybe raise an exception | |
player | |
end | |
end | |
def squad_with_players | |
squad = Squad.new | |
2.times do |i| | |
squad << Player.new("Goalkeeper #{i}", :goalkeeper, rand(10) + 1, false) | |
end | |
8.times do |i| | |
squad << Player.new("Defender #{i}", :defender, rand(10) + 1, false) | |
end | |
8.times do |i| | |
squad << Player.new("Midfielder #{i}", :midfielder, rand(10) + 1, false) | |
end | |
4.times do |i| | |
squad << Player.new("Attacker #{i}", :attacker, rand(10) + 1, false) | |
end | |
squad | |
end | |
first_squad = squad_with_players | |
puts "***************************** Assigned 22 players to the squad" | |
puts "here are the details of your squad" | |
first_squad.players.each {|player| puts player} | |
puts "******Initialising coach" | |
puts "Initialising a new manager" | |
manager = Manager.new() | |
puts "And now to pick the team" | |
team = manager.pick_a_team(first_squad, 2,7, 0) | |
puts " | |
Our chosen team:" | |
team.players.each {|player| puts player.to_s} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment