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(player) ⇒ Object
- #create_high_score_entry(player) ⇒ Object
-
#initialize(title) ⇒ Game
constructor
A new instance of Game.
- #load_players(file) ⇒ Object
- #play(rounds) ⇒ Object
- #print_name_and_health(player) ⇒ Object
- #print_stats ⇒ Object
- #save_scores(file = "high_scores.txt") ⇒ Object
- #total_points ⇒ Object
Constructor Details
#initialize(title) ⇒ Game
12 13 14 15 |
# File 'lib/studio_game/game.rb', line 12 def initialize(title) @title = title @players = [] end |
Instance Attribute Details
#title ⇒ Object (readonly)
Returns the value of attribute title.
10 11 12 |
# File 'lib/studio_game/game.rb', line 10 def title @title end |
Instance Method Details
#add_player(player) ⇒ Object
17 18 19 20 |
# File 'lib/studio_game/game.rb', line 17 def add_player(player) #@players.push(player) @players << player end |
#create_high_score_entry(player) ⇒ Object
41 42 43 44 |
# File 'lib/studio_game/game.rb', line 41 def create_high_score_entry(player) formatted_score = player.name.ljust(30, ".") "#{formatted_score}#{player.score}" end |
#load_players(file) ⇒ Object
27 28 29 30 31 |
# File 'lib/studio_game/game.rb', line 27 def load_players(file) File.readlines(file).each do |line| add_player(Player.from_csv(line)) end end |
#play(rounds) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/studio_game/game.rb', line 46 def play(rounds) treasures = TreasureTrove::TREASURES puts "There are #{treasures.count} treasures to be found." treasures.each do |treasure| puts "A #{treasure.name} is worth #{treasure.points} points." end puts "\nThere are #{@players.size} players in #{@title}: " @players.each do |player| puts player end 1.upto(rounds) do |turn| puts "\nRound: #{turn}" @players.each do |player| GameTurn.take_turn(player) puts player end end end |
#print_name_and_health(player) ⇒ Object
71 72 73 |
# File 'lib/studio_game/game.rb', line 71 def print_name_and_health(player) puts player.name.ljust(30, ".").concat(player.score.to_s) end |
#print_stats ⇒ Object
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 100 |
# File 'lib/studio_game/game.rb', line 75 def print_stats strong_players, wimpy_players = @players.partition {|player| player.strong?} puts "\name#{total_points} total points from treasure found" @players.sort.each do |player| puts "\n#{player.name}'s point totals:" player.each_found_treasure {|treasure| puts "#{treasure.points} total #{treasure.name} points"} puts "#{player.points} grand total points" end puts "\n#{strong_players.count} strong players:" strong_players.each do |player| puts "#{player.name} (#{player.health})" end puts "#{wimpy_players.count} wimpy players:" wimpy_players.each do |player| puts "#{player.name} (#{player.health})" end @players.sort.each do |player| print_name_and_health(player) end end |
#save_scores(file = "high_scores.txt") ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/studio_game/game.rb', line 33 def save_scores(file="high_scores.txt") File.open(file, 'w') do |file| @players.each do |player| file.puts create_high_score_entry(player) end end end |
#total_points ⇒ Object
22 23 24 25 |
# File 'lib/studio_game/game.rb', line 22 def total_points @players.reduce(0) {|sum, player| sum + player.points} end |