Class: StudioGame::Game
- Inherits:
-
Object
- Object
- StudioGame::Game
- Defined in:
- lib/studio_game/game.rb
Instance Method Summary collapse
- #add_player(player) ⇒ Object
- #high_score_entry(player) ⇒ Object
-
#initialize(name) ⇒ Game
constructor
A new instance of Game.
- #list_scores ⇒ Object
- #load_players(from_file) ⇒ Object
- #play(rounds) ⇒ Object
- #print_stats ⇒ Object
- #save_high_scores(to_file = 'high_scores.txt') ⇒ Object
- #total_points ⇒ Object
- #w00t_blam ⇒ Object
Constructor Details
#initialize(name) ⇒ Game
Returns a new instance of Game.
7 8 9 10 |
# File 'lib/studio_game/game.rb', line 7 def initialize(name) @name = name @players = [] end |
Instance Method Details
#add_player(player) ⇒ Object
18 19 20 |
# File 'lib/studio_game/game.rb', line 18 def add_player(player) @players.push(player) end |
#high_score_entry(player) ⇒ Object
82 83 84 |
# File 'lib/studio_game/game.rb', line 82 def high_score_entry(player) "#{player.name.ljust(20, '.')} #{player.score}" end |
#list_scores ⇒ Object
41 42 43 |
# File 'lib/studio_game/game.rb', line 41 def list_scores @players.each { |p| puts p } end |
#load_players(from_file) ⇒ Object
12 13 14 15 16 |
# File 'lib/studio_game/game.rb', line 12 def load_players(from_file) File.readlines(from_file).each do |line| add_player(Player.from_csv(line)) end end |
#play(rounds) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/studio_game/game.rb', line 22 def play(rounds) puts "There are #{TreasureTrove::TREASURES.size} treasures to be found:" TreasureTrove::TREASURES.each { |t| puts "A #{t.name} is worth #{t.points} points" } puts "\n" puts "There are #{@players.size} players in #{@name}:" 1.upto(rounds) do |round| if block_given? && yield break end puts "Round #{round}:" @players.each do |player| GameTurn.take_turn(player) end puts "\n" end print_stats puts "#{total_points} total points from treasures found" end |
#print_stats ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/studio_game/game.rb', line 53 def print_stats puts "#{@name} Statistics:" strong, wimpy = @players.partition { |p| p.strong? } puts "#{strong.count} strong players:" strong.each { |p| puts "#{p.name} (#{p.score})" } puts "#{wimpy.count} wimpy players:" wimpy.each { |p| puts "#{p.name} (#{p.score})" } puts " " puts "#{@name} High Scores:" @players.sort.each { |p| puts high_score_entry(p)} puts " " @players.each do |player| puts "#{player.name}'s points totals:" player.each_found_treasure do |treasure| puts "#{treasure.points} total #{treasure.name} points" end puts "#{player.points} grand total points\n " end end |
#save_high_scores(to_file = 'high_scores.txt') ⇒ Object
73 74 75 76 77 78 79 80 |
# File 'lib/studio_game/game.rb', line 73 def save_high_scores(to_file='high_scores.txt') File.open(to_file, 'w') do |file| file.puts "#{@name} High Scores:" @players.sort.each do |player| file.puts high_score_entry(player) end end end |
#total_points ⇒ Object
49 50 51 |
# File 'lib/studio_game/game.rb', line 49 def total_points @players.reduce(0) { |sum, player| sum + player.points } end |
#w00t_blam ⇒ Object
45 46 47 |
# File 'lib/studio_game/game.rb', line 45 def w00t_blam @players.each { |p| p.w00t; p.blam } end |