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(a_player)
click to toggle source
# File lib/studio_game/game.rb, line 51 def add_player(a_player) @players << a_player #another way to do it @players.push(a_player) end
high_score_entry(player)
click to toggle source
# File lib/studio_game/game.rb, line 45 def high_score_entry(player) formatted_name = player.name.ljust(20, '.') "#{formatted_name} #{player.score}" end
load_players(from_file)
click to toggle source
Roll your own CSV file reading
# File lib/studio_game/game.rb, line 18 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/studio_game/game.rb, line 56 def play(rounds) puts "There are #{@players.size} players in #{@title}: " @players.each do |player| puts player end 1.upto(rounds) do |round| if block_given? break if yield end puts "\nRound #{round}:" @players.each do |player| GameTurn.take_turn(player) puts player end end #end up to block treasures = TreasureTrove::TREASURES puts "\nThe treasure trove contains #{TreasureTrove::TREASURES.size} treasures." treasures.each do |treasure| puts "A #{treasure.name} is worth #{treasure.points} points." end end
print_name_and_health(player)
click to toggle source
# File lib/studio_game/game.rb, line 84 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 97 def print_stats puts "#{total_points} total points from treasures found" #Create two arrays: #one containing only the strong players strong_players = @players.select { |player| player.strong? } #and the other containing only the wimpy players. wimpy_players = @players.reject { |player| player.strong? } puts "\n#{@title} Statistics:" puts "\n#{strong_players.size} strong players" strong_players.each do |strong_player| print_name_and_health(strong_player) end puts "\n#{wimpy_players.size} wimpy players" wimpy_players.each do |wimpy_player| print_name_and_health(wimpy_player) end sorted_players = @players.sort { |a, b| b.score <=> a.score } # puts "\n#{@title} High Scores:" # @players.sort.each do |player| # formatted_name = player.name.ljust(20, '.') # puts "#{formatted_name} #{player.score}" # end #Refactored below: puts "\n#{@title} High Scores:" @players.sort.each do |player| puts high_score_entry(player) end puts "\n#{@title} Total Points:" @players.sort.each do |player| puts "\n#{player.name}'s point totals:" player.each_found_treasure do |treasure| puts "#{treasure.points} total #{treasure.name} points" end puts "#{player.points} grand total points" end end
save_high_scores(to_file="high_scores.txt")
click to toggle source
def load_players(from_file) CSV.foreach(from_file) do |row| player = Player.new(row[0], row[1].to_i) add_player(Player.from_csv(line)) end
end
# File lib/studio_game/game.rb, line 36 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 #players.each end #File.open end
total_points()
click to toggle source
iterates through each player and sums up the total points
# File lib/studio_game/game.rb, line 90 def total_points @players.reduce(0) do |sum, player| sum + player.points end end