Class: TextGame::Game
- Inherits:
-
Object
- Object
- TextGame::Game
- Defined in:
- lib/text_game/game.rb
Instance Method Summary collapse
- #add_player(player) ⇒ Object
-
#initialize(name) ⇒ Game
constructor
A new instance of Game.
- #load(file_name) ⇒ Object
- #play(rounds = 1) ⇒ Object
- #print_highscores ⇒ Object
- #print_stats ⇒ Object
- #save(to_file = "player_scores.csv") ⇒ Object
- #total_points_accumulated ⇒ Object
Constructor Details
#initialize(name) ⇒ Game
Returns a new instance of Game.
8 9 10 11 |
# File 'lib/text_game/game.rb', line 8 def initialize name @name = name.capitalize @players = [] end |
Instance Method Details
#add_player(player) ⇒ Object
13 14 15 |
# File 'lib/text_game/game.rb', line 13 def add_player player @players.push player end |
#load(file_name) ⇒ Object
39 40 41 42 43 |
# File 'lib/text_game/game.rb', line 39 def load file_name File.readlines(file_name).each do |line| add_player Player.from_csv(line) end end |
#play(rounds = 1) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/text_game/game.rb', line 17 def play rounds=1 puts "There are #{@players.size} players in #{@name}" puts @players.sort treasures = TreasureTrove::TREASURES puts "\nThere are #{treasures.size} treasures available in the trove" treasures.each { |treasure| puts "#{treasure.name.capitalize} has #{treasure.points} points."} rounds.times do |count| puts "\nRound #{count+1}" #game round @players.each do |player| GameLogic.check player treasure = TreasureTrove.random player.found_treasure treasure puts player end #@players.each { |player| puts player} end end |
#print_highscores ⇒ Object
81 82 83 84 85 86 87 88 89 |
# File 'lib/text_game/game.rb', line 81 def print_highscores puts "\n#{@name} High Scores:" sorted = @players.sort sorted.each do |player| puts "#{player.name}".ljust(30,'.')+"#{player.score}" end 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 |
# File 'lib/text_game/game.rb', line 57 def print_stats puts "\n#{@name} Statistics:" puts "#{total_points_accumulated} total points accumulated" @players.sort.each do |player| puts "\n#{player.name}'s point totals:" player.each_treasure do |treasure| puts "#{treasure.name} worth #{treasure.points} points" end puts "#{player.points_accumulated} grand total points" end strong, weak = @players.partition { |player| player.strong?} puts "\nStrong:" puts strong.sort puts "\nWeak:" puts weak.sort end |
#save(to_file = "player_scores.csv") ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/text_game/game.rb', line 45 def save to_file="player_scores.csv" File.open(to_file,"w") do |file| @players.sort.each do |player| file.puts player.to_csv end end end |
#total_points_accumulated ⇒ Object
53 54 55 |
# File 'lib/text_game/game.rb', line 53 def total_points_accumulated @players.reduce(0) { |sum, player| sum + player.points_accumulated } end |