class StudioGame::Game
Attributes
name[R]
Public Class Methods
new(title)
click to toggle source
# File lib/studio_game/game.rb, line 14 def initialize(title) @players = [] @title = title end
Public Instance Methods
add_player(player)
click to toggle source
# File lib/studio_game/game.rb, line 19 def add_player(player) @players << player end
high_score_entry(player)
click to toggle source
# File lib/studio_game/game.rb, line 96 def high_score_entry(player) formatted_name = player.name.ljust(20, '.') "#{formatted_name} #{player.score}" end
load_players(file)
click to toggle source
# File lib/studio_game/game.rb, line 81 def load_players(file) File.readlines(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 23 def play(rounds) puts "There are #{@players.size} players in #{@name}:" treasures = TreasureTrove::TREASURES 1.upto(rounds) do |round| puts "Round #{round}" @players.each do |player| GameTurn.take_turn(player) puts player end end puts "\nThere are #{treasures.size} treasures to be found:" treasures.each do |treasure| puts "A #{treasure.name} is worth #{treasure.points} points" end end
print_name_and_health(player)
click to toggle source
# File lib/studio_game/game.rb, line 42 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 50 def print_stats strong, wimpy = @players.partition(&:strong?) puts "\n#{@title} Statistics:" puts "\n#{strong.size} strong players:" strong.each do |s| print_name_and_health(s) end puts "\n#{wimpy.size} wimpy players:" wimpy.each do |w| print_name_and_health(w) end puts 'Knuckleheads High Scores:' @players.sort.each do |p| puts high_score_entry(p) end puts "\n#{total_points} total points from treasures found" @players.sort.each do |player| puts "\n#{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 end
save_high_scores(to_file = 'high_scores.txt')
click to toggle source
# File lib/studio_game/game.rb, line 87 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 |p| file.puts high_score_entry(p) end end end
total_points()
click to toggle source
# File lib/studio_game/game.rb, line 46 def total_points @players.each.reduce(0) { |acc, player| acc + player.points } end