class StudioGame::Game
Attributes
name[R]
players[R]
Public Class Methods
new(name)
click to toggle source
# File lib/studio_game/game.rb, line 9 def initialize name @name = name @players = [] @number_players = 0 @center = 40 @alignment = 38 end
Public Instance Methods
add_player(player)
click to toggle source
# File lib/studio_game/game.rb, line 16 def add_player player @players.push(player) @number_players += 1 end
find_random_treasures()
click to toggle source
# File lib/studio_game/game.rb, line 46 def find_random_treasures treasures = TreasureTrove::TREASURES treasures[rand(treasures.length)] end
load_players(array)
click to toggle source
# File lib/studio_game/game.rb, line 105 def load_players array array.each do |file| File.readlines(file).each do |line| name, health = line.split(",") add_player(Player.new(name, Integer(health))) end end end
play(num_games)
click to toggle source
# File lib/studio_game/game.rb, line 31 def play num_games print_game_intro print_all_treasures 1.upto(num_games) do |num| print_game_number(num) @players.each do |player| puts " #{player.name}'s Turn ".center(@center, "-") player.found_treasure(find_random_treasures) GameTurn.take_turn(player) puts end end print_high_scores end
print_all_treasures()
click to toggle source
# File lib/studio_game/game.rb, line 50 def print_all_treasures treasures = TreasureTrove::TREASURES puts "-" * @center puts "|" + "There are #{treasures.length} treasures in this game".center(@alignment) + "|" puts "-" * @center treasures.each do |t| puts "|" + "#{t.name} is worth #{t.points} points.".center(@alignment) + "|" end puts "-" * @center end
print_game_intro()
click to toggle source
# File lib/studio_game/game.rb, line 21 def print_game_intro puts "-" * @center puts "|" + "There are #{@number_players} players in #{@name}".center(@alignment) + "|" puts "-" * @center end
print_game_number(num)
click to toggle source
# File lib/studio_game/game.rb, line 26 def print_game_number num puts "*" * @center puts "*" + "Playing Game #{num}.".center(@alignment) + "*" puts "*" * @center end
print_high_scores()
click to toggle source
# File lib/studio_game/game.rb, line 60 def print_high_scores puts "\nPrinting High Scores:" high_scores = @players.sort{|a, b| b.score <=> a.score} high_scores.each do |player| puts "#{player.name}".ljust(@alignment, ".") + "#{player.score}" end puts "\nTotal Points accumulated #{total_points} from Treasure Trove\n\n" end
print_player_stats()
click to toggle source
# File lib/studio_game/game.rb, line 78 def print_player_stats @players.sort.each do |player| puts " Player #{player.name}'s point breakdown ".center(@center, "*") puts "-" * @center player.each_found_treasure do |treasure| puts "|" + "#{treasure.name} #{treasure.points} points".center(@alignment) + "|" end puts "-" * @center puts "#{player.name}'s total points: #{player.points}" puts "#{player.name}'s total health: #{player.health}" puts end end
print_stats()
click to toggle source
# File lib/studio_game/game.rb, line 96 def print_stats print_stats_header print_weak_strong print_player_stats end
print_stats_header()
click to toggle source
# File lib/studio_game/game.rb, line 91 def print_stats_header puts "-" * @center puts "|" + "#{@name} Statistics".center(@alignment) + "|" puts "-" * @center end
print_weak_strong()
click to toggle source
# File lib/studio_game/game.rb, line 69 def print_weak_strong strong, weak = @players.partition{|n| n.strong?} puts "Top #{strong.length} STRONG players." puts strong.sort puts "\nTop #{weak.length} WEAK players." puts weak.sort puts end
save_high_scores(file_name="high_scores.txt")
click to toggle source
# File lib/studio_game/game.rb, line 113 def save_high_scores file_name="high_scores.txt" File.open(file_name, "w") do |fd| fd.puts "#{@name} High Scores:" @players.sort.each do |player| fd.puts "#{player.name},#{player.score}" end end end
total_points()
click to toggle source
# File lib/studio_game/game.rb, line 101 def total_points @players.reduce(0) { |sum, player| sum + player.points } end