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
- #high_score_entry(player) ⇒ Object
-
#initialize(title) ⇒ Game
constructor
A new instance of Game.
- #load(from_file) ⇒ Object
- #play(rounds) ⇒ Object
- #print_name_and_health(player) ⇒ Object
- #print_stats ⇒ Object
- #save_high_scores(to_file = "high_scores.txt") ⇒ Object
- #total_points ⇒ 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(player) ⇒ Object
36 37 38 |
# File 'lib/studio_game/game.rb', line 36 def add_player(player) @players << player end |
#high_score_entry(player) ⇒ Object
32 33 34 |
# File 'lib/studio_game/game.rb', line 32 def high_score_entry(player) print_name_and_health(player) end |
#load(from_file) ⇒ Object
15 16 17 18 19 |
# File 'lib/studio_game/game.rb', line 15 def load(from_file) File.readlines(from_file).each do |line| add_player(Player.from_csv(line)) end end |
#play(rounds) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/studio_game/game.rb', line 40 def play(rounds) puts "\n\n#{@title}'s game" treasures = TreasureTrove::TREASURES puts "\nThere are #{treasures.size} treasures to be found:" treasures.each do |treasure| puts "A #{treasure.name} is worth #{treasure.points}" end 1.upto(rounds) do |round| puts "\n---- New round: #{round} ----" @players.each do |player| GameTurn.take_turn(player) puts player end puts "Points at the end of the #{round}round: #{total_points}" # if yield # break # end end end |
#print_name_and_health(player) ⇒ Object
62 63 64 65 |
# File 'lib/studio_game/game.rb', line 62 def print_name_and_health(player) "#{player.name}".ljust(20, '.') + "#{player.score}" # "#{player.score}".ljust(20, '.') + "#{player.name}" end |
#print_stats ⇒ Object
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 |
# File 'lib/studio_game/game.rb', line 67 def print_stats puts "\n\n#{@title}'s Stats" strongs, wimpies = @players.partition{ |player| player.strong? } # strongs.sort.reverse! puts "\n#{strongs.length} strong player:" strongs.each { |player| puts "#{player.name} (#{player.health})" } puts "\n#{wimpies.length} wimpy player:" wimpies.each { |player| puts "#{player.name} (#{player.health})" } # sorted_players = @players.sort_by { |player| player.score } .reverse # sorted_players = @players.sort { |x,y| y.score <=> x.score } @players.each do |player| puts "\n#{player.name}'s points totals:" player.each_found_treasure do | treasure | puts "#{treasure.points} total #{treasure.name} points" end puts "#{player.points} grand total points" end puts "\n\n#{@title}'s High Scores" @players.sort.each do |player| puts high_score_entry(player) end end |
#save_high_scores(to_file = "high_scores.txt") ⇒ Object
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/studio_game/game.rb', line 21 def save_high_scores(to_file="high_scores.txt") File.open(to_file,'w') do |file| file.puts "#{@title}'s High Scores:" @players.sort.each do |player| # file.puts print_name_and_health(player) file.puts "#{high_score_entry(player)}" end end end |
#total_points ⇒ Object
94 95 96 |
# File 'lib/studio_game/game.rb', line 94 def total_points @players.reduce(0) { |sum, player| sum + player.points } end |