Class: StudioGame::Game
- Inherits:
-
Object
- Object
- StudioGame::Game
- Defined in:
- lib/studio_game/game.rb
Instance Attribute Summary collapse
-
#players ⇒ Object
readonly
Returns the value of attribute players.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Instance Method Summary collapse
- #add_player(player) ⇒ Object
-
#initialize(title) ⇒ Game
constructor
A new instance of Game.
- #load_players(filename) ⇒ Object
- #play(rounds) ⇒ Object
- #print_high_score(p) ⇒ Object
- #print_name_and_health(player) ⇒ Object
- #print_stats ⇒ Object
- #print_treasures ⇒ Object
- #save_high_scores(filename = "high_scores.txt") ⇒ Object
- #total_points ⇒ Object
Constructor Details
#initialize(title) ⇒ Game
Returns a new instance of Game.
11 12 13 14 |
# File 'lib/studio_game/game.rb', line 11 def initialize(title) @title = title.capitalize @players = Array.new end |
Instance Attribute Details
#players ⇒ Object (readonly)
Returns the value of attribute players.
9 10 11 |
# File 'lib/studio_game/game.rb', line 9 def players @players end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
9 10 11 |
# File 'lib/studio_game/game.rb', line 9 def title @title end |
Instance Method Details
#add_player(player) ⇒ Object
16 17 18 |
# File 'lib/studio_game/game.rb', line 16 def add_player(player) @players.push(player) end |
#load_players(filename) ⇒ Object
54 55 56 57 58 |
# File 'lib/studio_game/game.rb', line 54 def load_players(filename) File.readlines(filename).each do |line| add_player(Player.from_csv(line)) end end |
#play(rounds) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/studio_game/game.rb', line 20 def play(rounds) 1.upto(rounds) do |round| if block_given? break if yield end puts "There are #{@players.size} players in #{@title}." @players.each do |p| puts p end puts "\nRound #{round}:" @players.each do |p| GameTurn.take_turn(p) puts p end end end |
#print_high_score(p) ⇒ Object
40 41 42 |
# File 'lib/studio_game/game.rb', line 40 def print_high_score(p) "#{p.name.ljust(20, '.')} #{p.score}" end |
#print_name_and_health(player) ⇒ Object
60 61 62 |
# File 'lib/studio_game/game.rb', line 60 def print_name_and_health(player) puts "#{player.name} (#{player.health})" end |
#print_stats ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/studio_game/game.rb', line 64 def print_stats puts "\n#{@title} Statistics" @players.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 points" end strong, wimpy = @players.partition { |p| p.strong? } puts "\n#{strong.size} strong players:" strong.each do |s| print_name_and_health(s) end puts "\n#{wimpy.size} wimpy players:" wimpy.each do |w| print_name_and_health(w) end puts "\n#{@title} High Scores:" @players.sort.each do |p| puts print_high_score(p) end puts "\nTotal points found: #{total_points}" end |
#print_treasures ⇒ Object
98 99 100 101 102 103 104 |
# File 'lib/studio_game/game.rb', line 98 def print_treasures puts "There are #{TreasureTrove::TREASURES.size} treasures to be found:" TreasureTrove::TREASURES.each do |t| puts "A #{t.name} is worth #{t.points} points." end puts "\n" end |
#save_high_scores(filename = "high_scores.txt") ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/studio_game/game.rb', line 44 def save_high_scores(filename="high_scores.txt") File.open(filename, 'w') do |file| file.puts "#{@title} High Scores:" @players.sort.each do |p| file.puts print_high_score(p) end end end |
#total_points ⇒ Object
106 107 108 109 |
# File 'lib/studio_game/game.rb', line 106 def total_points #@players.reduce(0) { |sum, p1.points| sum + p1.points } @players.reduce(0) { |sum, player| sum + player.points } end |