Class: StudioGame::Game
- Inherits:
-
Object
- Object
- StudioGame::Game
- Defined in:
- lib/studio_game/game.rb
Instance Attribute Summary collapse
-
#game ⇒ Object
readonly
Returns the value of attribute game.
Instance Method Summary collapse
- #add_player(a_player) ⇒ Object
- #high_score_entry(player) ⇒ Object
-
#initialize(game) ⇒ Game
constructor
A new instance of Game.
- #load_players(from_file) ⇒ Object
- #play(turn) ⇒ Object
- #print_status ⇒ Object
- #save_high_scores(to_file = "high_scores.txt") ⇒ Object
- #total_points ⇒ Object
Constructor Details
#initialize(game) ⇒ Game
Returns a new instance of Game.
12 13 14 15 |
# File 'lib/studio_game/game.rb', line 12 def initialize game @game = game @players = [] end |
Instance Attribute Details
#game ⇒ Object (readonly)
Returns the value of attribute game.
10 11 12 |
# File 'lib/studio_game/game.rb', line 10 def game @game end |
Instance Method Details
#add_player(a_player) ⇒ Object
17 18 19 |
# File 'lib/studio_game/game.rb', line 17 def add_player a_player @players.push(a_player) end |
#high_score_entry(player) ⇒ Object
53 54 55 56 |
# File 'lib/studio_game/game.rb', line 53 def high_score_entry(player) formatted_name = player.name.ljust(20, '.') "#{player.name},total #{player.points} " end |
#load_players(from_file) ⇒ Object
21 22 23 24 25 |
# File 'lib/studio_game/game.rb', line 21 def load_players(from_file) File.foreach(from_file) do |line| add_player(Player.from_csv(line)) end end |
#play(turn) ⇒ Object
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 27 def play (turn) puts "There are #{@players.size} players in #{@game}:\n" puts @players.sort swag = SwagBag::SWAGS puts " \n THere are #{swag.size} pieces of Swag here." swag.each do |swag| puts "#{swag.name} has #{swag.points} value" end # figure out how to add up all the points for swag box 1.upto(turn) do |count| puts "\nTurn number #{count}:" @players.each do |player| GameTurn.take_turn(player) treat = SwagBag.random puts "#{player.name} got a #{treat.name} worth $#{treat.points}" # puts player end end end |
#print_status ⇒ Object
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 86 87 88 89 90 91 92 |
# File 'lib/studio_game/game.rb', line 58 def print_status weak, strong = @players.partition { |player| player.strong?} puts "\nWeak:" puts weak.sort puts "\nStrong:" puts strong.sort puts "\n$#{total_points} of swag." sorted_players = @players.sort puts "\n#{@title} High Scores:" @players.sort.each do |player| puts high_score_entry(player) end 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 |player| file.puts high_score_entry(player) end end end puts "\n#{@game} High Score Winner: " sorted_players.each do |player| puts "\n#{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." end end |
#save_high_scores(to_file = "high_scores.txt") ⇒ Object
75 76 77 78 79 80 81 82 |
# File 'lib/studio_game/game.rb', line 75 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 |player| file.puts high_score_entry(player) end end end |
#total_points ⇒ Object
49 50 51 |
# File 'lib/studio_game/game.rb', line 49 def total_points @players.reduce(0) { |sum, player| sum + player.points} end |