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
-
#initialize(title) ⇒ Game
constructor
A new instance of Game.
- #load_players(file_name) ⇒ Object
- #play(rounds) ⇒ Object
- #print_name_and_health(player) ⇒ Object
- #print_stats ⇒ Object
- #print_strong_or_wimpy_count(type, player_obj) ⇒ Object
- #save_high_scores(file_name = "high_scores.txt") ⇒ Object
- #total_points ⇒ Object
Constructor Details
#initialize(title) ⇒ Game
Returns a new instance of Game.
9 10 11 12 |
# File 'lib/studio_game/game.rb', line 9 def initialize(title) @title = title @players = [] end |
Instance Attribute Details
#title ⇒ Object (readonly)
Returns the value of attribute title.
7 8 9 |
# File 'lib/studio_game/game.rb', line 7 def title @title end |
Instance Method Details
#add_player(player) ⇒ Object
14 15 16 |
# File 'lib/studio_game/game.rb', line 14 def add_player(player) @players << player end |
#load_players(file_name) ⇒ Object
31 32 33 34 35 |
# File 'lib/studio_game/game.rb', line 31 def load_players(file_name) File.readlines(file_name).each do |line| add_player(Player.from_csv(line)) end end |
#play(rounds) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/studio_game/game.rb', line 37 def play(rounds) treasures = TreasureTrove::TREASURES puts "There are #{@players.size} players in #{title}:" puts "\nThere are #{treasures.count} treasure to be found:" treasures.each do |treasure| puts "A #{treasure.name} is worth #{treasure.points} points" end @players.each do |p| puts p end 1.upto(rounds) do |round| puts "\nRound #{round}:" @players.each do |p| GameTurn.take_turn(p) puts p end end end |
#print_name_and_health(player) ⇒ Object
58 59 60 |
# File 'lib/studio_game/game.rb', line 58 def print_name_and_health(player) puts "#{player.name} (#{player.health})" end |
#print_stats ⇒ Object
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 97 |
# File 'lib/studio_game/game.rb', line 66 def print_stats strong, wimpy = @players.partition { |p| p.strong? } #another way to do this #strong_players = @players.select { |player| player.strong? } #wimpy_players = @players.reject { |player| player.strong? } puts "\n#{@title} Statistics:\n\n" print_strong_or_wimpy_count("strong", strong) strong.each do |p| print_name_and_health(p) end print_strong_or_wimpy_count("wimpy", wimpy) wimpy.each do |p| print_name_and_health(p) end puts "\n#{@title} High Scores:\n" @players.sort.each do |p| puts p.print_high_score end @players.each do |p| puts "#{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\n" end puts "#{total_points} total points from treasures found." end |
#print_strong_or_wimpy_count(type, player_obj) ⇒ Object
62 63 64 |
# File 'lib/studio_game/game.rb', line 62 def print_strong_or_wimpy_count(type, player_obj) puts "#{player_obj.count} #{type} players:\n" end |
#save_high_scores(file_name = "high_scores.txt") ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/studio_game/game.rb', line 18 def save_high_scores(file_name="high_scores.txt") File.open(file_name, "w") do |file| file.puts "#{@title} High Scores:" @players.sort.each do |p| file.puts p.print_high_score end end end |
#total_points ⇒ Object
27 28 29 |
# File 'lib/studio_game/game.rb', line 27 def total_points @players.reduce(0) { |sum, player| sum + player.points } end |