Skip to content
This repository has been archived by the owner on Nov 29, 2017. It is now read-only.

Commit

Permalink
Added Chain merging
Browse files Browse the repository at this point in the history
  • Loading branch information
vapour101 committed Aug 18, 2017
1 parent b645bcc commit 351046a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/main/java/logic/Chain.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,32 @@ protected Set<Coords> getLiberties() {
protected boolean isAdjacentTo(Coords coords) {
return liberties.contains(coords);
}

protected boolean isAdjacentTo(Chain other) {
for (Coords lib : liberties)
if (other.contains(lib))
return true;

return false;
}

protected int size() {
return stones.size();
}

private void clear() {
stones.clear();
liberties.clear();
}

protected void mergeChain(Chain other) {
if (!isAdjacentTo(other))
throw new IllegalArgumentException("Chains are not adjacent and cannot be merged.");

stones.addAll(other.stones);

other.clear();

recalculateLiberties();
}
}
27 changes: 27 additions & 0 deletions src/test/java/logic/ChainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,13 @@ public void contains() {
@Test
public void adjacency() {
Chain chain = new Chain(getCoords(4, 4));
Chain other = new Chain(getCoords(3, 4));

assertThat(chain.isAdjacentTo(getCoords(3, 4)), is(true));

assertThat(chain.isAdjacentTo(getCoords(3, 3)), is(false));

assertThat(chain.isAdjacentTo(other), is(true));
}

@Test
Expand All @@ -62,4 +65,28 @@ public void liberties() {

assertThat(liberties.size(), is(2));
}

@Test
public void size() {
Chain chain = new Chain(getCoords(1, 1));

assertThat(chain.size(), is(1));
}

@Test
public void merge() {
Chain main = new Chain(getCoords(4, 4));
Chain other = new Chain(getCoords(3, 4));

main.mergeChain(other);

assertThat(main.size(), is(2));
assertThat(other.size(), is(0));

assertThat(main.contains(getCoords(4, 4)), is(true));
assertThat(main.contains(getCoords(3, 4)), is(true));

assertThat(main.getLiberties().size(), is(6));
assertThat(other.getLiberties().size(), is(0));
}
}

0 comments on commit 351046a

Please sign in to comment.