From 37d5f92ff0ea56944f69ecb2f42329ebac33f459 Mon Sep 17 00:00:00 2001 From: Sven Koschnicke Date: Wed, 10 Oct 2018 11:04:35 +0200 Subject: [PATCH] Fix edge case on swarm size calculation. Fixes #12 --- RELEASES.md | 8 ++++++++ .../game_rule_logic.rb | 5 +++-- lib/software_challenge_client/version.rb | 2 +- spec/game_rule_logic_spec.rb | 20 +++++++++++++++++++ 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/RELEASES.md b/RELEASES.md index e7d43e0..d387165 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -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 diff --git a/lib/software_challenge_client/game_rule_logic.rb b/lib/software_challenge_client/game_rule_logic.rb index d664b68..db422b5 100644 --- a/lib/software_challenge_client/game_rule_logic.rb +++ b/lib/software_challenge_client/game_rule_logic.rb @@ -196,8 +196,9 @@ def self.neighbours(board, field) # @param current_biggest_swarm [Set] Aktuell größte zusammenhängende Feldmenge. Für rekursiven Aufruf. # @return [Set] 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 diff --git a/lib/software_challenge_client/version.rb b/lib/software_challenge_client/version.rb index 735e1e0..28a3084 100644 --- a/lib/software_challenge_client/version.rb +++ b/lib/software_challenge_client/version.rb @@ -1,4 +1,4 @@ # encoding: UTF-8 module SoftwareChallengeClient - VERSION = "19.0.0" + VERSION = "19.0.1" end diff --git a/spec/game_rule_logic_spec.rb b/spec/game_rule_logic_spec.rb index 386f27e..9eeda8c 100644 --- a/spec/game_rule_logic_spec.rb +++ b/spec/game_rule_logic_spec.rb @@ -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