Class: StudioGame::Game
- Inherits:
-
Object
- Object
- StudioGame::Game
- Defined in:
- lib/studio_game/game.rb
Instance Method Summary collapse
- #add_player(player) ⇒ Object
- #formatted_scores ⇒ Object
-
#initialize(title) ⇒ Game
constructor
A new instance of Game.
- #load_players(csv_file) ⇒ Object
- #play(rounds) ⇒ Object
- #print_name_and_health(player) ⇒ Object
- #save_high_scores(file_name = 'high_scores.txt') ⇒ Object
- #show_statistics ⇒ Object
- #total_points ⇒ Object
Constructor Details
#initialize(title) ⇒ Game
Returns a new instance of Game.
8 9 10 11 |
# File 'lib/studio_game/game.rb', line 8 def initialize(title) @title = title @players = [] end |
Instance Method Details
#add_player(player) ⇒ Object
30 31 32 |
# File 'lib/studio_game/game.rb', line 30 def add_player(player) @players << player end |
#formatted_scores ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/studio_game/game.rb', line 109 def formatted_scores scores = "" # sort the players based on their score sorted_players = @players.sort sorted_players.each do |p| scores += "#{p.name}".ljust(25, '.') + "#{p.score}\n" end scores end |
#load_players(csv_file) ⇒ Object
13 14 15 16 17 18 19 20 |
# File 'lib/studio_game/game.rb', line 13 def load_players(csv_file) File.readlines(csv_file).each do |line| name, health = line.split(',') player = Player.new(name, Integer(health)) puts "Adding #{player.name} to the game" add_player(player) end end |
#play(rounds) ⇒ Object
34 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 |
# File 'lib/studio_game/game.rb', line 34 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 |t| puts "A #{t.name} is worth #{t.points} points" end 1.upto(rounds) do |round| if block_given? break if yield end puts "\nRound #{round}:" @players.each do |player| GameTurn.take_turn(player) end end end |
#print_name_and_health(player) ⇒ Object
99 100 101 |
# File 'lib/studio_game/game.rb', line 99 def print_name_and_health(player) puts "#{player.name} (#{player.health})" end |
#save_high_scores(file_name = 'high_scores.txt') ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/studio_game/game.rb', line 22 def save_high_scores(file_name='high_scores.txt') sorted_players = @players.sort File.open(file_name, 'w') do |file| file.puts "Knuckleheads High Scores:" file.puts formatted_scores end end |
#show_statistics ⇒ Object
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 93 94 95 96 97 |
# File 'lib/studio_game/game.rb', line 61 def show_statistics # separate the strong from wimpy players strong, wimpy = @players.partition {|p| p.strong?} puts "\n#{@title}'s Statistics:" # print out the strong players puts "\n#{strong.size} strong players:" strong.each do |p| print_name_and_health(p) end # print out the wimpy players puts "\n#{wimpy.size} wimpy players:" wimpy.each do |p| print_name_and_health(p) end # print out the high scores puts "\n#{@title}'s High Scores:" puts formatted_scores # print out the total points per player puts "\nPoint Totals:" @players.each do |p| puts "\n#{p.name}'s point totals:" p.each_found_treasure do |treasure| puts "#{treasure.points} total #{treasure.name} points" end puts "#{p.points} grand total points" end # print out the total treasure points found puts "\n#{total_points} total points from treasures found" end |
#total_points ⇒ Object
103 104 105 106 107 |
# File 'lib/studio_game/game.rb', line 103 def total_points @players.reduce(0) do |sum, player| sum += player.points end end |