class StudioGame::Game
Attributes
name[RW]
players[R]
Public Class Methods
new(mylist)
click to toggle source
# File lib/studio_game/game.rb, line 12 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 38 def add_player(player) @players << player end
high_score_entry(player)
click to toggle source
# File lib/studio_game/game.rb, line 23 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 17 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 42 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 "#{t.name} has #{t.points} points" end 1.upto(rounds) do |round| puts "\nRound number #{round}:" @players.each do |player| GameTurn.take_turn(player) player.found_treasure(TreasureTrove.random) puts player end end end
print_name_and_health(player)
click to toggle source
# File lib/studio_game/game.rb, line 103 def print_name_and_health(player) puts "#{player.name} has a health of #{player.health}." end
print_stats()
click to toggle source
# File lib/studio_game/game.rb, line 69 def print_stats puts "\n#{@name}'s status:" puts "#{total_points} total points earned" @players.sort.each do |p| puts "\n#{p.name}'s total points:" p.each_found_treasure do |treasure| puts "#{treasure.points} total #{treasure.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 "\n#{@name} High Scores:" @players.sort.each do |player| puts high_score_entry(player) end end
save_high_scores(to_file="high_scores.txt")
click to toggle source
# File lib/studio_game/game.rb, line 29 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 |player| file.puts high_score_entry(player) end end end
to_s()
click to toggle source
# File lib/studio_game/game.rb, line 99 def to_s puts "There are #{@players.length} players in Knuckleheads:" 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