Class: StudioGame::Game
- Inherits:
-
Object
- Object
- StudioGame::Game
- Defined in:
- lib/studio_game/game.rb
Instance Attribute Summary collapse
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Instance Method Summary collapse
- #add_player(insert) ⇒ Object
-
#initialize(title) ⇒ Game
constructor
A new instance of Game.
- #load_players(from_file) ⇒ Object
- #play(rounds) ⇒ Object
- #print_stats ⇒ Object
- #save_high_scores(to_file = "high_scores.txt") ⇒ Object
Constructor Details
#initialize(title) ⇒ Game
Returns a new instance of Game.
10 11 12 13 |
# File 'lib/studio_game/game.rb', line 10 def initialize(title) @title = title @players = [] end |
Instance Attribute Details
#title ⇒ Object (readonly)
Returns the value of attribute title.
8 9 10 |
# File 'lib/studio_game/game.rb', line 8 def title @title end |
Instance Method Details
#add_player(insert) ⇒ Object
15 16 17 |
# File 'lib/studio_game/game.rb', line 15 def add_player(insert) @players.push(insert) end |
#load_players(from_file) ⇒ Object
19 20 21 22 23 24 25 26 27 |
# 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) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# 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 ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# 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") ⇒ Object
101 102 103 104 105 106 107 108 109 110 |
# 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 |