class Startupgem::Game

Attributes

game_name[R]

Public Class Methods

new(name) click to toggle source
# File lib/startupgem/game.rb, line 11
def initialize(name)
        @game_name = name
        @players = []
        puts "\n\t\t\t Welcome StartUp Institute!\n\tThank you very much for taking this time to check out my gem."
        puts "\n\t\t~Game #{@game_name} has been created~\n"
end

Public Instance Methods

add_player(player) click to toggle source
# File lib/startupgem/game.rb, line 45
def add_player(player)
        @players.push(player)
        puts "\t\t\t*added player #{player.name}"
end
high_score_entry(player) click to toggle source
# File lib/startupgem/game.rb, line 22
def high_score_entry(player)
        "#{player.name.ljust(20,'.')} #{player.health} health - #{player.score} score"
end
load_file(file) click to toggle source
# File lib/startupgem/game.rb, line 37
def load_file(file)
        puts %Q(\t\t::Loading File '#{file}'::)
        CSV.foreach(file) do |row| 
                add_player(Player.new(row[0],Integer(row[1])))
        end

end
name=(name) click to toggle source
# File lib/startupgem/game.rb, line 18
def name=(name)
        @game_name = name.capitalize
end
play(rounds) { || ... } click to toggle source
# File lib/startupgem/game.rb, line 58
def play(rounds)
        puts "\n::PRESSED PLAY::There are #{@players.size} players in Startup Institute:"
        puts @players.sort.map.with_index(1) {|p,i| puts "\t#{i}. #{p}"}

        treasures = TreasureTrove::TREASURES
        puts "\nThere are #{treasures.size} treasures in the Treasure Trove"
        sorted_treasures = treasures.sort {|x,y| y.points <=> x.points}
        sorted_treasures.map.with_index(1) {|t,i| puts "\t#{i}. #{t.name} - #{t.points} points"}

        puts "\n\t\t:::: GAME START ::::"
        1.upto(rounds) do |round|
                puts "\n~~Round #{round}~~"
                @players.sort.each do |player|
                        if block_given?
                                if yield
                                        puts "No more Treasure available. Nothing to play!"
                                        break
                                else
                                        Startupgem::GameTurn.take_turn(player)
                                        puts "__________________________________________________________________"
                                end
                        else
                                Startupgem::GameTurn.take_turn(player)
                                puts "__________________________________________________________________"
                        end
                end
        end
end
print_name_and_health(player) click to toggle source
print_stats() click to toggle source
save_high_scores(file="high_scores.txt") click to toggle source
# File lib/startupgem/game.rb, line 26
def save_high_scores(file="high_scores.txt")
        File.open(file,'a+') do |file| 
                file.puts("\n\t#{game_name}'s High Scores:")
                @players.sort{|x,y| y.score <=> x.score}.map.with_index(1) do |player,i|
                        file.puts "#{i}." + high_score_entry(player)
                end
                time = Time.now 
                file.puts(time.strftime("Printed on %m/%d/%Y at %I:%M%p"))
        end
end
total_treasure_points() click to toggle source
# File lib/startupgem/game.rb, line 54
def total_treasure_points
        @players.reduce(0) {|sum,p| sum + p.points}
end