class StudioGame::Game
Attributes
title[R]
Public Class Methods
new(title)
click to toggle source
# File lib/studio_game/game.rb, line 7 def initialize(title) @title = title @players = [] end
Public Instance Methods
add_player(player)
click to toggle source
# File lib/studio_game/game.rb, line 12 def add_player(player) @players<<player end
high_score_entry(player)
click to toggle source
# File lib/studio_game/game.rb, line 41 def high_score_entry(player) formatted_name = player.name.ljust(20, '.') "#{formatted_name} #{player.score}" end
load_players(file = 'player.csv')
click to toggle source
# File lib/studio_game/game.rb, line 35 def load_players(file = 'player.csv') 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 16 def play(rounds) puts "There are #{@players.length} players in #{@title}:" 1.upto(rounds) do |round| puts "\nRound #{round}" @players.each do |player_obj| GameTurn.take_turn(player_obj) puts player_obj end end end
print_name_and_health(player)
click to toggle source
# File lib/studio_game/game.rb, line 27 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 55 def print_stats puts "#{@tilte} Statistics" strong, wimpy = @players.partition {|player| player.strong?} puts "#{strong.length} strong players" strong.each do |player| print_name_and_health(player) end puts "#{wimpy.length} Statistics" wimpy.each do |player| print_name_and_health(player) end sorted_players = @players.sort puts "#{@title} High Score" sorted_players.each do |player| puts high_score_entry(player) end treasures = TreasureTrove::TREASURES puts "There are #{treasures.length} treasures to be found:" treasures.each do |treasure| puts "A #{treasure.name} is worth #{treasure.points} points" end @players.each do |player| puts "#{player.name}'s points totals:" puts "#{player.score} grand total points" end @players.each do |player| puts "-------------" player.each_found_treasure do |treasure| puts "#{treasure.points} total #{treasure.name} points" end end puts "#{total_points} total points from treasure found" end
save_high_scores(to_file = "high_scores.txt")
click to toggle source
# File lib/studio_game/game.rb, line 46 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 end end
total_points()
click to toggle source
# File lib/studio_game/game.rb, line 31 def total_points @players.reduce(0) { |sum, player| sum + player.points} end