class StudioGame::Game
Public Class Methods
new(title)
click to toggle source
# File lib/studio_game/game.rb, line 10 def initialize(title) @title = title.capitalize @players = [] end
Public Instance Methods
add_player(player_name)
click to toggle source
# File lib/studio_game/game.rb, line 35 def add_player(player_name) @players << player_name end
high_score_entry(player)
click to toggle source
# File lib/studio_game/game.rb, line 15 def high_score_entry(player) "#{player.name.ljust(20, '.')} #{player.score}" end
load_players(file)
click to toggle source
# File lib/studio_game/game.rb, line 20 def load_players(file) CSV.foreach(file) do |row| add_player(Player.new(row[0], row[1].to_i)) end end
play(rounds)
click to toggle source
# File lib/studio_game/game.rb, line 82 def play(rounds) TreasureTrove.list puts "\nThere are #{@players.size} players in #{@title}." @players.each do |p| puts p end 1.upto(rounds) do |count| puts "\nRound #{count}" @players.each do |p| GameTurn.take_turn(p) puts p end end end
print_score()
click to toggle source
# File lib/studio_game/game.rb, line 39 def print_score puts "\n#{@title} high scores:" @players.sort.each do |player| puts high_score_entry(player) end end
print_stats()
click to toggle source
# File lib/studio_game/game.rb, line 66 def print_stats puts "\n#{@title}'s totals:" puts "#{total_treasure_points} total points from treasures found." @players.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.points} GRAND TOTAL POINTS." end puts "\n#{@title} statistics:" strong_players, wimpy_players = @players.partition { |player| player.strong? } strong_stats(strong_players) wimpy_stats(wimpy_players) end
save_high_scores(output_file="high_scores.txt")
click to toggle source
# File lib/studio_game/game.rb, line 26 def save_high_scores(output_file="high_scores.txt") File.open(output_file, "w") do |file| file.puts "#{@title} High Scores:" @players.sort.each do |player| file.puts high_score_entry(player) end end end
strong_stats(strong_players)
click to toggle source
# File lib/studio_game/game.rb, line 46 def strong_stats(strong_players) puts "\n#{strong_players.size} strong players:" strong_players.each do |player| puts "#{player.name} (#{player.health})" end end
total_treasure_points()
click to toggle source
# File lib/studio_game/game.rb, line 60 def total_treasure_points @players.reduce(0) do |sum, player| sum + player.points end end
wimpy_stats(wimpy_players)
click to toggle source
# File lib/studio_game/game.rb, line 53 def wimpy_stats(wimpy_players) puts "\n#{wimpy_players.size} wimpy players:" wimpy_players.each do |player| puts "#{player.name} (#{player.health})" end end