Class: StudioGame::Game
- Inherits:
-
Object
- Object
- StudioGame::Game
- Defined in:
- lib/studio_game/game.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
- #add_player(player) ⇒ Object
- #high_score_entry(player) ⇒ Object
-
#initialize(name) ⇒ Game
constructor
A new instance of Game.
- #load_players(file_to_load) ⇒ Object
- #play(rounds_number) ⇒ Object
- #print_name_and_health(player) ⇒ Object
- #print_stats ⇒ Object
- #save_high_scores(file_to_save) ⇒ Object
- #total_points ⇒ Object
Constructor Details
#initialize(name) ⇒ Game
Returns a new instance of Game.
9 10 11 12 |
# File 'lib/studio_game/game.rb', line 9 def initialize(name) @name = name.capitalize @players = [] end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
7 8 9 |
# File 'lib/studio_game/game.rb', line 7 def name @name end |
Instance Method Details
#add_player(player) ⇒ Object
14 15 16 |
# File 'lib/studio_game/game.rb', line 14 def add_player(player) @players << player end |
#high_score_entry(player) ⇒ Object
18 19 20 |
# File 'lib/studio_game/game.rb', line 18 def high_score_entry(player) "#{player.name.ljust(15, '.')} #{player.score}" end |
#load_players(file_to_load) ⇒ Object
22 23 24 25 26 27 |
# File 'lib/studio_game/game.rb', line 22 def load_players(file_to_load) CSV.foreach(file_to_load) do |row| player = Player.new(row[0], Integer(row[1])) add_player(player) end end |
#play(rounds_number) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/studio_game/game.rb', line 40 def play(rounds_number) puts "The game #{@name} has been started with #{@players.length} players" treasures = TreasureTrove::TREASURES puts "There are #{treasures.length} treasures" treasures.each do |treasure| puts "a #{treasure.name} worth #{treasure.point} points" end 1.upto(rounds_number) do |number| puts "\nRound #{number} Started" @players.each do |player| GameTurn.take_turn(player) end if block_given? break if yield end puts @players end end |
#print_name_and_health(player) ⇒ Object
62 63 64 |
# File 'lib/studio_game/game.rb', line 62 def print_name_and_health(player) puts "#{player.name} (#{player.health})" 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 |
# File 'lib/studio_game/game.rb', line 74 def print_stats puts "\n#{@name} statistics" puts "\nTotal treasures points found in the game" puts "#{total_points} total points" puts "\nTotal each player grand points" @players.each do |player| puts "\n#{player.name} #{player.points} total points" player.each_found_treasure do |treasure| puts "#{treasure.name} with the total of #{treasure.point}" end end strong, wimpy = @players.partition do |player| player.strong? end puts "\n#{strong.length} strong players" strong.each do |player| print_name_and_health(player) end puts "\n#{wimpy.length} wimpy players" wimpy.each do |player| print_name_and_health(player) end puts "\n#{@name} High Scores" sorted_players = @players.sort sorted_players.each do |player| puts high_score_entry(player) end end |
#save_high_scores(file_to_save) ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/studio_game/game.rb', line 29 def save_high_scores(file_to_save) File.open(file_to_save, "w") do |file| file.puts "\n#{@name} High Scores" sorted_players = @players.sort sorted_players.each do |player| file.puts high_score_entry(player) end end end |
#total_points ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/studio_game/game.rb', line 66 def total_points points = 0 @players.each do |player| points += player.points end points end |