Class: StudioGame::Game
- Inherits:
-
Object
- Object
- StudioGame::Game
- Defined in:
- lib/studio_game/game.rb
Instance Method Summary collapse
- #add_player(player_name) ⇒ Object
- #high_score_entry(player) ⇒ Object
-
#initialize(title) ⇒ Game
constructor
A new instance of Game.
- #load_players(file) ⇒ Object
- #play(rounds) ⇒ Object
- #print_score ⇒ Object
- #print_stats ⇒ Object
- #save_high_scores(output_file = "high_scores.txt") ⇒ Object
- #strong_stats(strong_players) ⇒ Object
- #total_treasure_points ⇒ Object
- #wimpy_stats(wimpy_players) ⇒ Object
Constructor Details
#initialize(title) ⇒ Game
Returns a new instance of Game.
10 11 12 13 |
# File 'lib/studio_game/game.rb', line 10 def initialize(title) @title = title.capitalize @players = [] end |
Instance Method Details
#add_player(player_name) ⇒ Object
35 36 37 |
# File 'lib/studio_game/game.rb', line 35 def add_player(player_name) @players << player_name end |
#high_score_entry(player) ⇒ Object
15 16 17 |
# File 'lib/studio_game/game.rb', line 15 def high_score_entry(player) "#{player.name.ljust(20, '.')} #{player.score}" end |
#load_players(file) ⇒ Object
20 21 22 23 24 |
# File 'lib/studio_game/game.rb', line 20 def load_players(file) CSV.foreach(file) do |row| add_player(Player.new(row[0], row[1].to_i)) end end |
#play(rounds) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/studio_game/game.rb', line 82 def play(rounds) TreasureTrove.list puts "\nThere are #{@players.size} players in #{@title}." @players.each do |p| puts p end 1.upto(rounds) do |count| puts "\nRound #{count}" @players.each do |p| GameTurn.take_turn(p) puts p end end end |
#print_score ⇒ Object
39 40 41 42 43 44 |
# File 'lib/studio_game/game.rb', line 39 def print_score puts "\n#{@title} high scores:" @players.sort.each do |player| puts high_score_entry(player) end end |
#print_stats ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/studio_game/game.rb', line 66 def print_stats puts "\n#{@title}'s totals:" puts "#{total_treasure_points} total points from treasures found." @players.each do |player| puts "\n#{player.name}'s point totals:" player.each_found_treasure do |treasure| puts "#{treasure.points} total #{treasure.name} points." end puts "#{player.points} GRAND TOTAL POINTS." end puts "\n#{@title} statistics:" strong_players, wimpy_players = @players.partition { |player| player.strong? } strong_stats(strong_players) wimpy_stats(wimpy_players) end |
#save_high_scores(output_file = "high_scores.txt") ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/studio_game/game.rb', line 26 def save_high_scores(output_file="high_scores.txt") File.open(output_file, "w") do |file| file.puts "#{@title} High Scores:" @players.sort.each do |player| file.puts high_score_entry(player) end end end |
#strong_stats(strong_players) ⇒ Object
46 47 48 49 50 51 |
# File 'lib/studio_game/game.rb', line 46 def strong_stats(strong_players) puts "\n#{strong_players.size} strong players:" strong_players.each do |player| puts "#{player.name} (#{player.health})" end end |
#total_treasure_points ⇒ Object
60 61 62 63 64 |
# File 'lib/studio_game/game.rb', line 60 def total_treasure_points @players.reduce(0) do |sum, player| sum + player.points end end |
#wimpy_stats(wimpy_players) ⇒ Object
53 54 55 56 57 58 |
# File 'lib/studio_game/game.rb', line 53 def wimpy_stats(wimpy_players) puts "\n#{wimpy_players.size} wimpy players:" wimpy_players.each do |player| puts "#{player.name} (#{player.health})" end end |