Class: Game
- Inherits:
-
Object
- Object
- Game
- Defined in:
- lib/game.rb
Instance Attribute Summary collapse
-
#players ⇒ Object
readonly
Returns the value of attribute players.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Instance Method Summary collapse
- #add_player(player) ⇒ Object
-
#initialize(title) ⇒ Game
constructor
A new instance of Game.
- #load_player(file_path) ⇒ Object
- #play(count) ⇒ Object
- #print_name_and_health(player) ⇒ Object
- #print_stats ⇒ Object
- #save_high_score(file_name = "high_score.txt") ⇒ Object
- #total_points ⇒ Object
Constructor Details
#initialize(title) ⇒ Game
Returns a new instance of Game.
8 9 10 11 12 13 14 |
# File 'lib/game.rb', line 8 def initialize(title) @title = title @players = [] @strong = [] @wimpy = [] @files = [] end |
Instance Attribute Details
#players ⇒ Object (readonly)
Returns the value of attribute players.
7 8 9 |
# File 'lib/game.rb', line 7 def players @players end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
7 8 9 |
# File 'lib/game.rb', line 7 def title @title end |
Instance Method Details
#add_player(player) ⇒ Object
25 26 27 |
# File 'lib/game.rb', line 25 def add_player(player) @players.push(player) end |
#load_player(file_path) ⇒ Object
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/game.rb', line 15 def load_player(file_path) File.open(file_path) do |aFile| aFile.each_line do |line| @files<<line end end @files.each do |i| add_player(Player.from_csv(i)) end end |
#play(count) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/game.rb', line 60 def play(count) 1.upto(count.to_i) do |time| puts "Round #{time}" @players.each do |player| GameTurn.take_turn(player) puts player end end treasure = TreasureTrove::TREASURES puts "There are #{treasure.size} treasures to be found" treasure.each do |treasure| puts "A #{treasure.name} is worth #{treasure.point} points" end end |
#print_name_and_health(player) ⇒ Object
28 29 30 |
# File 'lib/game.rb', line 28 def print_name_and_health(player) puts "#{player.name}(#{player.health})" end |
#print_stats ⇒ Object
31 32 33 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/game.rb', line 31 def print_stats puts "#{@title} Statistatics :- \n\n" @strong,@wimpy = @players.partition{|player| player.strong?} puts "#{@strong.length} strong player:" @strong.each do |player| print_name_and_health(player) end puts "#{@wimpy.length} Wimpy player : " @wimpy.each do |player| print_name_and_health(player) end puts "#{@title} High Score : " @players.sort.each do |player| puts "#{player.name.ljust(10,".")}#{player.score}" end puts "Points Table" @players.each do |player| puts "#{player.name} points table:" puts "#{player.points} grand total !!" end puts total_points @players.each do |player| player.each_found_treasure do |treasure| puts "#{treasure.name.ljust(10,".")}#{treasure.point}" end end end |
#save_high_score(file_name = "high_score.txt") ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/game.rb', line 82 def save_high_score(file_name = "high_score.txt") file = File .open(file_name,"a") file.puts "knucleheads high scores!!!!" players.sort.each do |player| file.puts "#{player.name.ljust(10,".")}#{player.score}" end end |
#total_points ⇒ Object
74 75 76 77 78 79 80 81 |
# File 'lib/game.rb', line 74 def total_points @players.each do |player| player.found_treasures.each do |key,value| $total_points += value end end $total_points end |