class StudioGame::Game

Attributes

title[R]

Public Class Methods

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

Public Instance Methods

add_player(p_name) click to toggle source
# File lib/studio_game/game.rb, line 17
def add_player(p_name)
  @players << p_name
end
high_score_entry(player) click to toggle source
# File lib/studio_game/game.rb, line 97
def high_score_entry(player)
  formatted_name = player.player_name.ljust(20, '.')
  "#{formatted_name} #{player.score}"
end
load_players(file_name) click to toggle source

stuck here. I do not know why it is not saving the name. whenever I run it says @player_name=nil

# File lib/studio_game/game.rb, line 78
def load_players(file_name)
  File.readlines(file_name).each do |line|
    # name, health = line.split(',')
    # new_player = Player.new(name, health.to_i)
    # add_player(new_player)
    add_player(Player.from_csv(line))
  end
end
play(rounds) click to toggle source
# File lib/studio_game/game.rb, line 21
def play(rounds)
  puts "There are #{@players.size} players in #{@title}."
  puts @players
  treasures = TreasureTrove::TREASURES
  puts "\nThere are #{treasures.size} treasures to be found:"
  treasures.each { |treasure| puts "A #{treasure.name} is worth #{treasure.points} points." }

  1.upto(rounds) do |count|
    puts "\nRound #{count}:"
    @players.each do |player|
      GameTurn.take_turn(player)
    end
  end
end
print_name_and_health(player) click to toggle source
print_stats() click to toggle source
save_high_scores(file_name="high_scores.txt") click to toggle source
# File lib/studio_game/game.rb, line 87
def save_high_scores(file_name="high_scores.txt")
  File.open(file_name, "w") do |file|
    @players.sort.each do |player|
      # formatted_name = player.player_name.ljust(20, '.')
      # file.puts "#{formatted_name} #{player.score}"
      file.puts high_score_entry(player)
    end
  end
end