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
-
#initialize(name) ⇒ Game
constructor
A new instance of Game.
- #load_players(file_name) ⇒ Object
- #play(rounds) ⇒ Object
- #print_stats ⇒ Object
- #save_high_scores(file_name = "high_scores.txt") ⇒ Object
- #total_points ⇒ Object
Constructor Details
#initialize(name) ⇒ Game
Returns a new instance of Game.
12 13 14 15 |
# File 'lib/studio_game/game.rb', line 12 def initialize(name) @title = name @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(player) ⇒ Object
39 40 41 |
# File 'lib/studio_game/game.rb', line 39 def add_player(player) @players << player end |
#load_players(file_name) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/studio_game/game.rb', line 19 def load_players(file_name) File.readlines(file_name).each do |line| #puts line add_player(Player.from_csv(line)) end end |
#play(rounds) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/studio_game/game.rb', line 43 def play(rounds) puts "There are #{@players.size} players in #{@title} :" @players.each do |p| puts p end treasures = TreasureTrove::TREASURES puts("\nWe have #{treasures.size} treasures") treasures.each do |treasure| puts("A #{treasure.name} is worth #{treasure.points} points") end 1.upto(rounds) do |round| if block_given? break if yield end puts "\n Round : #{round}" @players.each do |p| GameTurn.take_turn(p) puts p end end end |
#print_stats ⇒ Object
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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/studio_game/game.rb', line 74 def print_stats sorted_players = @players.sort { |a, b| b.score <=> a.score } puts "\n#{@title} High Scores:" # sorted_players.each do |p| # formatted_name = p.name.ljust(20, '.') # puts "#{formatted_name} #{p.score}" # end @players.sort.each do |p| puts p.high_score_entry end @players.sort.each do |p| puts "\n#{p.name}'s point totals:" p.each_found_treasure do |t| puts "#{t.points} total #{t.name} points" end puts "#{p.points} grand total points" end strong, wimpy = @players.partition { |p| p.strong?} puts("\n Strong players:") strong.each do |p| p.printStats end puts("\n Wimpy players:") wimpy.each do |p| p.printStats end puts("Total treasure points found during game = #{total_points}") end |
#save_high_scores(file_name = "high_scores.txt") ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/studio_game/game.rb', line 28 def save_high_scores(file_name = "high_scores.txt") File.open(file_name, "w") do |file| file.puts "#{title} High Scores:" @players.sort.each do |p| file.puts p.high_score_entry end end end |
#total_points ⇒ Object
69 70 71 72 |
# File 'lib/studio_game/game.rb', line 69 def total_points @players.reduce(0) { |sum, player| sum + player.points} end |