class StudioGame::Game
Attributes
title[R]
Public Class Methods
new(title)
click to toggle source
# File lib/studio_game/game.rb, line 11 def initialize(title) @title = title @players = [] end
Public Instance Methods
add_player(player_name)
click to toggle source
# File lib/studio_game/game.rb, line 16 def add_player(player_name) @players << player_name end
load_players(file_name)
click to toggle source
# File lib/studio_game/game.rb, line 20 def load_players(file_name) #File.readlines(file_name).each do |line| #add_player(Player.from_csv(line)) CSV.foreach(file_name) do |row| player = Player.new(row[0], row[1].to_i) add_player(player) end #end end
play(rounds) { || ... }
click to toggle source
# File lib/studio_game/game.rb, line 77 def play(rounds) puts "There are #{@players.size} players in #{@title}:" @players.each do |player| puts player end treasures = TreasureTrove::TREASURES puts "\nThere are #{treasures.size} treasures available!" treasures.each do |treasure| name = treasure.name.to_s puts "#{name.ljust(20,'.')}#{treasure.points}pts" end 1.upto(rounds) do |round_number| if block_given? break if yield end puts "\nRound number: #{round_number}" @players.each do |player| GameTurn.take_turn(player) puts player end end end
print_name_and_health(player)
click to toggle source
# File lib/studio_game/game.rb, line 44 def print_name_and_health(player) "#{player.name} (#{player.health})" end
print_stats()
click to toggle source
# File lib/studio_game/game.rb, line 48 def print_stats strong_player, wimpy_player = @players.partition { |p| p.strong? } puts "\n#{@title} Statistics\n" puts "\n#{strong_player.size} strong players:" strong_player.each do |player| print_name_and_health(player) end puts "\n#{wimpy_player.size} wimpy players:" wimpy_player.each do |player| print_name_and_health(player) end @players.each do |player| puts "\n#{player.name}'s point totals:" player.each_found_treasure {|treasure| puts "#{treasure.points} total #{treasure.name} points"} puts "#{player.points} grand total points" end puts "\n#{@title} High Scores:" @players.sort.each do |player| puts write_name_score(player) end puts "\nTotal points found during the game: #{total_points}" end
save_high_scores(file_name="high_scores.txt")
click to toggle source
# File lib/studio_game/game.rb, line 30 def save_high_scores(file_name="high_scores.txt") File.open(file_name, "w") do |file| file.puts "#{title} High Scores:" puts "\n#{@title} High Scores:" @players.sort.each do |player| file.puts write_name_score(player) end end end
total_points()
click to toggle source
# File lib/studio_game/game.rb, line 102 def total_points @players.reduce(0) { |sum, player| sum + player.points} end
write_name_score(player)
click to toggle source
# File lib/studio_game/game.rb, line 40 def write_name_score(player) "#{player.name.ljust(20,'.')}#{player.score}" end