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(player) click to toggle source
# File lib/studio_game/game.rb, line 15
def add_player(player)
  @players << player
end
format_high_score(player) click to toggle source
# File lib/studio_game/game.rb, line 61
def format_high_score(player)
  "#{player.name}".ljust(20, ".") + "#{player.score}"
end
load_players(file) click to toggle source
# File lib/studio_game/game.rb, line 94
def load_players(file)
  File.readlines(file).each do |line|
  add_player(Player.from_csv(line))
  end
end
play(rounds) { || ... } click to toggle source
# File lib/studio_game/game.rb, line 65
def play(rounds)
  treasures = TreasureTrove::TREASURES

  puts "There are #{treasures.length} treasures to be found:"

  treasures.each{ |t| puts "A #{t.name} is worith #{t.points}"}

  puts "There are #{@players.length} in the #{title}:"
  @players.each do |player|
    puts player
  end
  1.upto(rounds) do |round|
    if block_given?
      break if yield
    end
    puts "\nRound: #{round}"
    @players.each do |player|
      GameTurn.take_turn(player)
      puts player
    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/studio_game/game.rb, line 19
def save_high_scores(file="high_scores.txt")
  File.open(file, "w") do |file| 
    file.puts "#{@title} High Scores:"
    @players.sort.each do |p|
      file.puts format_high_score(p)
    end
  end
end
total_points() click to toggle source
# File lib/studio_game/game.rb, line 88
def total_points
  @players.reduce(0) do |sum, p|
    sum + p.points
  end
end