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_score_entry(player) ⇒ Object
-
#initialize(name) ⇒ Game
constructor
A new instance of Game.
- #load_players ⇒ Object
- #play(rounds) ⇒ Object
- #print_stats ⇒ Object
- #save_high_scores ⇒ Object
- #strong_players ⇒ Object
- #to_s ⇒ Object
- #total_points ⇒ Object
- #wimpy_players ⇒ Object
Constructor Details
#initialize(name) ⇒ Game
Returns a new instance of Game.
8 9 10 11 |
# File 'lib/studio_game/game.rb', line 8 def initialize(name) @title = name @players = [] end |
Instance Attribute Details
#players ⇒ Object (readonly)
Returns the value of attribute players.
6 7 8 |
# File 'lib/studio_game/game.rb', line 6 def players @players end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
6 7 8 |
# File 'lib/studio_game/game.rb', line 6 def title @title end |
Instance Method Details
#add_player(player) ⇒ Object
13 14 15 |
# File 'lib/studio_game/game.rb', line 13 def add_player(player) @players.push(player) end |
#high_score_entry(player) ⇒ Object
93 94 95 |
# File 'lib/studio_game/game.rb', line 93 def high_score_entry(player) "#{player.name.ljust(10,'.')} #{player.score}\n" end |
#load_players ⇒ Object
86 87 88 89 90 91 |
# File 'lib/studio_game/game.rb', line 86 def load_players default_player_file = File.join(File.dirname(__FILE__), 'players.csv') File.readlines(ARGV.shift || default_player_file).each do |line| add_player(Player.from_csv(line)) end end |
#play(rounds) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/studio_game/game.rb', line 51 def play(rounds) if block_given? yield else puts "There are #{@players.size} players in #{@title}: " @players.each do |player| puts player end 1.upto(rounds) do |round| puts "\\nRound #{round}:" @players.each do |player| GameTurn.take_turn(player) end end treasures = TreasureTrove::TREASURES treasures.each do |treasure| puts "A #{treasure.name} is worth #{treasure.points} points" end end end |
#print_stats ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/studio_game/game.rb', line 17 def print_stats puts "Strong players" strong_players.each do |player2| puts "#{player2.name} health #{player2.health}" end puts "Wimpy players" wimpy_players.each do |player| puts "#{player.name} health #{player.health}" end @players.sort.each do |player| puts "#{player.name} point totals:" puts high_score_entry(player) player.each_found_treasure do |treasure| puts "treasure name #{treasure.name} with #{treasure.points} points" end puts "#{player.points} grand total points" end end |
#save_high_scores ⇒ Object
77 78 79 80 81 82 83 84 |
# File 'lib/studio_game/game.rb', line 77 def save_high_scores File.open(ARGV.shift || "highscores.txt",mode="w") do |file| file.write "#{@title} High Scores:\n" @players.sort.each do |player| file.write(high_score_entry(player)) end end end |
#strong_players ⇒ Object
42 43 44 45 |
# File 'lib/studio_game/game.rb', line 42 def strong_players players = @players.select {|player| player.strong?} players end |
#to_s ⇒ Object
47 48 49 |
# File 'lib/studio_game/game.rb', line 47 def to_s puts "There are #{@players.length} players in #{title}:" end |
#total_points ⇒ Object
73 74 75 |
# File 'lib/studio_game/game.rb', line 73 def total_points @players.reduce(0) {|sum,player| sum +player.points} end |
#wimpy_players ⇒ Object
37 38 39 40 |
# File 'lib/studio_game/game.rb', line 37 def wimpy_players players = @players.reject {|player| player.strong?} players end |