Class: StudioGame::Game
- Inherits:
-
Object
- Object
- StudioGame::Game
- Includes:
- Auditable
- Defined in:
- lib/studio_game/game.rb
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
- #print_stats ⇒ Object
- #roll_die ⇒ Object
- #save_high_scores(to_file = "high_scores.csv") ⇒ Object
- #sorted_players ⇒ Object
Methods included from Auditable
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
#players ⇒ Object (readonly)
Returns the value of attribute players.
8 9 10 |
# File 'lib/studio_game/game.rb', line 8 def players @players end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
8 9 10 |
# File 'lib/studio_game/game.rb', line 8 def title @title end |
Instance Method Details
#add_player(player) ⇒ Object
39 40 41 |
# File 'lib/studio_game/game.rb', line 39 def add_player(player) @players << player end |
#high_score_entry(player) ⇒ Object
35 36 37 |
# File 'lib/studio_game/game.rb', line 35 def high_score_entry(player) "#{player.name.ljust(20, ".")}#{player.score}" end |
#load_players(from_file) ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/studio_game/game.rb', line 15 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 print "Whoops, #{from_file} not found\n" exit 1 end |
#play(rounds = 1) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/studio_game/game.rb', line 49 def play(rounds = 1) puts "*" * 30 puts "Lets Play #{@title}!\n" puts "\nThe following treasures can be found:" puts TreasureTrove.treasure_items puts "-" * 20 puts @players 1.upto(rounds) do |round| puts "\nRound #{round}:" @players.each do |player| number_rolled = roll_die case number_rolled when 1..3 player.drain puts " #{player.name} got drained 😩" when 4..6 puts " #{player.name} got no effect 😐" else player.boost puts " #{player.name} got boosted 😁" end treasure = TreasureTrove.random_treasure player.found_treasure(treasure.name, treasure.points) puts " #{player.name} found a treasure: [#{treasure.name}] worth #{treasure.points} points" end end end |
#print_stats ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/studio_game/game.rb', line 86 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
43 44 45 46 47 |
# File 'lib/studio_game/game.rb', line 43 def roll_die number = rand(1..10) audit(number) number end |
#save_high_scores(to_file = "high_scores.csv") ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/studio_game/game.rb', line 25 def save_high_scores(to_file = "high_scores.csv") 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
82 83 84 |
# File 'lib/studio_game/game.rb', line 82 def sorted_players @players.sort_by { |player| player.score }.reverse end |