class StudioGame::Game
Attributes
game_title[R]
Public Class Methods
new(game_title)
click to toggle source
# File lib/studio_game/game.rb, line 13 def initialize(game_title) @game_title = game_title.capitalize @players = [] end
Public Instance Methods
add_player(player)
click to toggle source
# File lib/studio_game/game.rb, line 18 def add_player(player) @players.push player end
load_players(from_file)
click to toggle source
# File lib/studio_game/game.rb, line 84 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
play_game(rounds)
click to toggle source
# File lib/studio_game/game.rb, line 22 def play_game(rounds) puts "There are #{@players.length} players in the game:" @players.each do |player| puts "#{player.name} (#{player.health})" end treasures = TreasureTrove::TREASURES puts "\nThere are #{treasures.length} the treasures to be found:" treasures.each do |treasure| puts "#{treasure.name.capitalize} worth #{treasure.points} points" end 1.upto(rounds).each do |round| puts "\nRound: #{round}" @players.each do |player| GameTurn.take_turn(player) end end end
print_name_and_health(player)
click to toggle source
# File lib/studio_game/game.rb, line 48 def print_name_and_health(player) puts "#{player.name} #{player.score}" end
print_stats()
click to toggle source
# File lib/studio_game/game.rb, line 52 def print_stats puts "\nKnucklehead Statistics:" strong_players, wimpy_players = @players.partition { |player| player.strong? } puts "Strong players:" strong_players.each do |player| puts "#{player.name} (#{player.health})" end puts "\nWimpy players:" wimpy_players.each do |player| puts "#{player.name} (#{player.health})" end puts "\nPoint Totals:" @players.each do |player| puts "\n#{player.name}'s point totals:'" player.each_found_treasure do |treasure| puts "#{treasure.points} #{treasure.name}" end puts "#{player.points} grand total points" end puts "\n#{total_points} total points from treasures found." end
save_high_scores(to_file='high_scores.txt')
click to toggle source
# File lib/studio_game/game.rb, line 92 def save_high_scores(to_file='high_scores.txt') File.open(to_file, 'w') do |file| file.puts "#{@game_title} High Scores:" @players.sort.each do |player| formatted_name = player.name.ljust(20, '.') file.puts "#{formatted_name}#{player.points}" end end end
total_points()
click to toggle source
# File lib/studio_game/game.rb, line 80 def total_points @players.reduce(0) { |sum, player| sum + player.points } end