Class: StudioGame::Game
- Inherits:
-
Object
- Object
- StudioGame::Game
- Defined in:
- lib/studio_game/game.rb
Instance Method Summary collapse
- #add_player(player) ⇒ Object
-
#initialize(name) ⇒ Game
constructor
initialize the state of the objecct.
- #load_players(filename) ⇒ Object
- #play(rounds) ⇒ Object
- #print_name_and_health(player) ⇒ Object
- #print_name_and_points(player) ⇒ Object
- #print_stats ⇒ Object
- #save_high_scores(filename = 'high_scores.txt') ⇒ Object
- #save_to_csv(player) ⇒ Object
- #total_points ⇒ Object
Constructor Details
#initialize(name) ⇒ Game
initialize the state of the objecct
9 10 11 12 |
# File 'lib/studio_game/game.rb', line 9 def initialize(name) ## initialize the state of the objecct @name = name @players = [] 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 |
#load_players(filename) ⇒ Object
33 34 35 36 37 |
# File 'lib/studio_game/game.rb', line 33 def load_players(filename) File.readlines(filename).each do |line| add_player(Player.from_csv(line)) end end |
#play(rounds) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/studio_game/game.rb', line 82 def play(rounds) treasure = TreasureTrove::TREASURES puts "There are #{treasure.size} treasures to be found:" treasure.each do |treasure| puts "#{treasure.name} worth #{treasure.points} points" end puts "\nThere are #{@players.size} players in the game:" puts @players # puts die.to_s 1.upto(rounds) do |round| puts "\nRound #{round}:" @players.each do |player| GameTurn.take_turn(player) end end end |
#print_name_and_health(player) ⇒ Object
39 40 41 |
# File 'lib/studio_game/game.rb', line 39 def print_name_and_health(player) puts "#{player.name} (#{player.health})" end |
#print_name_and_points(player) ⇒ Object
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/studio_game/game.rb', line 43 def print_name_and_points(player) puts "\n#{player.name}\'s point totals:" player.each_found_treasure do |treasure| # puts "#{treasure}" puts "#{treasure.points} total #{treasure.name} points." # puts "#{treasure.points} total #{treasure.name} points" end puts "#{player.points} grand total points" end |
#print_stats ⇒ Object
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 |
# File 'lib/studio_game/game.rb', line 54 def print_stats strong_players, weak_players = @players.partition { |player| player.strong? } puts "\nGame Statistics:" @players.each do |player| print_name_and_points(player) puts "\n#{total_points} total points from treasures found" end puts "\n#{strong_players.count} strong players:" strong_players.each do | player| print_name_and_health(player) end puts "\n#{weak_players.count} wimpy players:" weak_players.each do | player| print_name_and_health(player) end sorted_players = @players.sort sorted_players.each do |player| puts "#{player.name.ljust(20, '.')}#{player.score}" end end |
#save_high_scores(filename = 'high_scores.txt') ⇒ Object
24 25 26 27 28 29 30 31 |
# File 'lib/studio_game/game.rb', line 24 def save_high_scores(filename='high_scores.txt') File.open(filename, 'w') do |file| file.puts "\nKnuckleheads High Scores:" @players.sort.each do |player| file.puts save_to_csv(player) end end end |
#save_to_csv(player) ⇒ Object
18 19 20 21 |
# File 'lib/studio_game/game.rb', line 18 def save_to_csv(player) formatted_name = player.name.ljust(20, '.') "#{formatted_name} #{player.score}" 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 |