class StudioGame::Game
Attributes
name[RW]
players[R]
Public Class Methods
new(mylist)
click to toggle source
# File lib/studio_game/game.rb, line 11 def initialize(mylist) @name = mylist.capitalize @players = [] end
Public Instance Methods
add_player(player)
click to toggle source
# File lib/studio_game/game.rb, line 32 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) formatted_name = player.name.ljust(20, '.') "#{formatted_name} #{player.score}" end
load_players(from_file)
click to toggle source
# File lib/studio_game/game.rb, line 16 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 36 def play(rounds) puts "#{@name}'s game:" puts @players.sort treasures = TreasureTrove::TREASURES puts "\nThere are #{treasures.size} treasures to be found:" treasures.each do |t| puts "A #{t.name} has a value of #{t.value}. " end 1.upto(rounds) do |round| puts "\nRound #{round}:" @players.each do |player| GameTurn.take_turn(player) player.found_treasure(TreasureTrove.random) puts player end end end
print_name_and_health(p)
click to toggle source
# File lib/studio_game/game.rb, line 59 def print_name_and_health(p) puts "#{p.name} has a health of #{p.health}" end
print_stats()
click to toggle source
# File lib/studio_game/game.rb, line 75 def print_stats puts "\n#{@name}'s Stats:" puts "#{total_points} total points earned" @players.sort.each do |player| puts "\n#{player.name}'s treasure totals:" player.each_found_treasure do |treasure| puts "#{treasure.value} total #{treasure.name} points" end puts "#{player.points} grand total points." end strong, weak = @players.partition { |p| p.strong? } puts "\nStrong:" strong.each {|p| self.print_name_and_health(p)} puts "\nWeak:" weak.each {|p| self.print_name_and_health(p)} puts "\n High Scores:" @players.sort.each do |p| puts high_score_entry(p) end end
save_high_scores(to_file="high_scores.txt")
click to toggle source
# File lib/studio_game/game.rb, line 22 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 |p| file.puts high_score_entry(p) end end end
to_s()
click to toggle source
# File lib/studio_game/game.rb, line 101 def to_s "There are #{@players.size} players in #{name}" end
total_points()
click to toggle source
# File lib/studio_game/game.rb, line 63 def total_points @players.reduce(0) do |sum, player| sum + player.points end end