Class: StudioGame::Game
- Inherits:
-
Object
- Object
- StudioGame::Game
- Defined in:
- lib/studio_game/game.rb
Instance Attribute Summary collapse
-
#players ⇒ Object
readonly
Returns the value of attribute players.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Instance Method Summary collapse
- #add_player(player) ⇒ Object
- #high_scores_entry(player) ⇒ Object
-
#initialize(title) ⇒ Game
constructor
A new instance of Game.
- #load_players(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.
14 15 16 17 |
# File 'lib/studio_game/game.rb', line 14 def initialize(title) @title = title.capitalize @players = [] end |
Instance Attribute Details
#players ⇒ Object (readonly)
Returns the value of attribute players.
12 13 14 |
# File 'lib/studio_game/game.rb', line 12 def players @players end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
11 12 13 |
# File 'lib/studio_game/game.rb', line 11 def title @title end |
Instance Method Details
#add_player(player) ⇒ Object
28 29 30 |
# File 'lib/studio_game/game.rb', line 28 def add_player(player) @players << player end |
#high_scores_entry(player) ⇒ Object
44 45 46 47 48 |
# File 'lib/studio_game/game.rb', line 44 def high_scores_entry(player) formatted_name = player.name.ljust(20, '.') "#{formatted_name} #{player.score}" end |
#load_players(from_file) ⇒ Object
19 20 21 22 23 24 |
# File 'lib/studio_game/game.rb', line 19 def load_players(from_file) CSV.foreach(from_file) do |row| player = Player.new(row[0], row[1].to_i) add_player(player) end end |
#play(rounds) ⇒ Object
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/studio_game/game.rb', line 97 def play(rounds) puts @players 1.upto(rounds) do |round| puts "\nRound #{round}:" @players.each do |p| GameTurn.take_turn(p) puts p end end end |
#print_name_and_health(player) ⇒ Object
88 89 90 |
# File 'lib/studio_game/game.rb', line 88 def print_name_and_health(player) puts "#{player.name} (#{player.health})" end |
#print_stats ⇒ Object
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 |
# File 'lib/studio_game/game.rb', line 58 def print_stats strong_players, wimpy_players = @players.partition {|x| x.strong?} puts "\n#{strong_players.size} strong players:" strong_players.each do |player| print_name_and_health(player) end puts "\n#{wimpy_players.size} wimpy players:" wimpy_players.each do |player| print_name_and_health(player) 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 puts "#{total_points} total points from treasures found" puts "\n#{title} High Scores:" @players.sort.each do |player| puts high_scores_entry(player) end end |
#save_high_scores(to_file = "high_scores.txt") ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/studio_game/game.rb', line 32 def save_high_scores(to_file="high_scores.txt") File.open to_file, "w+" do |file| file.puts "#{@title} High Scores:" @players.sort.each do |player| file.puts high_scores_entry(player) end end end |
#total_points ⇒ Object
92 93 94 |
# File 'lib/studio_game/game.rb', line 92 def total_points @players.reduce (0) {|sum, player| sum + player.points } end |