class StudioGame::Game

Attributes

title[R]

ATTRIBUTES##

Public Class Methods

new(title) click to toggle source

INITIALIZE AND BASIC INTERACTION##

# File lib/studio_game/game.rb, line 13
def initialize (title)
  @title = title
  @players = []
end

Public Instance Methods

add_player(player) click to toggle source

GAME METHODS##

# File lib/studio_game/game.rb, line 28
def add_player(player)
  @players.push(player)
end
high_score_entry(player) click to toggle source
# File lib/studio_game/game.rb, line 22
def high_score_entry(player)
  player.name.ljust(15,".") + player.score.to_s 
end
load_players(from_file) click to toggle source
# File lib/studio_game/game.rb, line 32
def load_players(from_file)
  File.readlines(from_file).each do |line|
    player = Player.from_csv(line)
    add_player(player)
  end
end
play(rounds) click to toggle source
# File lib/studio_game/game.rb, line 52
def play(rounds)
  #Load treasures from TreasureTrove module and print their values.
  treasures = TreasureTrove::TREASURES
  puts("\nThere are #{treasures.length} treasures to be found:")
  treasures.each do |treasure|
    puts "A #{treasure.name} is worth #{treasure.points} points"
  end
  puts "\n"

  #Display number of players in the game and show their current health.
  puts("There are #{@players.length} players in #{@title}:")
  @players.each do  |player|
    puts player
  end
  puts "\n"

  #Use GameTurn module to play the game and then print stats using the print_stats method.
  rounds.times do |round|
    puts "Round #{round+1}:"
    @players.each do |player|
      GameTurn.take_turn(player)
      puts player
      puts "\n"
    end
    puts "\n"
  end
  print_stats
end
print_name_and_health(player) 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 39
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
to_s() click to toggle source
# File lib/studio_game/game.rb, line 18
def to_s
  @title
end