Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch edge cases where nearest_human is nil #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions models/board.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,25 @@ def next_turn
nearest_zombie = nearest_humanoid(humanoid, :zombie)
nearest_human = nearest_humanoid(humanoid, :human)
if humanoid.type == :zombie
if humanoid.distance_to(nearest_human.position) < humanoid.distance_to(nearest_zombie.position) * 6
if !nearest_human
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should really be unless nearest_human

destination = humanoid.move_nearest(nearest_zombie)
elsif humanoid.distance_to(nearest_human.position) < humanoid.distance_to(nearest_zombie.position) * 6
destination = humanoid.move_nearest(nearest_human)
else
destination = humanoid.move_nearest(nearest_zombie)
end
elsif humanoid.type == :human
if humanoid.distance_to(nearest_zombie.position) < 50
destination = humanoid.move_nearest(nearest_zombie)
else
elsif nearest_human
destination = humanoid.move_nearest(nearest_human)
else
destination = humanoid.move_randomly
end
end
destination[:y] = destination[:y] % height
destination[:x] = destination[:x] % width
humanoid.bite nearest_human if humanoid.bites? and humanoid.distance_to(nearest_human.position) < 10
humanoid.bite nearest_human if humanoid.bites? and nearest_human and humanoid.distance_to(nearest_human.position) < 10
humanoid.position = destination if valid_destination?(destination)
end
humanoids.any? {|humanoid| humanoid.type == :human} ? humanoids : nil
Expand Down