Class: MasmaGame::Game
- Inherits:
-
Object
- Object
- MasmaGame::Game
- Defined in:
- lib/masma_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(p) ⇒ Object
-
#initialize(name) ⇒ Game
constructor
A new instance of Game.
- #load_players(from_file) ⇒ Object
- #play(rounds) ⇒ Object
- #print_stats ⇒ Object
- #save_high_scores(to_file = "high_scores.txt") ⇒ Object
- #total_points ⇒ Object
Constructor Details
#initialize(name) ⇒ Game
Returns a new instance of Game.
10 11 12 13 |
# File 'lib/masma_game/game.rb', line 10 def initialize(name) @name = name.capitalize @player = [] end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
8 9 10 |
# File 'lib/masma_game/game.rb', line 8 def name @name end |
Instance Method Details
#add_player(player) ⇒ Object
15 16 17 |
# File 'lib/masma_game/game.rb', line 15 def add_player(player) @player << player end |
#high_score_entry(p) ⇒ Object
23 24 25 26 |
# File 'lib/masma_game/game.rb', line 23 def high_score_entry(p) formatted_name = p.name.ljust(20, '.') "#{formatted_name} #{p.score}" end |
#load_players(from_file) ⇒ Object
85 86 87 88 89 |
# File 'lib/masma_game/game.rb', line 85 def load_players(from_file) File.readlines(from_file).each do |line| add_player(Player.from_csv(line)) end end |
#play(rounds) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/masma_game/game.rb', line 28 def play(rounds) puts "There are #{@player.size} players in #{@name} game" @player.each do |player| puts player end hoards = HoardStore::HOARDS puts "\nThere are #{hoards.size} hoards to be found" hoards.each do |hoard| puts "A #{hoard.name} is worth #{hoard.points} points" end puts "---------------------------------" 1.upto(rounds) do |n| puts "\nRound #{n}" @player.each do |p| NextGame.next_player(p) hoard = HoardStore.random puts "#{p.name} found a #{hoard.name} worth #{hoard.points} points" puts p end end 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 81 82 83 |
# File 'lib/masma_game/game.rb', line 54 def print_stats puts "\n#{@name} Statistics:" puts "#{total_points} total points from hoards found" @player.each do |p| puts "\n#{p.name}'s point totals" p.each_found_hoard do |hoard| puts "#{hoard.points} total #{hoard.name} points" end puts "#{p.points} grand total points" end strong, wimpy = @player.partition {|p| p.strong?} puts "\n#{strong.size} Strong Players:" strong.each do |p| puts "#{p.name} (#{p.health})" end puts "\n#{wimpy.size} Wimpy Players:" wimpy.each do |p| puts "#{p.name} (#{p.health})" end puts "\n#{@name} High Scores:" @player.sort.each do |p| puts "#{p.name.ljust(25, '-')} #{p.score}" end end |
#save_high_scores(to_file = "high_scores.txt") ⇒ Object
91 92 93 94 95 96 97 98 |
# File 'lib/masma_game/game.rb', line 91 def save_high_scores(to_file="high_scores.txt") File.open(to_file, "w") do |file| file.puts "#{@name} High Scores:" @player.sort.each do |p| file.puts high_score_entry(p) end end end |
#total_points ⇒ Object
19 20 21 |
# File 'lib/masma_game/game.rb', line 19 def total_points @player.reduce(0) { |sum, player| sum + player.points} end |