Class: StudioGame::Game
- Inherits:
-
Object
- Object
- StudioGame::Game
- Includes:
- Auditable
- Defined in:
- lib/studio_game/game.rb
Overview
Provides methods for working with the Game object
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(from_file) ⇒ Object
rubocop:enable Metrics/AbcSize.
- #play(rounds = 1) ⇒ Object
-
#print_stats ⇒ Object
rubocop:disable Metrics/AbcSize.
- #roll_die ⇒ Object
- #save_high_scores(to_file = 'high_scores.txt') ⇒ Object
- #sorted_players ⇒ Object
Methods included from Auditable
Constructor Details
#initialize(title) ⇒ Game
Returns a new instance of Game.
14 15 16 17 |
# File 'lib/studio_game/game.rb', line 14 def initialize(title) @title = title @players = [] end |
Instance Attribute Details
#players ⇒ Object (readonly)
Returns the value of attribute players.
12 13 14 |
# File 'lib/studio_game/game.rb', line 12 def players @players end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
12 13 14 |
# File 'lib/studio_game/game.rb', line 12 def title @title end |
Instance Method Details
#add_player(player) ⇒ Object
19 20 21 |
# File 'lib/studio_game/game.rb', line 19 def add_player(player) @players.push(player) end |
#load_players(from_file) ⇒ Object
rubocop:enable Metrics/AbcSize
65 66 67 68 69 70 71 72 73 |
# File 'lib/studio_game/game.rb', line 65 def load_players(from_file) CSV.foreach(from_file) do |row| player = Player.from_csv(row) add_player(player) end rescue Errno::ENOENT puts "Whoops, #{from_file} not found!" exit 1 end |
#play(rounds = 1) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/studio_game/game.rb', line 29 def play(rounds = 1) play_rounds(rounds) end |
#print_stats ⇒ Object
rubocop:disable Metrics/AbcSize
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/studio_game/game.rb', line 44 def print_stats puts "\n Game Stats:" puts '-' * 30 puts sorted_players @players.each do |player| puts "\n#{player.name}'s treasure point totals:" player.found_treasures.each do |name, points| puts "#{name}: #{points}" end puts "total: #{player.points}" end puts "\nHigh Scores:" sorted_players.each do |player| puts high_score_entry(player) end end |
#roll_die ⇒ Object
23 24 25 26 27 |
# File 'lib/studio_game/game.rb', line 23 def roll_die number = [1, 1, 2, 5, 6, 6].sample audit(number) number end |
#save_high_scores(to_file = 'high_scores.txt') ⇒ Object
75 76 77 78 79 80 81 82 |
# File 'lib/studio_game/game.rb', line 75 def save_high_scores(to_file = 'high_scores.txt') File.open(to_file, 'w') do |file| file.puts "#{@title} High Scores:" sorted_players.each do |player| file.puts high_score_entry(player) end end end |
#sorted_players ⇒ Object
39 40 41 |
# File 'lib/studio_game/game.rb', line 39 def sorted_players @players.sort_by(&:score).reverse end |