Class: StudioGame::Game
- Inherits:
-
Object
- Object
- StudioGame::Game
- Defined in:
- lib/studio_game/game.rb
Instance Attribute Summary collapse
-
#title ⇒ Object
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) ⇒ Object
- #play(rounds) ⇒ Object
- #print_high_scores(player) ⇒ Object
- #print_name_and_health(player) ⇒ Object
- #print_stats ⇒ Object
- #save_high_scores(file = "high_scores_revised.txt") ⇒ Object
Constructor Details
#initialize(title) ⇒ Game
Returns a new instance of Game.
13 14 15 16 |
# File 'lib/studio_game/game.rb', line 13 def initialize(title) @title = title @players = [] end |
Instance Attribute Details
#title ⇒ Object
Returns the value of attribute title.
11 12 13 |
# File 'lib/studio_game/game.rb', line 11 def title @title end |
Instance Method Details
#add_player(player) ⇒ Object
18 19 20 21 |
# File 'lib/studio_game/game.rb', line 18 def add_player(player) @players.push(player) # puts @players end |
#load_players(file) ⇒ Object
95 96 97 98 99 |
# File 'lib/studio_game/game.rb', line 95 def load_players(file) File.readlines(file).each do |line| add_player(Player.from_csv(line)) end end |
#play(rounds) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/studio_game/game.rb', line 23 def play(rounds) puts "Let's play Knuckleheads!" puts "\nLooks like we have #{@players.size} players this game:" @players.each do |player| formatted_name = player.name.ljust(20, '.') puts "#{formatted_name} #{player.health} HP" end treasures = TreasureTrove::TREASURES puts "\nThere are #{treasures.count} treasures in the Treasure Trove:" treasures.each do |treasure| puts "A #{treasure.name} is worth #{treasure.points} HP" end 1.upto(rounds) do |round| puts "\nRound #{round}:" @players.each do |player| GameTurn.take_turn(player) puts "#{player}" puts "\n" end end end |
#print_high_scores(player) ⇒ Object
52 53 54 55 |
# File 'lib/studio_game/game.rb', line 52 def print_high_scores(player) formatted_name = player.name.ljust(20, '.') "#{formatted_name} #{player.score}" end |
#print_name_and_health(player) ⇒ Object
91 92 93 |
# File 'lib/studio_game/game.rb', line 91 def print_name_and_health(player) puts "#{player.name} (#{player.health}" end |
#print_stats ⇒ Object
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 |
# File 'lib/studio_game/game.rb', line 57 def print_stats puts "\n*** Knucklehead Statistics ***" strong_players, wimpy_players = @players.partition { |player| player.strong? } puts "\n#{strong_players.count} strong players:" strong_players.each do |player| puts "#{player.name} (#{player.health})" end puts "\n#{wimpy_players.count} wimpy players:" wimpy_players.each do |player| puts "#{player.name} (#{player.health})" end puts "\n#{title} Final Ranking:" @players.sort.each do |player| puts print_high_scores(player) end @players.each do |player| puts "\n#{player.name}'s Point Breakdown" player.each_found_treasure do |treasure| puts "+ #{treasure.points} total #{treasure.name} points" end puts "= #{player.points} Treasure Points" puts "+ #{player.health} HP" puts "= #{player.score} Total Game Points" end end |
#save_high_scores(file = "high_scores_revised.txt") ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/studio_game/game.rb', line 101 def save_high_scores(file="high_scores_revised.txt") File.open(file, "w") do |file| file.puts "#{@title} High Score" @players.sort.each do |player| file.puts print_high_scores(player) end end end |