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
- #addplayers(player) ⇒ Object
- #high_scores_entry(player) ⇒ Object
-
#initialize(gametitle) ⇒ Game
constructor
A new instance of Game.
- #load_players(file) ⇒ Object
- #play(rounds) ⇒ Object
- #print_name_health(player) ⇒ Object
- #print_stats ⇒ Object
- #save_high_scores(file = "high_scores.txt") ⇒ Object
- #total_points ⇒ Object
Constructor Details
#initialize(gametitle) ⇒ Game
Returns a new instance of Game.
11 12 13 14 |
# File 'lib/studio_game/game.rb', line 11 def initialize(gametitle) @title = gametitle @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
#addplayers(player) ⇒ Object
16 17 18 |
# File 'lib/studio_game/game.rb', line 16 def addplayers(player) @players << player end |
#high_scores_entry(player) ⇒ Object
107 108 109 110 111 |
# File 'lib/studio_game/game.rb', line 107 def high_scores_entry(player) "#{player.name.ljust(20,".")} #{player.score}" end |
#load_players(file) ⇒ Object
20 21 22 23 24 25 |
# File 'lib/studio_game/game.rb', line 20 def load_players(file) CSV.foreach(file) do |registro| player = Player.new(registro[0],Integer(registro[1])) addplayers(player) end end |
#play(rounds) ⇒ Object
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 |
# File 'lib/studio_game/game.rb', line 39 def play(rounds) puts "There are #{@players.size} in #{self.title}:" puts @players treasure = TreasureTrove::TREASURES puts "\n The are #{treasure.size} treasure to be found" treasure.each do |treasure| puts "A #{treasure.name} is worth #{treasure.points} points" end 1.upto(rounds) do |round| puts "\n Round number: #{round} " @players.each do |player| GameTurn.take_turn(player) puts player end if block_given? if yield(total_points) puts "\n\n ending game 2000+ points reached #{total_points}" break end end end end |
#print_name_health(player) ⇒ Object
68 69 70 |
# File 'lib/studio_game/game.rb', line 68 def print_name_health(player) player.name + "....." + player.health.to_s end |
#print_stats ⇒ Object
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 99 100 101 102 103 104 105 |
# File 'lib/studio_game/game.rb', line 72 def print_stats puts "Statistics...." strong, wimpy = @players.partition {|player| player.strong?} puts "\n Strong Players:" strong.each { |player| puts self.print_name_health(player) } puts "\n Wimpy Players:" wimpy.each { |player| puts self.print_name_health(player) } #sorted_players = @players.sort {|p1,p2| p2.score <=> p1.score} @players.sort.each do |player| puts "\n #{player.name}'s points totals:" player.each_found_treasure do |treasure| puts "#{treasure.points} totals #{treasure.name} points" end puts "\n #{player.points} grand totals..." end puts "Players Total Points" @players.each do |player| puts "\n #{player.name}'s point totals:" puts "#{player.points} grand total points" end puts "\n #{self.title} High Scores:" @players.sort.each do |player| puts high_scores_entry(player) end end |
#save_high_scores(file = "high_scores.txt") ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/studio_game/game.rb', line 27 def save_high_scores (file="high_scores.txt") File.open(file,"w") do |file| file.puts "#{@title} High Scores:" @players.sort.each do |player| file.puts high_scores_entry(player) end end end |
#total_points ⇒ Object
115 116 117 118 119 |
# File 'lib/studio_game/game.rb', line 115 def total_points @players.reduce(0) do |sum,player| sum += player.points end end |