class StudioGame::Game
Attributes
title[R]
Public Class Methods
new(title)
click to toggle source
# File lib/studio_game/game.rb, line 9 def initialize(title) @title = title @players = [] end
Public Instance Methods
add_player(player)
click to toggle source
# File lib/studio_game/game.rb, line 34 def add_player(player) @players << player end
load(from_file)
click to toggle source
# File lib/studio_game/game.rb, line 14 def load(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 38 def play(rounds) treasure = TreasureTrove::TREASURES puts "\nThere are #{treasure.size} treasures to be found.\n" treasure.each do |treasure| puts "A #{treasure.name} is worth #{treasure.points} points." end 1.upto(rounds) do |round| if block_given? break if yield end puts "\nRound #{round}:" @players.each do |player| GameTurn.take_turn(player) end end end
print_name_and_health(player)
click to toggle source
# File lib/studio_game/game.rb, line 57 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 61 def print_stats puts "\n#{title} Statistics:" strong_players, wimpy_players = @players.partition { |player| player.strong? } puts "\nStrong Players:" strong_players.each do |player| print_name_and_health(player) end puts "\nWimpy Players" wimpy_players.each do |player| print_name_and_health(player) end puts "\n#{title} High Scores" @players.sort.each do |player| formatted_name = player.name.ljust(20, '.') puts "#{formatted_name} #{player.health}" end @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.score} grand total points" end puts "\n#{total_points} total points from treasures found." end
save(to_file="player_rankings.csv")
click to toggle source
# File lib/studio_game/game.rb, line 20 def save(to_file="player_rankings.csv") File.open(to_file, "w") do |file| @players.sort.each do |player| file.puts player.to_csv end end end
total_points()
click to toggle source
# File lib/studio_game/game.rb, line 28 def total_points @players.reduce(0) do |sum, player| sum + player.points end end