0

Class methods [ruby]

In the code below why is the method .fight defined outside the class? class Player attr_accessor :name, :health, :power def initialize(n, h, pow) @name = n @health = h @power = pow end def isAlive @health > 0 end def hit(opponent) opponent.health -= self.power end def to_s "#{name}: Health: #{health}, Power: #{power}" end end def fight(p1, p2) while p1.isAlive && p2.isAlive p1.hit(p2) p2.hit(p1) show_info(p1, p2) end if p1.isAlive puts "#{p1.name} WON!" elsif p2.isAlive puts "#{p2.name} WON!" else puts "TIE!" end end def show_info(*p) p.each { |x| puts x} end #initialize Players puts "PLAYERS INFO" p1 = Player.new("Player 1", 1+rand(100), 1+rand(20)) p2 = Player.new("Player 2", 1+rand(100), 1+rand(20)) #show Player info show_info(p1, p2) puts "LETS FIGHT!" fight(p1, p2)

13th Nov 2018, 5:26 PM
Ana Lopez
Ana Lopez - avatar
1 Resposta
+ 3
// Please, insert your code to solve a problem.
13th Nov 2018, 7:28 PM
program
program - avatar