class StudioGame::Game
Attributes
title[R]
Public Class Methods
new(title)
click to toggle source
# File lib/studio_game/game.rb, line 10 def initialize(title) @title = title @players = [] end
Public Instance Methods
add_player(player)
click to toggle source
# File lib/studio_game/game.rb, line 15 def add_player(player) @players << player end
format_high_score(player)
click to toggle source
# File lib/studio_game/game.rb, line 61 def format_high_score(player) "#{player.name}".ljust(20, ".") + "#{player.score}" end
load_players(file)
click to toggle source
# File lib/studio_game/game.rb, line 94 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 65 def play(rounds) treasures = TreasureTrove::TREASURES puts "There are #{treasures.length} treasures to be found:" treasures.each{ |t| puts "A #{t.name} is worith #{t.points}"} puts "There are #{@players.length} in the #{title}:" @players.each do |player| puts player end 1.upto(rounds) do |round| if block_given? break if yield end puts "\nRound: #{round}" @players.each do |player| GameTurn.take_turn(player) puts player end end end
print_name_and_health(player)
click to toggle source
# File lib/studio_game/game.rb, line 28 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 32 def print_stats() strong, weak = @players.partition{|player| player.strong? } puts "\n#{@title} Statistics:" puts "\n#{strong.length} strong players:" strong.each{ |s| print_name_and_health(s)} puts "\n#{weak.length} weak players:" weak.each { |w| print_name_and_health(w)} puts "\nHigh Scores:" @players.sort.each do |p| puts format_high_score(p) end @players.each do |p| puts "\n#{p.name}'s point totals:" p.each_found_treasure do |t| puts "#{t.points} total #{t.name} points" end puts "#{p.points} grand total points" end puts "\n#{total_points} points from treasures found" end
save_high_scores(file="high_scores.txt")
click to toggle source
# File lib/studio_game/game.rb, line 19 def save_high_scores(file="high_scores.txt") File.open(file, "w") do |file| file.puts "#{@title} High Scores:" @players.sort.each do |p| file.puts format_high_score(p) end end end
total_points()
click to toggle source
# File lib/studio_game/game.rb, line 88 def total_points @players.reduce(0) do |sum, p| sum + p.points end end