Class: StudioGame::Game
- Inherits:
-
Object
- Object
- StudioGame::Game
- Defined in:
- lib/studio_game/game.rb
Instance Attribute Summary collapse
-
#title ⇒ Object
readonly
Returns the value of attribute title.
-
#total_game_points ⇒ Object
Returns the value of attribute total_game_points.
Instance Method Summary collapse
- #add_player(player) ⇒ Object
-
#initialize(title) ⇒ Game
constructor
A new instance of Game.
- #load_players(csvfile = 'players.csv') ⇒ Object
- #play(rounds = 5) ⇒ Object
- #print_high_scores ⇒ Object
- #print_name_and_health(player) ⇒ Object
- #print_name_and_score(player) ⇒ Object
- #print_stats ⇒ Object
- #print_treasures ⇒ Object
- #save_high_scores(highfile = 'high_scores.txt') ⇒ Object
- #show_game_status ⇒ Object
- #show_players ⇒ Object
Constructor Details
#initialize(title) ⇒ Game
Returns a new instance of Game.
14 15 16 17 18 |
# File 'lib/studio_game/game.rb', line 14 def initialize(title) @title = title.capitalize @players = [] @treasure = TreasureTrove::TREASURES end |
Instance Attribute Details
#title ⇒ Object (readonly)
Returns the value of attribute title.
11 12 13 |
# File 'lib/studio_game/game.rb', line 11 def title @title end |
#total_game_points ⇒ Object
Returns the value of attribute total_game_points.
12 13 14 |
# File 'lib/studio_game/game.rb', line 12 def total_game_points @total_game_points end |
Instance Method Details
#add_player(player) ⇒ Object
20 21 22 |
# File 'lib/studio_game/game.rb', line 20 def add_player(player) @players << player end |
#load_players(csvfile = 'players.csv') ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/studio_game/game.rb', line 104 def load_players(csvfile = 'players.csv') puts "\ncsvfile = #{csvfile}\n" filehandle = File.open(csvfile) players = [] index = 0 loop do currentline = filehandle.gets break if currentline == nil players[index] = currentline.chomp index += 1 end players.each do |player| temparray = player.split(',') entrant = Player.new(temparray[0].to_s,temparray[1].to_i) add_player(entrant) end end |
#play(rounds = 5) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/studio_game/game.rb', line 28 def play(rounds=5) print_treasures 1.upto(rounds) do |round| yield if block_given? puts "\n\nSTARTING ROUND #{round}" @players.each do |player| GameTurn.take_turn(player) end end end |
#print_high_scores ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/studio_game/game.rb', line 70 def print_high_scores sorted_players = @players.sort puts "\n\n" puts "Scores in Descending Order" sorted_players.each do |player| print_name_and_score(player) end end |
#print_name_and_health(player) ⇒ Object
83 84 85 |
# File 'lib/studio_game/game.rb', line 83 def print_name_and_health(player) puts "#{player.name} #{player.health.to_s.rjust(20,'.')}" end |
#print_name_and_score(player) ⇒ Object
79 80 81 |
# File 'lib/studio_game/game.rb', line 79 def print_name_and_score(player) puts "#{player.name} #{player.score.to_s.rjust(20,'.')}" end |
#print_stats ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/studio_game/game.rb', line 46 def print_stats strong_players, weak_players = @players.partition {|player| player.strong?} puts "\nReport on Player Health" puts "\n#{strong_players.size} strong players\n" strong_players.each do |player| print_name_and_health(player) end puts "\n#{weak_players.size} weak players\n" weak_players.each do |player| print_name_and_health(player) end puts "\n\n" puts "\nPoint Totals With a Custom Iterator\n" @players.each do |player| puts "\n#{player.name}\'s point totals:" player.each_found_treasure do |treasure| puts "#{treasure.points} total #{treasure.name} points" end end end |
#print_treasures ⇒ Object
87 88 89 90 91 92 |
# File 'lib/studio_game/game.rb', line 87 def print_treasures puts "There are #{@treasure.size} treasures to be found:" @treasure.each do |treasure| puts "A #{treasure.name} is worth #{treasure.points} points" end end |
#save_high_scores(highfile = 'high_scores.txt') ⇒ Object
126 127 128 |
# File 'lib/studio_game/game.rb', line 126 def save_high_scores(highfile = 'high_scores.txt') target = File.open(highfile, 'w') {|f| f.write(print_high_scores)} end |
#show_game_status ⇒ Object
41 42 43 44 |
# File 'lib/studio_game/game.rb', line 41 def show_game_status puts "\n\nGAME STATUS\nThere are #{@players.size} players in #{self.title}:" show_players end |
#show_players ⇒ Object
24 25 26 |
# File 'lib/studio_game/game.rb', line 24 def show_players puts @players end |