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.
Class Method Summary collapse
Instance Method Summary collapse
- #add_player(player) ⇒ Object
-
#initialize(title) ⇒ Game
constructor
A new instance of Game.
- #load_players(load_file) ⇒ Object
- #play(rounds) ⇒ Object
- #player_name_score(player) ⇒ Object
- #print_name_and_health(player) ⇒ Object
- #print_stats ⇒ Object
- #save_high_scores(save_file = "high_scores.txt") ⇒ Object
- #total_points ⇒ 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 @players = [] end |
Instance Attribute Details
#title ⇒ Object (readonly)
Returns the value of attribute title.
8 9 10 |
# File 'lib/studio_game/game.rb', line 8 def title @title end |
Class Method Details
.from_csv(line) ⇒ Object
15 16 17 18 |
# File 'lib/studio_game/game.rb', line 15 def self.from_csv(line) name, health = line.split(',') Player.new(name, Integer(health)) end |
Instance Method Details
#add_player(player) ⇒ Object
35 36 37 |
# File 'lib/studio_game/game.rb', line 35 def add_player(player) @players << player end |
#load_players(load_file) ⇒ Object
19 20 21 22 23 |
# File 'lib/studio_game/game.rb', line 19 def load_players(load_file) File.readlines(load_file).each do |line| add_player(Player.from_csv(line)) end end |
#play(rounds) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/studio_game/game.rb', line 39 def play(rounds) treasures = TreasureTrove::TREASURES puts "There are #{@players.size} players in #{@title}" puts "There are #{treasures.size} treasures to be found:" treasures.each do |treasure| puts "A #{treasure.name} is worth #{treasure.points} points" end puts @players 1.upto(rounds) do |r| if block_given? break if yield end puts "\nround number #{r}\n" @players.each do |player| GameTurn.take_turn(player) puts player end end end |
#player_name_score(player) ⇒ Object
32 33 34 |
# File 'lib/studio_game/game.rb', line 32 def player_name_score(player) "#{player.name.ljust(20, '.')} #{player.score}" end |
#print_name_and_health(player) ⇒ Object
67 68 69 |
# File 'lib/studio_game/game.rb', line 67 def print_name_and_health (player) puts "#{player.name} (#{player.health})" end |
#print_stats ⇒ Object
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 71 def print_stats strong, weak = @players.partition {|p| p.strong?} puts "\n#{@title} Statistics:\n" puts "#{strong.size} strong players:" strong.each do |player| print_name_and_health (player) end puts "#{weak.size} weak players:" weak.each do |player| print_name_and_health (player) end @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\n" end puts"\n#{@title} High Scores" @players.sort.each do |player| puts player_name_score(player) end puts "Total treasure points found #{total_points}" end |
#save_high_scores(save_file = "high_scores.txt") ⇒ Object
24 25 26 27 28 29 30 31 |
# File 'lib/studio_game/game.rb', line 24 def save_high_scores(save_file="high_scores.txt") File.open(save_file, "w") do |file| file.puts "#{@title} High Scores:" @players.sort.each do |player| file.puts player_name_score(player) end end end |
#total_points ⇒ Object
62 63 64 65 66 |
# File 'lib/studio_game/game.rb', line 62 def total_points @players.reduce(0) do |sum, player| sum + player.points end end |