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.
Instance Method Summary collapse
- #add_player(player) ⇒ Object
-
#initialize(title) ⇒ Game
constructor
A new instance of Game.
- #load_players(from_file) ⇒ Object
- #play(rounds) ⇒ Object
- #print_players(players) ⇒ Object
- #print_stats ⇒ Object
- #save_high_scores(to_file = 'high_scores.txt') ⇒ Object
- #total_points ⇒ Object
Constructor Details
#initialize(title) ⇒ Game
Returns a new instance of Game.
9 10 11 12 |
# File 'lib/studio_game/game.rb', line 9 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 |
Instance Method Details
#add_player(player) ⇒ Object
14 15 16 |
# File 'lib/studio_game/game.rb', line 14 def add_player(player) @players << player end |
#load_players(from_file) ⇒ Object
18 19 20 21 22 |
# File 'lib/studio_game/game.rb', line 18 def load_players(from_file) File.foreach(from_file) do |line| @players << Player.from_csv(line) end end |
#play(rounds) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/studio_game/game.rb', line 46 def play(rounds) print_players(@players) treasures = TreasureTrove::TREASURES puts "\nThere are #{treasures.size} treasures to be found:\n" treasures.each do |treasure| puts "A #{treasure.name} is worth #{treasure.points} points" end puts "\n----" 1.upto(rounds) do |r| if block_given? break if yield end puts "\nRound: #{r}" @players.each_with_index do |player, index| GameTurn.take_turn(player) end end end |
#print_players(players) ⇒ Object
34 35 36 37 38 |
# File 'lib/studio_game/game.rb', line 34 def print_players(players) players.each do |player| puts "#{player.name} - #{player.health}" end end |
#print_stats ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/studio_game/game.rb', line 67 def print_stats puts "\n#{@title} Statistics:\n" strong,wimpy = @players.partition { |player| player.strong? } puts "\n#{@title} High Scores:" @players.sort.each do |player| puts player.to_pretty_s player.each_found_treasure do |treasure| puts "#{treasure.points} #{treasure.name} total points" end end puts "\n#{strong.length} strong player(s):\n" print_players(strong) puts "\n#{wimpy.length} wimpy player(s):\n" print_players(wimpy) grand_total_points = @players.reduce(0) do |total_points, player| total_points += player.points end puts "\n#{grand_total_points} total points found from treasures.\n\n" end |
#save_high_scores(to_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(to_file='high_scores.txt') File.open(to_file, 'w') do |file| file.puts "#{@title} High Scores:" @players.sort.each do |player| file.puts player.to_pretty_s end end end |
#total_points ⇒ Object
40 41 42 43 44 |
# File 'lib/studio_game/game.rb', line 40 def total_points total_points = @players.reduce(0) do |total_points, player| total_points += player.points end end |