class StudioGame::Game

Attributes

title[R]

Public Class Methods

new(title) click to toggle source
# File lib/studio_game/game.rb, line 10
def initialize(title)
  @title = title
  @players = []
end

Public Instance Methods

add_player(insert) click to toggle source
# File lib/studio_game/game.rb, line 15
def add_player(insert)
  @players.push(insert)
end
load_players(from_file) click to toggle source
# File lib/studio_game/game.rb, line 19
def load_players(from_file)
    puts "\nLoading inputs from file = #{from_file}:"
    File.readlines(from_file).each do |line|
      puts line
      name, health = line.split(',')
      player = Player.new(name, Integer(health))
      add_player(player)
    end
end
play(rounds) click to toggle source
# File lib/studio_game/game.rb, line 29
def play(rounds)
  puts "There are #{@players.size} players in #{@title} who will be playing #{rounds} rounds:"
  treasures = TreasureTrove::TREASURES

  puts "There are #{treasures.size} treasures to be found"
  treasures.each do |treasure|
    puts "A #{treasure.name} is worth #{treasure.points} points"
  end  
    
  1.upto(rounds) do |round|
    puts "\nRound #{round}:"
  
    @players.each do |player|
      puts player
    end

    @players.each do |player|
      puts "\n"
      GameTurn.take_turn(player)
      puts player  
    end
  end    

end
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 101
def save_high_scores(to_file="high_scores.txt")
  sorted_players_score = @players.sort {|a,b| b.score <=> a.score}
  File.open(to_file, "w") do |file|
    file.puts "#{@title} High Scores:"
    sorted_players_score.each do |player|
      formatted_name = player.name.ljust(20,'.')
      file.puts "#{formatted_name} #{player.score}"
    end
  end
end