class StudioGame::Game
Attributes
title[R]
Public Class Methods
new(title)
click to toggle source
# File lib/studio_game/game.rb, line 10 def initialize(title) @title = title @players = [] end
Public Instance Methods
add_player(insert)
click to toggle source
# File lib/studio_game/game.rb, line 15 def add_player(insert) @players.push(insert) end
load_players(from_file)
click to toggle source
# File lib/studio_game/game.rb, line 19 def load_players(from_file) puts "\nLoading inputs from file = #{from_file}:" File.readlines(from_file).each do |line| puts line name, health = line.split(',') player = Player.new(name, Integer(health)) add_player(player) end end
play(rounds)
click to toggle source
# File lib/studio_game/game.rb, line 29 def play(rounds) puts "There are #{@players.size} players in #{@title} who will be playing #{rounds} rounds:" treasures = TreasureTrove::TREASURES puts "There are #{treasures.size} treasures to be found" treasures.each do |treasure| puts "A #{treasure.name} is worth #{treasure.points} points" end 1.upto(rounds) do |round| puts "\nRound #{round}:" @players.each do |player| puts player end @players.each do |player| puts "\n" GameTurn.take_turn(player) puts player end end end
print_stats()
click to toggle source
# File lib/studio_game/game.rb, line 54 def print_stats strong_players = @players.select { |player| player.strong?} strong_players_sort = strong_players.sort {|a,b| b.health <=> a.health} wimpy_players = @players.reject { |player| player.strong?} wimpy_players_sort = wimpy_players.sort {|a,b| b.health <=> a.health} sorted_players_score = @players.sort {|a,b| b.score <=> a.score} sorted_players_points = @players.sort {|a,b| b.points <=> a.points} puts "\n#{title} Health Statistics:" puts "\n#{strong_players.size} strong players:" puts "Name: Health:" strong_players_sort.each do |player| formatted_name = player.name.ljust(20,'.') puts "#{formatted_name} #{player.health}" end puts "\n#{wimpy_players.size} wimpy players:" puts "Name: Health:" wimpy_players_sort.each do |player| formatted_name = player.name.ljust(20,'.') puts "#{formatted_name} #{player.health}" end puts "\n#{title} Total Points:" puts "Name: Points:" sorted_players_points.each do |player| formatted_name = player.name.ljust(20,'.') puts "#{formatted_name} #{player.points}" end puts "\n#{title} High Scores:" puts "Name: Score:" sorted_players_score.each do |player| formatted_name = player.name.ljust(20,'.') puts "#{formatted_name} #{player.score}" end @players.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
# File lib/studio_game/game.rb, line 101 def save_high_scores(to_file="high_scores.txt") sorted_players_score = @players.sort {|a,b| b.score <=> a.score} File.open(to_file, "w") do |file| file.puts "#{@title} High Scores:" sorted_players_score.each do |player| formatted_name = player.name.ljust(20,'.') file.puts "#{formatted_name} #{player.score}" end end end