class StudioGame::Game
Public Class Methods
new(title)
click to toggle source
# File lib/studio_game/game.rb, line 13 def initialize(title) @title = title.capitalize @players = [] end
Public Instance Methods
add_player(player)
click to toggle source
# File lib/studio_game/game.rb, line 18 def add_player(player) @players << player end
high_score_entry(player)
click to toggle source
# File lib/studio_game/game.rb, line 28 def high_score_entry(player) "#{player.name.ljust(20, '.')} #{player.score}" end
high_scores()
click to toggle source
# File lib/studio_game/game.rb, line 32 def high_scores "\n#{@title} High Scores:" @players.sort.each do |p| "#{p.name.ljust(20, '.')} #{p.score}" end end
load_players(from_file)
click to toggle source
# File lib/studio_game/game.rb, line 39 def load_players(from_file) CSV.foreach(from_file) do |line| player = Player.new(line[0], line[1].to_i) add_player(player) end end
play(rounds) { || ... }
click to toggle source
# File lib/studio_game/game.rb, line 82 def play(rounds) puts "There are #{@players.count} players in #{@title}" @players.each do |p| puts p end treasures = TreasureTrove::TREASURES puts "\nThere are #{treasures.size} to be found:" treasures.each do |treasure| puts "A #{treasure.name} is worth #{treasure.points} points" end 1.upto(rounds) do |n| if block_given? break puts "\nSTOPPING! Max score limit reached" if yield end puts "\nRound #{n}" @players.each do |p| GameTurn.take_turn(p) puts p end end end
print_player_stats(players, type)
click to toggle source
# File lib/studio_game/game.rb, line 22 def print_player_stats(players, type) puts "\n#{players.size} #{type} players:" players.each { |p| puts "#{p.name} (#{p.health})"} end
print_stats()
click to toggle source
# File lib/studio_game/game.rb, line 55 def print_stats strong_players, wimpy_players = @players.partition { |pl| pl.strong? } puts "\n#{@title} Statistics:" print_player_stats(strong_players, 'strong') print_player_stats(wimpy_players, 'wimpy') puts "\n#{total_points} total points from treasures found" puts "\n#{@title} High Scores" @players.sort.each do |p| puts high_score_entry(p) end @players.each do |player| puts "\n#{player.name}'s points totals:" player.each_found_treasure do |treasure| puts "#{treasure.points} total #{treasure.name} points" end puts "#{player.points} grand total points" end end
save_high_scores(to_file="high_scores.txt")
click to toggle source
# File lib/studio_game/game.rb, line 46 def save_high_scores(to_file="high_scores.txt") File.open(to_file, 'w+') do |file| file.puts "#{@title} High Scores:" @players.sort.each do |player| file.puts high_score_entry(player) end end end
total_points()
click to toggle source
# File lib/studio_game/game.rb, line 78 def total_points @players.reduce(0) { |sum, player| sum + player.points } end