class StudioGame::Game
Attributes
title[R]
Public Class Methods
new(title)
click to toggle source
# File lib/studio_game/game.rb, line 8 def initialize(title) @title = title.capitalize @players = [] end
Public Instance Methods
add_player(player)
click to toggle source
# File lib/studio_game/game.rb, line 13 def add_player(player) @players.push(player) end
load_players(from_file)
click to toggle source
# File lib/studio_game/game.rb, line 77 def load_players(from_file) File.readlines(from_file).each do |line| name, health = line.split(',') yungplayer = Player.new(name,health.to_i) add_player(yungplayer) end end
play(rounds)
click to toggle source
# File lib/studio_game/game.rb, line 54 def play(rounds) puts "Welcome to #{@title}!" puts "There are 6 treasures to be found:" treasures = TreasureTrove::TREASURES treasures.each do |treasure| puts "A #{treasure.name} is worth #{treasure.points} points!" end puts "There are #{@players.size} players in #{@title}. We will play #{rounds} rounds:" puts @players.each do |player| puts player end 1.upto(rounds) do |round| puts puts "----ROUND #{round} ------" @players.each do |player| GameTurn.take_turn(player) puts player end end print_stats end
pop_players()
click to toggle source
# File lib/studio_game/game.rb, line 17 def pop_players @players.pop end
print_final(player)
click to toggle source
# File lib/studio_game/game.rb, line 21 def print_final(player) puts "#{player.name}: #{player.health}" end
print_stats()
click to toggle source
# File lib/studio_game/game.rb, line 24 def print_stats puts puts "#{@title} Statistics:" puts @strong_players = @players.select { |playa| playa.strong? } @wimpy_players = @players.reject { |playa| playa.strong? } puts "#{@strong_players.size} strong players:" @strong_players.each do |playa| print_final(playa) end puts puts "#{@wimpy_players.size} weak players:" @wimpy_players.each do |playa| print_final(playa) end puts puts "#{@title} Point Totals:" @players.sort! @players.each do |playa| puts "#{playa.name.ljust(15, '.')} #{playa.points}" playa.each_found_treasure do |treasure| puts "#{treasure.name} .... #{treasure.points}" end puts end end
save_high_scores(to_file="high_scores.txt")
click to toggle source
# File lib/studio_game/game.rb, line 85 def save_high_scores(to_file="high_scores.txt") File.open(to_file, "w") do |file| file.puts "#{@title} high scores:" @players.sort! @players.each do |playa| file.puts "#{playa.name.ljust(15, '.')} #{playa.points}" end end end