"Use the bigger number" in ruby?

Started by arnquist, February 10, 2012, 09:53:46 am

Previous topic - Next topic

arnquist

The code now is

distance = ($game_player.x - self.x).abs + ($game_player.y - self.y).abs

I want it to be

distance = ($game_player.x - self.x).abs + ($game_player.y - self.y).abs   
OR   
($game_variables[1] - self.x).abs + ($game_variables[2] - self.y).abs
Depending on which is bigger

How do I say that in actual code?

Blizzard

The simplest way is:

distance = [($game_player.x - self.x).abs + ($game_player.y - self.y).abs, ($game_variables[1] - self.x).abs + ($game_variables[2] - self.y).abs].max


The fastest way is:

distance = ($game_player.x - self.x).abs + ($game_player.y - self.y).abs
d2 = ($game_variables[1] - self.x).abs + ($game_variables[2] - self.y).abs
distance = d2 if d2 > distance

Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

arnquist