Class: StudioGame::Game
- Inherits:
-
Object
- Object
- StudioGame::Game
- Defined in:
- lib/studio_game/game.rb
Instance Attribute Summary collapse
-
#game_name ⇒ Object
readonly
Returns the value of attribute game_name.
Instance Method Summary collapse
- #add_player(player) ⇒ Object
- #high_score_entry(player) ⇒ Object
-
#initialize(name) ⇒ Game
constructor
A new instance of Game.
- #load_players(filename) ⇒ Object
- #play(rounds, game) ⇒ Object
- #save_high_scores(filename = "high_scores.txt") ⇒ Object
- #statistics ⇒ Object
- #total_points ⇒ Object
Constructor Details
#initialize(name) ⇒ Game
Returns a new instance of Game.
11 12 13 14 |
# File 'lib/studio_game/game.rb', line 11 def initialize(name) @game_name = name @players = [] end |
Instance Attribute Details
#game_name ⇒ Object (readonly)
Returns the value of attribute game_name.
9 10 11 |
# File 'lib/studio_game/game.rb', line 9 def game_name @game_name end |
Instance Method Details
#add_player(player) ⇒ Object
16 17 18 |
# File 'lib/studio_game/game.rb', line 16 def add_player(player) @players << player end |
#high_score_entry(player) ⇒ Object
61 62 63 64 |
# File 'lib/studio_game/game.rb', line 61 def high_score_entry(player) formatted_name = player.player_name.ljust(20, '.') "#{formatted_name} #{player.score}" end |
#load_players(filename) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/studio_game/game.rb', line 54 def load_players(filename) CSV.foreach(filename) do |row| player = Player.new(row[0], row[1].to_i) add_player(player) end end |
#play(rounds, game) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/studio_game/game.rb', line 75 def play(rounds, game) if @players.length() > 1 puts "There are #{@players.size} players in #{@game_name}" else puts "There is #{@players.size} player in #{@game_name}" end treasures = TreasureTrove::TREASURES puts "\nThere are #{treasures.size} treasures in #{@game_name}:" treasures.each do |treasure| puts "- A #{treasure.name} is worth #{treasure.points} points" end 1.upto(rounds) do |round| #check to see if block was given > if yes > break the loop and exit game if block_given? break if yield end puts "\nRound #{round}" @players.each do |player| GameTurn.take_turn(player) end end end |
#save_high_scores(filename = "high_scores.txt") ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/studio_game/game.rb', line 66 def save_high_scores(filename="high_scores.txt") File.open(filename, "w") do |file| file.puts "#{@game_name} High Scores: " @players.sort.each do |player| file.puts high_score_entry(player) end end end |
#statistics ⇒ Object
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 |
# File 'lib/studio_game/game.rb', line 24 def statistics strong, wimpy = @players.partition { |player| player.strong? } puts "\n#{ @game_name } statistics: \n - #{ strong.size } strong player(s): " strong.each do |strong_player| GameTurn.print_name_and_health(strong_player) end puts "\n - #{ wimpy.size } wimpy player(s): " wimpy.each do |wimpy_player| GameTurn.print_name_and_health(wimpy_player) end @players.each do |player| puts "\n#{player.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" end puts "\n#{ @game_name} High Scores: " @players.sort.each do |player| puts high_score_entry(player) end puts "\n#{total_points} total points from treasures found \nWell played!!" end |
#total_points ⇒ Object
20 21 22 |
# File 'lib/studio_game/game.rb', line 20 def total_points @players.reduce(0) { |sum, player| sum + player.points} end |