Skip to content

Commit

Permalink
Fix edge case on swarm size calculation.
Browse files Browse the repository at this point in the history
Fixes #12
  • Loading branch information
SKoschnicke committed Oct 10, 2018
1 parent 7c7b4d0 commit 37d5f92
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
8 changes: 8 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
= 19.0.1

- fixed bug in swarm size calculation (thanks to wollw!)

= 19.0.0

First version for game "Piranhas"

= 1.2.1

- fixed a bug which could lead to an infinite loop in the possible_move method
Expand Down
5 changes: 3 additions & 2 deletions lib/software_challenge_client/game_rule_logic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,9 @@ def self.neighbours(board, field)
# @param current_biggest_swarm [Set<Field>] Aktuell größte zusammenhängende Feldmenge. Für rekursiven Aufruf.
# @return [Set<Field>]
def self.greatest_swarm_from_fields(board, fields_to_check, current_biggest_swarm = Set.new)
# stop searching when the size of the current found biggest set is bigger than the rest of the fields
return current_biggest_swarm if current_biggest_swarm.size > fields_to_check.size
# stop searching when the size of the current found biggest set is bigger
# than the rest of the fields or if there are no more fields to check
return current_biggest_swarm if current_biggest_swarm.size > fields_to_check.size || fields_to_check.empty?

# start a new set of adjacent fields with the first field in fields_to_check
current_swarm = Set.new
Expand Down
2 changes: 1 addition & 1 deletion lib/software_challenge_client/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# encoding: UTF-8
module SoftwareChallengeClient
VERSION = "19.0.0"
VERSION = "19.0.1"
end
20 changes: 20 additions & 0 deletions spec/game_rule_logic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,24 @@
expect(subject.swarm_size(gamestate.board, PlayerColor::BLUE)).to eq(10)
end

it 'calculates correct swarm size with very few fishes' do
# this is an edge case to capture Issue #12
field =
<<~FIELD
~ R ~ ~ ~ ~ ~ ~ ~ ~
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
~ ~ ~ ~ ~ O ~ ~ ~ ~
~ ~ ~ ~ ~ O ~ ~ ~ ~
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
FIELD
state_from_string!(field, gamestate)
expect(subject.swarm_size(gamestate.board, PlayerColor::RED)).to eq(1)
expect(subject.swarm_size(gamestate.board, PlayerColor::BLUE)).to eq(0)
end

end

0 comments on commit 37d5f92

Please sign in to comment.