0
I really don't get what's wrong with my code
4 Answers
+ 2
you are calling class methods without an instance or using the class name (static methods in other languages)
so add class name to the methods:
def Play.show_info(*z)
z.each {|x| puts x}
end
def Play.fight(p1,p2)
while p1.is_alive && p2.is_alive
p1.hit(p2)
p2.hit(p1)
show_info(p1,p2)
end
end
def Play.result(p1,p2)
if p1.is_alive
puts "WINNER IS #{p1.name}"
elsif p2.is_alive
puts "WINNER IS #{p2.name}"
else
puts "TIE"
end
end
end
and call them like:
Play.show_info(x1, x2)
Play.fight(x1, x2)
Play.result(x1, x2)
+ 1
Einstein Shilly shally
because in the sololearn code the functions are not part of the class they are just normal functions.
in your code they are methods of the Player class.
+ 1
Thank you so much . I see it now
0
Thank you Bahhađ§ but why isn't this happening in sololearn's code ? That code can be run without the class name