class StudioGame::Game
Attributes
title[RW]
Public Class Methods
new(title)
click to toggle source
# 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
# File lib/studio_game/game.rb, line 18 def add_player(player) @players.push(player) # puts @players end
load_players(file)
click to toggle source
# File lib/studio_game/game.rb, line 95 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 23 def play(rounds) puts "Let's play Knuckleheads!" puts "\nLooks like we have #{@players.size} players this game:" @players.each do |player| formatted_name = player.name.ljust(20, '.') puts "#{formatted_name} #{player.health} HP" end treasures = TreasureTrove::TREASURES puts "\nThere are #{treasures.count} treasures in the Treasure Trove:" treasures.each do |treasure| puts "A #{treasure.name} is worth #{treasure.points} HP" end 1.upto(rounds) do |round| puts "\nRound #{round}:" @players.each do |player| GameTurn.take_turn(player) puts "#{player}" puts "\n" end end end
print_high_scores(player)
click to toggle source
# File lib/studio_game/game.rb, line 52 def print_high_scores(player) formatted_name = player.name.ljust(20, '.') "#{formatted_name} #{player.score}" end
print_name_and_health(player)
click to toggle source
# File lib/studio_game/game.rb, line 91 def print_name_and_health(player) puts "#{player.name} (#{player.health}" end
print_stats()
click to toggle source
# File lib/studio_game/game.rb, line 57 def print_stats puts "\n*** Knucklehead Statistics ***" strong_players, wimpy_players = @players.partition { |player| player.strong? } puts "\n#{strong_players.count} strong players:" strong_players.each do |player| puts "#{player.name} (#{player.health})" end puts "\n#{wimpy_players.count} wimpy players:" wimpy_players.each do |player| puts "#{player.name} (#{player.health})" end puts "\n#{title} Final Ranking:" @players.sort.each do |player| puts print_high_scores(player) end @players.each do |player| puts "\n#{player.name}'s Point Breakdown" player.each_found_treasure do |treasure| puts "+ #{treasure.points} total #{treasure.name} points" end puts "= #{player.points} Treasure Points" puts "+ #{player.health} HP" puts "= #{player.score} Total Game Points" end end
save_high_scores(file="high_scores_revised.txt")
click to toggle source
# File lib/studio_game/game.rb, line 101 def save_high_scores(file="high_scores_revised.txt") File.open(file, "w") do |file| file.puts "#{@title} High Score" @players.sort.each do |player| file.puts print_high_scores(player) end end end