Class: StudioGame::Game
- Inherits:
-
Object
- Object
- StudioGame::Game
- Defined in:
- lib/studio_game/game.rb
Instance Attribute Summary collapse
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Instance Method Summary collapse
- #add_player(player) ⇒ Object
- #high_score_entry(player) ⇒ Object
-
#initialize(title) ⇒ Game
constructor
A new instance of Game.
- #load_players(from_file) ⇒ Object
- #play(rounds) ⇒ Object
- #print_name_and_health(player) ⇒ Object
- #print_stats ⇒ Object
- #save_high_scores(to_file = "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 @players = [] end |
Instance Attribute Details
#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
22 23 24 |
# File 'lib/studio_game/game.rb', line 22 def add_player(player) @players << player end |
#high_score_entry(player) ⇒ Object
68 69 70 |
# File 'lib/studio_game/game.rb', line 68 def high_score_entry(player) "#{player.name.ljust(20, '.')} #{player.score}" end |
#load_players(from_file) ⇒ Object
16 17 18 19 20 |
# File 'lib/studio_game/game.rb', line 16 def load_players(from_file) File.readlines(from_file).each do |line| add_player(Player.from_csv(line)) end end |
#play(rounds) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/studio_game/game.rb', line 26 def play(rounds) puts "There are #{@players.size} players in #{@title}" puts @players treasures = TreasureTrove::TREASURES puts "\nThere are #{treasures.size} treasures to be found:" treasures.each do |treasure| puts "A #{treasure.name} is worth #{treasure.points} points" end 1.upto(rounds) do |round| puts "\nRound #{round}:" @players.each do |p| GameTurn.take_turn(p) puts p end if block_given? break if yield end end end |
#print_name_and_health(player) ⇒ Object
55 56 57 |
# File 'lib/studio_game/game.rb', line 55 def print_name_and_health(player) puts "\n#{player.name} (#{player.health})" end |
#print_stats ⇒ Object
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 97 98 99 100 101 102 103 104 |
# File 'lib/studio_game/game.rb', line 72 def print_stats strong, wimpy = @players.partition { |p| p.strong? } puts "\n#{@title} Statistics:" if strong.size > 0 puts "\n#{strong.size} strong players:" strong.each do |p| print_name_and_health(p) end end if wimpy.size > 0 puts "\n#{wimpy.size} wimpy players:" wimpy.each do |p| print_name_and_health(p) end end puts "\n#{title} High Scores:\n" @players.sort.each do |p| puts high_score_entry(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 points" end puts "#{total_points} total points from treasures found" # "Knuckleheads Statistics:\n\n1 wimpy player:\nMoe (100)" end |
#save_high_scores(to_file = "high_scores.txt") ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/studio_game/game.rb', line 59 def save_high_scores(to_file="high_scores.txt") File.open(to_file, "w") do |file| file.puts "#{@title} High Scores:" @players.sort.each do |p| file.puts high_score_entry(p) end end end |
#total_points ⇒ Object
49 50 51 52 53 |
# File 'lib/studio_game/game.rb', line 49 def total_points @players.reduce(0) do |sum, player| sum + player.points end end |