Class: StudioGame::Game
- Inherits:
-
Object
- Object
- StudioGame::Game
- Defined in:
- lib/studio_game/game.rb
Instance Attribute Summary collapse
-
#players ⇒ Object
writeonly
Sets the attribute players.
-
#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.
- #load_players(file_in) ⇒ Object
- #play(rounds) ⇒ Object
- #print_stats ⇒ Object
- #save_high_scores(file_out = "high_scores.txt") ⇒ Object
- #total_points ⇒ Object
Constructor Details
#initialize(title) ⇒ Game
Returns a new instance of Game.
11 12 13 14 |
# File 'lib/studio_game/game.rb', line 11 def initialize(title) @title = title @players = [] end |
Instance Attribute Details
#players=(value) ⇒ Object (writeonly)
Sets the attribute players
9 10 11 |
# File 'lib/studio_game/game.rb', line 9 def players=(value) @players = value end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
8 9 10 |
# File 'lib/studio_game/game.rb', line 8 def title @title end |
Instance Method Details
#add_player(player) ⇒ Object
31 32 33 |
# File 'lib/studio_game/game.rb', line 31 def add_player(player) @players << player end |
#load_players(file_in) ⇒ Object
16 17 18 19 20 |
# File 'lib/studio_game/game.rb', line 16 def load_players(file_in) File.readlines(file_in).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 53 54 55 56 57 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 |
# File 'lib/studio_game/game.rb', line 35 def play(rounds) puts "There are #{@players.size} 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) # die = Die.new # number_rolled = die.roll # puts "number on the die: #{number_rolled}" # case number_rolled # when 1..2 # player.blam # when 3..4 # puts "#{player.name} was skipped" # when 5..6 # player.w00ted # end # if number_rolled >= 5 # player.w00ted # puts "#{player.name} was w00ted" # elsif number_rolled >= 3 # puts "#{player.name} was skipped." # else # player.blam # puts "#{player.name} was blammed" # end puts player end if block_given? if yield puts "ending game early" break end end end end |
#print_stats ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/studio_game/game.rb', line 92 def print_stats strong_players, weak_players = @players.partition {|player| player.strong? } puts "\n#{title} Statistics:" @players.each do |player| puts "#{player.name}'s points totals:" puts "#{player.points} grand total points\n" player.each_found_treasure do |treasure| puts "#{treasure.points} total #{treasure.name} points" end end puts "Total treasure points: #{total_points}\n" puts "\n1 strong players:" strong_players.each do |player| puts "#{player.name} (#{player.health})" end puts "\n1 weak players:" weak_players.each do |player| puts "#{player.name} (#{player.health})" end #sorted_players = @players.sort {|a,b| b.score <=> a.score} #sorted_players.each do |player| puts "\n#{title} High Scores:" @players.sort.each do |player| puts player.formatted_name end end |
#save_high_scores(file_out = "high_scores.txt") ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/studio_game/game.rb', line 22 def save_high_scores(file_out="high_scores.txt") File.open(file_out, "w") do |file| file.puts "#{title} High Scores:" @players.sort.each do |player| file.puts player.formatted_name end end end |
#total_points ⇒ Object
128 129 130 131 132 |
# File 'lib/studio_game/game.rb', line 128 def total_points @players.reduce(0) do |sum, player | sum += player.points end end |