class StudioGame::Game
Attributes
name[RW]
players[R]
Public Class Methods
new(mylistname)
click to toggle source
# File lib/studio_game/game.rb, line 11 def initialize (mylistname) @name = mylistname.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 66 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 treas = TreasureTrove::TREASURES puts "\nThere are #{treas.size} treasures to be found." treas.each do |t| puts "#{t.name} is worth #{t.points} points" end 1.upto(rounds) do |round| puts "\nRound number #{round}:" @players.each do |player| GameTurn.take_turn(player) puts player end end end
print_name_and_health(pla)
click to toggle source
# File lib/studio_game/game.rb, line 56 def print_name_and_health(pla) puts "#{pla.name} has a health of #{pla.health}" end
print_stats()
click to toggle source
# File lib/studio_game/game.rb, line 71 def print_stats puts "\n#{@name}'s Stats:" puts "#{total_points} total points earned" @players.sort.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 strong, weak = @players.partition { |player| player.strong? } puts "\nStrong:" strong.each {|p| self.print_name_and_health(p)} # puts strong.sort puts "\nWeak:" weak.each {|p| self.print_name_and_health(p)} # puts weak.sort puts "\nHigh 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 99 def to_s "There are #{@players.size} players in #{name}." end
total_points()
click to toggle source
# File lib/studio_game/game.rb, line 60 def total_points @players.reduce(0) do |sum, player| sum + player.points end end