Class: JkimsGame::Game
- Inherits:
-
Object
- Object
- JkimsGame::Game
- Defined in:
- lib/jkims_game/game.rb
Instance Attribute Summary collapse
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Instance Method Summary collapse
- #add_player(player) ⇒ Object
-
#initialize(title) ⇒ Game
constructor
A new instance of Game.
- #loop_players(from_file) ⇒ Object
- #play(rounds) ⇒ Object
- #print_name_and_health(player) ⇒ Object
- #print_stats ⇒ Object
- #save_high_scores(filename = "high_scores.txt") ⇒ Object
- #total_points ⇒ Object
Constructor Details
#initialize(title) ⇒ Game
Returns a new instance of Game.
10 11 12 13 |
# File 'lib/jkims_game/game.rb', line 10 def initialize(title) @title = title @players = [] end |
Instance Attribute Details
#title ⇒ Object (readonly)
Returns the value of attribute title.
9 10 11 |
# File 'lib/jkims_game/game.rb', line 9 def title @title end |
Instance Method Details
#add_player(player) ⇒ Object
31 32 33 |
# File 'lib/jkims_game/game.rb', line 31 def add_player(player) @players << player end |
#loop_players(from_file) ⇒ Object
15 16 17 18 19 |
# File 'lib/jkims_game/game.rb', line 15 def loop_players(from_file) File.readlines(from_file).each do |line| add_player (Player.from_csv(line)) end end |
#play(rounds) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/jkims_game/game.rb', line 35 def play(rounds) puts "There are #{@players.length} players in #{@title}:" @players.each do |player| puts player end treasures = TreasureTrove::TREASURES puts "\nThere are #{treasures.size} treasures to be found:" treasures.each do |treasure| puts "A #{treasure.name} is worth #{treasure.points} points" end 1.upto(rounds) do |round| puts "\nRound: #{round}:" @players.each do |player| GameTurn.take_turn(player) puts player end end end |
#print_name_and_health(player) ⇒ Object
54 55 56 |
# File 'lib/jkims_game/game.rb', line 54 def print_name_and_health(player) "#{player.name} (#{player.health})" end |
#print_stats ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/jkims_game/game.rb', line 62 def print_stats @players.sort.each do |player| puts "#{player.name.ljust(20, ".")}#{player.score.to_s.rjust(3)}" end puts "\n#{@title} Statistics:" strong, wimpy = @players.partition { |player| player.strong? } puts "\n#{strong.length} strong players:" strong.each { |s| puts print_name_and_health(s)} puts "\n#{wimpy.length} wimpy players:" wimpy.each { |w| puts print_name_and_health(w)} @players.each do |player| puts "#{player.name}'s total points:" player.each_found_treasure { |treasure| puts "#{treasure.points} total #{treasure.name} points"} puts "#{player.points} grand total points" end puts "#{total_points} total points from treasures found" end |
#save_high_scores(filename = "high_scores.txt") ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/jkims_game/game.rb', line 21 def save_high_scores(filename = "high_scores.txt") File.open(filename, "w") do |file| file.puts "#{@title} High Scores:" @players.sort.each do |player| file.puts "#{player.name.ljust(20, ".")}#{player.score.to_s.rjust(3)}" end end end |
#total_points ⇒ Object
58 59 60 |
# File 'lib/jkims_game/game.rb', line 58 def total_points @players.reduce(0) {|sum, player| sum + player.points} end |