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_players(filename) ⇒ Object
- #play(rounds) ⇒ Object
- #print_name_and_health(player) ⇒ Object
- #print_stats ⇒ Object
- #save_high_scores(high_score_filename = "high_scores.txt") ⇒ Object
- #total_points ⇒ Object
Constructor Details
#initialize(title) ⇒ Game
Returns a new instance of Game.
9 10 11 12 |
# File 'lib/studio_game/game.rb', line 9 def initialize(title) @title = title.capitalize @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
14 15 16 |
# File 'lib/studio_game/game.rb', line 14 def add_player(player) @players << player end |
#high_score_entry(player) ⇒ Object
106 107 108 |
# File 'lib/studio_game/game.rb', line 106 def high_score_entry(player) "#{player.name}".ljust(20, '.') + "#{player.score}\n" end |
#load_players(filename) ⇒ Object
91 92 93 94 95 |
# File 'lib/studio_game/game.rb', line 91 def load_players(filename) File.readlines(filename).each do |line| add_player(Player.from_csv(line)) end end |
#play(rounds) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/studio_game/game.rb', line 18 def play(rounds) puts "\nThere are #{@players.size} in #{@title}." @players.each do |player| puts player end treasures = TreasureTrove::TREASURES puts "\nThere are #{treasures.size} treasures to be found:" treasures.each do |t| puts "A #{t.name} is worth #{t.points} points" end 1.upto(rounds) do |r| if block_given? break if yield end puts "\n" puts "Round #{r}".center(20, '-') @players.each do |player| GameTurn.take_turn(player) puts player end end puts "Game Over".center(50, '*') end |
#print_name_and_health(player) ⇒ Object
49 50 51 |
# File 'lib/studio_game/game.rb', line 49 def print_name_and_health(player) puts "#{player.name} (#{player.health})\n" end |
#print_stats ⇒ Object
53 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 |
# File 'lib/studio_game/game.rb', line 53 def print_stats strong_players, wimpy_players = @players.partition {|p| p.strong?} puts "\n#{@title} Statistics:\n" puts "\n#{strong_players.size} strong players:" strong_players.each do |p| print_name_and_health(p) end puts "\n#{wimpy_players.size} wimpy players:" wimpy_players.each do |p| print_name_and_health(p) end puts "\n#{@title} High Scores:" @players.sort.each do |p| puts high_score_entry(p) end puts "\n" @players.sort.each do |player| puts "#{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\n\n" end puts "#{total_points} total points from treasures found" end |
#save_high_scores(high_score_filename = "high_scores.txt") ⇒ Object
97 98 99 100 101 102 103 104 |
# File 'lib/studio_game/game.rb', line 97 def save_high_scores(high_score_filename="high_scores.txt") File.open(high_score_filename, "w") do |file| file.puts "#{@title} High Scores:" @players.sort.each do |p| file.puts high_score_entry(p) end end end |
#total_points ⇒ Object
87 88 89 |
# File 'lib/studio_game/game.rb', line 87 def total_points @players.reduce(0) {|sum, player| sum + player.points} end |