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(a_player) ⇒ Object
- #high_score_entry(player) ⇒ Object
-
#initialize(title) ⇒ Game
constructor
A new instance of Game.
- #load_players(from_file = "bin/villians.csv") ⇒ 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.
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(a_player) ⇒ Object
24 25 26 |
# File 'lib/studio_game/Game.rb', line 24 def add_player (a_player) @players.push(a_player) end |
#high_score_entry(player) ⇒ Object
55 56 57 58 |
# File 'lib/studio_game/Game.rb', line 55 def high_score_entry(player) formatted_name = player.name.ljust(25,'.') "#{formatted_name}#{player.score}" end |
#load_players(from_file = "bin/villians.csv") ⇒ Object
17 18 19 20 21 22 |
# File 'lib/studio_game/Game.rb', line 17 def load_players(from_file="bin/villians.csv") CSV.foreach(ARGV.shift || from_file) do |row| player = Player.new(row[0], row[1].to_i) add_player(player) end end |
#play(rounds) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/studio_game/Game.rb', line 28 def play(rounds) 1.upto(rounds) do |round| puts "\nROUND #{round}" puts "\nThere are #{@players.count} lactose-intolerant players in #{@title}. \nLet's drink some milk!" @players.each do |b| puts b 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 puts "\n" @players.each do |b| GameTurn.take_turn(b) puts b.follow_up end end end |
#print_name_and_health(player) ⇒ Object
51 52 53 |
# File 'lib/studio_game/Game.rb', line 51 def print_name_and_health(player) puts "#{player.name} (#{player.health})" end |
#print_stats ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/studio_game/Game.rb', line 69 def print_stats puts "\n#{@title} Statistics:" puts "\n" strong, wimpy = @players.partition { |p| p.strong? } puts "There are #{strong.size} strong players that don't shart that often:" strong.sort.each do |b| print_name_and_health(b) end puts "\nThere are #{wimpy.size} wimpy players that shart on a regular basis:" wimpy.sort.each do |b| print_name_and_health(b) end puts "\n#{title} High Scores:" @players.sort.each do |b| high_score_entry(b) end @players.each do |b| puts "\n#{b.name}'s point totals:" b.each_treasure_found do |t| puts "#{t.points} total #{t.name} points" end puts "#{b.points} grand total points" end puts "\nTotal Points Possible: #{total_points}" end |
#save_high_scores(to_file = "high_scores.txt") ⇒ Object
60 61 62 63 64 65 66 67 |
# File 'lib/studio_game/Game.rb', line 60 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 |b| file.puts high_score_entry(b) end end end |
#total_points ⇒ Object
100 101 102 103 104 105 106 |
# File 'lib/studio_game/Game.rb', line 100 def total_points @players.reduce(0) { |sum, player| sum + player.points} # all_points = @players.map { |player| player.points} # @players.map(&:points).inject(&:+) # @players.inject(0) { |sum, player| sum + player.points} #& = Proc.new{} end |