class StudioGame::Game
Attributes
name[R]
Public Class Methods
new(name)
click to toggle source
# File lib/studio_game/game.rb, line 9 def initialize(name) @name = name @players = [] end
Public Instance Methods
add_player(player)
click to toggle source
# File lib/studio_game/game.rb, line 14 def add_player(player) @players << player end
high_score_entry(player)
click to toggle source
# File lib/studio_game/game.rb, line 69 def high_score_entry(player) "#{player.name.ljust(20,".")} #{player.score}" end
load_players(from_file)
click to toggle source
# File lib/studio_game/game.rb, line 18 def load_players(from_file) File.readlines(from_file).each do |line| add_player(Player.from_csv(line)) end end
play(rounds) { || ... }
click to toggle source
# File lib/studio_game/game.rb, line 24 def play(rounds) puts "There are #{@players.count} players in #{name}:" puts @players puts "There are #{TreasureTrove::TREASURES.size} treasures to be found." TreasureTrove::TREASURES.each { |treasure| puts "A #{treasure.name} is worth #{treasure.points} points"} 1.upto(rounds) do |round_number| if block_given? if yield break end end puts "\nRound #{round_number}:" @players.each { |player| GameTurn.take_turn(player) puts player } end end
print_name_and_health(player)
click to toggle source
# File lib/studio_game/game.rb, line 44 def print_name_and_health(player) puts "#{player.name} (#{player.health})" end
print_stats()
click to toggle source
# File lib/studio_game/game.rb, line 48 def print_stats puts "\n#{@name} High Scores:" @players.sort.each { |player| puts high_score_entry(player) } strongs, wimpys = @players.partition { |player| player.strong? } puts "\n#{@name} Statistics:" puts "\n#{strongs.size} strong players:" strongs.sort.each { |strong| print_name_and_health(strong) } puts "\n#{wimpys.size} wimpy players:" wimpys.sort.each { |wimpy| print_name_and_health(wimpy) } @players.sort.each { |player| puts "\n#{player.name}'s treasure point totals:" player.each_found_treasure { |treasure| puts "#{treasure.points} total #{treasure.name} points" } puts "#{player.points} grand total treasure points" } puts "\n#{total_points} total points from treasures found" end
save_high_scores()
click to toggle source
# File lib/studio_game/game.rb, line 73 def save_high_scores to_file = File.join(File.dirname(File.dirname(File.dirname(__FILE__))), "high_scores.txt") File.open(to_file, "w") do |file| file.puts "\n#{@name} High Scores:" @players.sort.each { |player| file.puts high_score_entry(player) } end end
total_points()
click to toggle source
# File lib/studio_game/game.rb, line 65 def total_points @players.reduce(0) { |total, player| total += player.points } end