class FightClub::Game

Attributes

title[R]

Public Class Methods

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

Public Instance Methods

add_player(player) click to toggle source
# File lib/fightclub/game.rb, line 17
def add_player(player)
  @players << player
end
high_score_entry(player) click to toggle source
# File lib/fightclub/game.rb, line 21
def high_score_entry(player)
  formatted_name = player.name.ljust(20, ".")
  "#{formatted_name} #{player.score}"
end
load_players(from_file) click to toggle source
# File lib/fightclub/game.rb, line 26
def load_players(from_file)
  File.readlines(from_file).each do |line|
    add_player(Player.from_csv(line))
  end
end
play(rounds) click to toggle source
# File lib/fightclub/game.rb, line 82
def play(rounds)
  puts "There are #{@players.length} players in the game:"
  puts @players

  treasures = TreasureTrove::TREASURES
  puts "\nThere are #{treasures.length} Treasures available in this game:"

  treasures.each do |treasure|
    puts "A #{treasure.name} is worth #{treasure.points} points."
  end
  puts "\n"
  i = 1
  1.upto(rounds) do
    puts "\nRound #{i}:"
    i += 1
    @players.each do |player|
      GameTurn.take_turn(player)
      puts player
    end
  end
end
print_stats() click to toggle source
save_high_scores(file_name="high_scores.txt") click to toggle source
# File lib/fightclub/game.rb, line 45
def save_high_scores(file_name="high_scores.txt")
  File.open(file_name, "w") do |file|
    file.puts("#{@title}'s High Scores:")
    @players.sort.each do |player|

      file.puts high_score_entry(player)
    end
  end
end
total_points() click to toggle source

alternate using CSV library require 'csv'

def load_players(from_file)
  CSV.foreach(from_file) do |row|
    player = Player.new(row[0], row[1].to_i)
    add_player(player)
  end
end
# File lib/fightclub/game.rb, line 41
def total_points
  @players.reduce(0) {|sum, player| sum + player.points}
end