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
print_stats() click to toggle source
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