Class: StudioGame::Game
- Inherits:
-
Object
- Object
- StudioGame::Game
- Defined in:
- lib/studio_game/game.rb
Instance Method Summary collapse
- #add_player(p) ⇒ Object
-
#initialize(name) ⇒ Game
constructor
A new instance of Game.
- #load_players(from_file) ⇒ Object
- #play(rounds) ⇒ Object
- #print_name_and_health(n) ⇒ Object
- #print_stats ⇒ Object
- #save_high_scores(to_file = "high_scores.txt") ⇒ Object
- #total_points ⇒ Object
Constructor Details
#initialize(name) ⇒ Game
Returns a new instance of Game.
12 13 14 15 |
# File 'lib/studio_game/game.rb', line 12 def initialize(name) @name = name @players =[] end |
Instance Method Details
#add_player(p) ⇒ Object
23 24 25 |
# File 'lib/studio_game/game.rb', line 23 def add_player(p) @players << p end |
#load_players(from_file) ⇒ Object
17 18 19 20 21 |
# File 'lib/studio_game/game.rb', line 17 def load_players(from_file) CSV.foreach(from_file) do |line| add_player( Player.new(line[0],Integer(line[1]) )) end end |
#play(rounds) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/studio_game/game.rb', line 38 def play(rounds) puts "There are #{@players.size} players in #{@name}" 1.upto(rounds) do |round| if block_given? break if yield end puts "Round #{round}\n" @players.each do |p| GameTurn.take_turn(p) puts p end puts "#{total_points}" end def print_name_and_health (n) "#{n.name} (#{n.health})\n" end def print_stats puts "#{@name} Statistics\n\n" strong,wimpy = @players.partition{ |p| p.strong? } puts "#{strong.size} strong players\n" strong.each { |n| puts print_name_and_health(n) } wimpy.each { |n| puts print_name_and_health(n) } puts "#{@name} High Scores:" @players.sort.each do |p| puts print_name_and_health(p) end @players.sort.each do |p| puts "\n#{p.name}'s point totals:" p.each_found_treasure do |treasure| puts "#{treasure.points} total #{treasure.name} points" end puts "#{p.points} grand total" end puts "#{total_points} total points from treasures found" end end |
#print_name_and_health(n) ⇒ Object
54 55 56 |
# File 'lib/studio_game/game.rb', line 54 def print_name_and_health (n) "#{n.name} (#{n.health})\n" end |
#print_stats ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/studio_game/game.rb', line 58 def print_stats puts "#{@name} Statistics\n\n" strong,wimpy = @players.partition{ |p| p.strong? } puts "#{strong.size} strong players\n" strong.each { |n| puts print_name_and_health(n) } wimpy.each { |n| puts print_name_and_health(n) } puts "#{@name} High Scores:" @players.sort.each do |p| puts print_name_and_health(p) end @players.sort.each do |p| puts "\n#{p.name}'s point totals:" p.each_found_treasure do |treasure| puts "#{treasure.points} total #{treasure.name} points" end puts "#{p.points} grand total" end puts "#{total_points} total points from treasures found" end |
#save_high_scores(to_file = "high_scores.txt") ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/studio_game/game.rb', line 30 def save_high_scores(to_file="high_scores.txt") File.open(to_file, 'w') do |file| file.puts("Knuckleheads High Scores:") @players.sort.each { |player| file.puts player.to_csv} end end |
#total_points ⇒ Object
26 27 28 |
# File 'lib/studio_game/game.rb', line 26 def total_points @players.reduce(0) { |sum,p| sum + p.points } end |