Class: StudioGame::Game
- Inherits:
-
Object
- Object
- StudioGame::Game
- Includes:
- Auditable
- Defined in:
- lib/studio_game/game.rb
Overview
game class
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
- #high_score_entry(player) ⇒ Object
-
#initialize(title) ⇒ Game
constructor
A new instance of Game.
- #load_players(from_file) ⇒ Object
-
#play(rounds = 1) ⇒ Object
rubocop:disable Metrics/MethodLength,Metrics/AbcSize.
-
#print_stats ⇒ Object
rubocop:disable Metrics/MethodLength.
- #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.
12 13 14 15 |
# File 'lib/studio_game/game.rb', line 12 def initialize(title) @title = title @players = [] end |
Instance Attribute Details
#players ⇒ Object (readonly)
Returns the value of attribute players.
10 11 12 |
# File 'lib/studio_game/game.rb', line 10 def players @players end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
10 11 12 |
# File 'lib/studio_game/game.rb', line 10 def title @title end |
Instance Method Details
#add_player(player) ⇒ Object
17 18 19 |
# File 'lib/studio_game/game.rb', line 17 def add_player(player) @players.push(player) end |
#high_score_entry(player) ⇒ Object
102 103 104 105 106 |
# File 'lib/studio_game/game.rb', line 102 def high_score_entry(player) name = player.name.ljust(20, '.') points = player.score.round.to_s.rjust(5) "#{name}#{points}" end |
#load_players(from_file) ⇒ Object
83 84 85 86 87 88 89 90 91 |
# File 'lib/studio_game/game.rb', line 83 def load_players(from_file) File.readlines(from_file, chomp: true).each do |line| player = Player.from_csv(line) add_player(player) end rescue Errno::ENOENT puts "Looks like the #{from_file} file doesn't exist friend." exit 1 end |
#play(rounds = 1) ⇒ Object
rubocop:disable Metrics/MethodLength,Metrics/AbcSize
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/studio_game/game.rb', line 21 def play(rounds = 1) # rubocop:disable Metrics/MethodLength,Metrics/AbcSize puts "\nLet's play #{@title}" puts "\nThe following treasures can be found:" puts TreasureTrove.treasure_items puts puts "\nBefore playing:" puts @players 1.upto(rounds) do |round| puts "\nRound #{round}" @players.each do |player| number_rolled = roll_die case number_rolled when 1..2 player.drain puts "#{player.name} got drained 😩" when 3..4 puts "#{player.name} got skipped" else player.boost puts "#{player.name} got boosted 😁" end treasure_found = TreasureTrove.random_treasure player.found_treasure(treasure_found.name, treasure_found.points) puts "#{player.name} found a #{treasure_found.name} worth #{treasure_found.points}" end end end |
#print_stats ⇒ Object
rubocop:disable Metrics/MethodLength
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/studio_game/game.rb', line 61 def print_stats # rubocop:disable Metrics/MethodLength puts "\n#{@title} Game Stats:" puts '-' * 30 puts sorted_players @players.each do |player| puts "\n#{player.name}'s treasure points totals:" player.found_treasures.each do |treasure, points| puts "#{treasure}: #{points}" end end puts "\nHigh Scores:" sorted_players.each do |player| puts high_score_entry(player) end end |
#roll_die ⇒ Object
55 56 57 58 59 |
# File 'lib/studio_game/game.rb', line 55 def roll_die number = rand(1..6) audit(number) number end |
#save_high_scores(to_file = 'high_scores_txt') ⇒ Object
93 94 95 96 97 98 99 100 |
# File 'lib/studio_game/game.rb', line 93 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
79 80 81 |
# File 'lib/studio_game/game.rb', line 79 def sorted_players @players.sort_by { |player| player.score }.reverse # rubocop:disable Style/SymbolProc end |