From 58d21f9202fa6821677c968d0b02c1137f0f2fb9 Mon Sep 17 00:00:00 2001 From: Jeffrey Sander Date: Mon, 5 Mar 2018 17:02:58 -0500 Subject: [PATCH 1/6] go-counting: add new exercise --- config.json | 28 +++ .../.meta/src/reference/java/GoCounting.java | 157 ++++++++++++++++ exercises/go-counting/.meta/version | 2 + exercises/go-counting/README.md | 50 +++++ exercises/go-counting/build.gradle | 18 ++ .../src/test/java/GoCountingTest.java | 175 ++++++++++++++++++ exercises/settings.gradle | 1 + 7 files changed, 431 insertions(+) create mode 100644 exercises/go-counting/.meta/src/reference/java/GoCounting.java create mode 100644 exercises/go-counting/.meta/version create mode 100644 exercises/go-counting/README.md create mode 100644 exercises/go-counting/build.gradle create mode 100644 exercises/go-counting/src/test/java/GoCountingTest.java diff --git a/config.json b/config.json index 40ab860fd..3d5a26824 100644 --- a/config.json +++ b/config.json @@ -653,6 +653,34 @@ }, { "core": false, +<<<<<<< HEAD +======= + "difficulty": 5, + "slug": "two-bucket", + "topics": [ + "algorithms", + "loops", + "conditionals", + "mathematics" + ], + "unlocked_by": "triangle", + "uuid": "210bf628-b385-443b-8329-3483cc6e8d7e" + }, + { + "core": false, + "difficulty": 5, + "slug": "go-counting", + "topics": [ + "algorithms", + "loops", + "conditionals" + ], + "unlocked_by": "triangle", + "uuid": "2e760ae2-fadd-4d31-9639-c4554e2826e9" + }, + { + "core": false, +>>>>>>> 18e8f90... Go-Counting: Add new exercise "difficulty": 6, "slug": "alphametics", "topics": [ diff --git a/exercises/go-counting/.meta/src/reference/java/GoCounting.java b/exercises/go-counting/.meta/src/reference/java/GoCounting.java new file mode 100644 index 000000000..d9b5bdfe3 --- /dev/null +++ b/exercises/go-counting/.meta/src/reference/java/GoCounting.java @@ -0,0 +1,157 @@ +import java.awt.Point; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Set; + + +public class GoCounting { + int NONE = 0; + int WHITE = 1; + int BLACK = 2; + + int[][] board; + + GoCounting (String boardString) { + String[] lines = boardString.split("\n"); + + board = new int[lines[0].length()][lines.length]; + + for(int i = 0; i < lines.length; i++) { + for(int j = 0; j < lines[i].length(); j++) { + if(lines[i].charAt(j) == 'B') { + board[j][i] = BLACK; + } else if(lines[i].charAt(j) == 'W') { + board[j][i] = WHITE; + } else { + board[j][i] = NONE; + } + } + } + } + + private ArrayList getAdjacent(Point p) { + ArrayList adjacent = new ArrayList<>(); + if(p.x > 0) { + adjacent.add(new Point(p.x - 1, p.y)); + } + if(p.x < board.length - 1) { + adjacent.add(new Point(p.x + 1, p.y)); + } + if(p.y > 0) { + adjacent.add(new Point(p.x, p.y - 1)); + } + if(p.y < board[0].length - 1) { + adjacent.add(new Point(p.x, p.y + 1)); + } + return adjacent; + } + + int getTerritoryOwner(int x, int y) { + + if(x < 0 || x >= board.length || y < 0 || y >= board[0].length) { + throw new IllegalArgumentException("Invalid coordinate"); + } + + if(board[x][y] == BLACK || board[x][y] == WHITE) { + return NONE; + } + + ArrayList visited = new ArrayList<>(); + ArrayList edges = new ArrayList<>(); + ArrayList territory = new ArrayList<>(); + + ArrayList tovisit = new ArrayList<>(); + + tovisit.add(new Point(x, y)); + + while(tovisit.size() > 0) { + Point current = tovisit.remove(0); + + if(board[current.x][current.y] == NONE) { + visited.add(current); + territory.add(current); + + ArrayList adjacent = getAdjacent(current); + + for(Point p : adjacent) { + if(!visited.contains(p) && !tovisit.contains(p)) { + tovisit.add(p); + } + } + } else { + edges.add(current); + } + } + + if(edges.size() == 0) { + return NONE; + } + + int owner = board[edges.get(0).x][edges.get(0).y]; + for(int i = 0; i < edges.size(); i++) { + if(owner != board[edges.get(i).x][edges.get(i).y]) { + owner = NONE; + } + } + + return owner; + } + + Set getTerritory(int x, int y) { + + if(x < 0 || x >= board.length || y < 0 || y >= board[0].length) { + throw new IllegalArgumentException("Invalid coordinate"); + } + + ArrayList visited = new ArrayList<>(); + HashSet territory = new HashSet<>(); + + ArrayList tovisit = new ArrayList<>(); + + tovisit.add(new Point(x, y)); + + while(tovisit.size() > 0) { + Point current = tovisit.remove(0); + + if(board[current.x][current.y] == NONE) { + visited.add(current); + territory.add(current); + + ArrayList adjacent = getAdjacent(current); + + for(Point p : adjacent) { + if(!visited.contains(p) && !tovisit.contains(p)) { + tovisit.add(p); + } + } + } + } + + return territory; + } + + HashMap> getTerritories() { + HashMap> territories = new HashMap>(); + + territories.put("WHITE", new HashSet()); + territories.put("BLACK", new HashSet()); + territories.put("NONE", new HashSet()); + + for(int i = 0; i < board[0].length; i++) { + for(int j = 0; j < board.length; j++) { + if(board[j][i] == NONE) { + if(getTerritoryOwner(j, i) == NONE) { + territories.get("NONE").add(new Point(j, i)); + } else if(getTerritoryOwner(j, i) == BLACK) { + territories.get("BLACK").add(new Point(j, i)); + } else { + territories.get("WHITE").add(new Point(j, i)); + } + } + } + } + + return territories; + } +} diff --git a/exercises/go-counting/.meta/version b/exercises/go-counting/.meta/version new file mode 100644 index 000000000..5bc4571bb --- /dev/null +++ b/exercises/go-counting/.meta/version @@ -0,0 +1,2 @@ +1.0.0 + diff --git a/exercises/go-counting/README.md b/exercises/go-counting/README.md new file mode 100644 index 000000000..75f49739f --- /dev/null +++ b/exercises/go-counting/README.md @@ -0,0 +1,50 @@ +# Go Counting + +Count the scored points on a Go board. + +In the game of go (also known as baduk, igo, cờ vây and wéiqí) points +are gained by completely encircling empty intersections with your +stones. The encircled intersections of a player are known as its +territory. + +Write a function that determines the territory of each player. You may +assume that any stones that have been stranded in enemy territory have +already been taken off the board. + +Write a function that determines the territory which includes a specified coordinate. + +Multiple empty intersections may be encircled at once and for encircling +only horizontal and vertical neighbours count. In the following diagram +the stones which matter are marked "O" and the stones that don't are +marked "I" (ignored). Empty spaces represent empty intersections. + +```text ++----+ +|IOOI| +|O O| +|O OI| +|IOI | ++----+ +``` + +To be more precise an empty intersection is part of a player's territory +if all of its neighbours are either stones of that player or empty +intersections that are part of that player's territory. + +For more information see +[wikipedia](https://en.wikipedia.org/wiki/Go_%28game%29) or [Sensei's +Library](http://senseis.xmp.net/). + +# Running the tests + +You can run all the tests for an exercise by entering + +```sh +$ gradle test +``` + +in your terminal. + +## Submitting Incomplete Solutions + +It's possible to submit an incomplete solution so you can see how others have completed the exercise. diff --git a/exercises/go-counting/build.gradle b/exercises/go-counting/build.gradle new file mode 100644 index 000000000..9e1448e14 --- /dev/null +++ b/exercises/go-counting/build.gradle @@ -0,0 +1,18 @@ +apply plugin: "java" +apply plugin: "eclipse" +apply plugin: "idea" + +repositories { + mavenCentral() +} + +dependencies { + testCompile "junit:junit:4.12" +} + +test { + testLogging { + exceptionFormat = 'full' + events = ["passed", "failed", "skipped"] + } +} diff --git a/exercises/go-counting/src/test/java/GoCountingTest.java b/exercises/go-counting/src/test/java/GoCountingTest.java new file mode 100644 index 000000000..9461f8a51 --- /dev/null +++ b/exercises/go-counting/src/test/java/GoCountingTest.java @@ -0,0 +1,175 @@ +import org.junit.Ignore; +import org.junit.Test; +import org.junit.experimental.runners.Enclosed; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import java.awt.Point; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; +import java.util.Set; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; + +import org.junit.Rule; +import org.junit.rules.ExpectedException; + +import java.util.HashSet; + +public class GoCountingTest { + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + String board5x5 = " B \n" + + " B B \n" + + "B W B\n" + + " W W \n" + + " W "; + + @Test + public void blackCorner5x5BoardTest() { + GoCounting gocounting = new GoCounting(board5x5); + + Set territory = new HashSet<>(); + territory.add(new Point(0, 0)); + territory.add(new Point(0, 1)); + territory.add(new Point(1, 0)); + + assertEquals(gocounting.BLACK, gocounting.getTerritoryOwner(0, 1)); + assertEquals(territory, gocounting.getTerritory(0, 1)); + } + + @Test + public void whiteCenter5x5BoardTest() { + GoCounting gocounting = new GoCounting(board5x5); + + Set territory = new HashSet<>(); + territory.add(new Point(2, 3)); + + assertEquals(gocounting.WHITE, gocounting.getTerritoryOwner(2, 3)); + assertEquals(territory, gocounting.getTerritory(2, 3)); + } + + @Test + public void openCorner5x5BoardTest() { + GoCounting gocounting = new GoCounting(board5x5); + + Set territory = new HashSet<>(); + territory.add(new Point(0, 3)); + territory.add(new Point(0, 4)); + territory.add(new Point(1, 4)); + + assertEquals(gocounting.NONE, gocounting.getTerritoryOwner(1, 4)); + assertEquals(territory, gocounting.getTerritory(1, 4)); + } + + @Test + public void stoneNotTerritory5x5Board() { + GoCounting gocounting = new GoCounting(board5x5); + + Set territory = new HashSet<>(); + + assertEquals(gocounting.NONE, gocounting.getTerritoryOwner(1, 1)); + assertEquals(territory, gocounting.getTerritory(1, 1)); + } + + @Test + public void invalidXTooLow5x5Board() { + GoCounting gocounting = new GoCounting(board5x5); + + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("Invalid coordinate"); + + gocounting.getTerritory(-1, 1); + } + + @Test + public void invalidXTooHigh5x5Board() { + GoCounting gocounting = new GoCounting(board5x5); + + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("Invalid coordinate"); + + gocounting.getTerritory(5, 1); + } + + @Test + public void invalidYTooLow5x5Board() { + GoCounting gocounting = new GoCounting(board5x5); + + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("Invalid coordinate"); + + gocounting.getTerritory(1, -1); + } + + @Test + public void invalidYTooHigh5x5Board() { + GoCounting gocounting = new GoCounting(board5x5); + + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("Invalid coordinate"); + + gocounting.getTerritory(1, 5); + } + + @Test + public void oneTerritoryIsWholeBoardTest() { + GoCounting gocounting = new GoCounting(" "); + + HashMap> territories = new HashMap<>(); + Set blackTerritory = new HashSet<>(); + Set whiteTerritory = new HashSet<>(); + Set noneTerritory = new HashSet<>(); + noneTerritory.add(new Point(0, 0)); + + territories.put("BLACK", blackTerritory); + territories.put("WHITE", whiteTerritory); + territories.put("NONE", noneTerritory); + + assertEquals(territories, gocounting.getTerritories()); + } + + @Test + public void twoTerritoryRectangularBoardTest() { + GoCounting gocounting = new GoCounting(" BW \n BW "); + + Set blackTerritory = new HashSet<>(); + blackTerritory.add(new Point(0, 0)); + blackTerritory.add(new Point(0, 1)); + + Set whiteTerritory = new HashSet<>(); + whiteTerritory.add(new Point(3, 0)); + whiteTerritory.add(new Point(3, 1)); + + Set noneTerritory = new HashSet<>(); + + HashMap> territories = new HashMap<>(); + territories.put("BLACK", blackTerritory); + territories.put("WHITE", whiteTerritory); + territories.put("NONE", noneTerritory); + + assertEquals(territories, gocounting.getTerritories()); + } + + @Test + public void twoRegionRectangularBoardTest() { + GoCounting gocounting = new GoCounting(" B "); + + HashMap> territories = new HashMap<>(); + Set blackTerritory = new HashSet<>(); + blackTerritory.add(new Point(0, 0)); + blackTerritory.add(new Point(2, 0)); + Set whiteTerritory = new HashSet<>(); + Set noneTerritory = new HashSet<>(); + + territories.put("BLACK", blackTerritory); + territories.put("WHITE", whiteTerritory); + territories.put("NONE", noneTerritory); + + assertEquals(territories, gocounting.getTerritories()); + } +} diff --git a/exercises/settings.gradle b/exercises/settings.gradle index 6e8dc1a66..695a17e0c 100644 --- a/exercises/settings.gradle +++ b/exercises/settings.gradle @@ -31,6 +31,7 @@ include 'flatten-array' include 'food-chain' include 'forth' include 'gigasecond' +include 'go-counting' include 'grade-school' include 'hamming' include 'hexadecimal' From e0d3b03b8ccbe4ae730e674e4c2964f6bf393597 Mon Sep 17 00:00:00 2001 From: Jeffrey Sander Date: Tue, 6 Mar 2018 09:53:50 -0500 Subject: [PATCH 2/6] go-counting: add new exercise --- config.json | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/config.json b/config.json index 3d5a26824..b72a27d3a 100644 --- a/config.json +++ b/config.json @@ -653,21 +653,6 @@ }, { "core": false, -<<<<<<< HEAD -======= - "difficulty": 5, - "slug": "two-bucket", - "topics": [ - "algorithms", - "loops", - "conditionals", - "mathematics" - ], - "unlocked_by": "triangle", - "uuid": "210bf628-b385-443b-8329-3483cc6e8d7e" - }, - { - "core": false, "difficulty": 5, "slug": "go-counting", "topics": [ @@ -680,7 +665,6 @@ }, { "core": false, ->>>>>>> 18e8f90... Go-Counting: Add new exercise "difficulty": 6, "slug": "alphametics", "topics": [ From 1c9407ff7848b6f293a5ed051c5c5477f8466be5 Mon Sep 17 00:00:00 2001 From: Jeffrey Sander Date: Tue, 6 Mar 2018 15:15:09 -0500 Subject: [PATCH 3/6] go-counting: add new exercise --- config.json | 5 +++-- .../.meta/src/reference/java/GoCounting.java | 3 +-- .../src/test/java/GoCountingTest.java | 19 +++++++++++-------- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/config.json b/config.json index b72a27d3a..08b3200b6 100644 --- a/config.json +++ b/config.json @@ -658,9 +658,10 @@ "topics": [ "algorithms", "loops", - "conditionals" + "conditionals", + "games" ], - "unlocked_by": "triangle", + "unlocked_by": "scrabble-score", "uuid": "2e760ae2-fadd-4d31-9639-c4554e2826e9" }, { diff --git a/exercises/go-counting/.meta/src/reference/java/GoCounting.java b/exercises/go-counting/.meta/src/reference/java/GoCounting.java index d9b5bdfe3..3cda09763 100644 --- a/exercises/go-counting/.meta/src/reference/java/GoCounting.java +++ b/exercises/go-counting/.meta/src/reference/java/GoCounting.java @@ -4,8 +4,7 @@ import java.util.HashSet; import java.util.Set; - -public class GoCounting { +class GoCounting { int NONE = 0; int WHITE = 1; int BLACK = 2; diff --git a/exercises/go-counting/src/test/java/GoCountingTest.java b/exercises/go-counting/src/test/java/GoCountingTest.java index 9461f8a51..35270329e 100644 --- a/exercises/go-counting/src/test/java/GoCountingTest.java +++ b/exercises/go-counting/src/test/java/GoCountingTest.java @@ -1,23 +1,16 @@ import org.junit.Ignore; import org.junit.Test; -import org.junit.experimental.runners.Enclosed; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; import java.awt.Point; -import java.util.Arrays; -import java.util.Collection; import java.util.HashMap; +import java.util.HashSet; import java.util.Set; -import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import org.junit.Rule; import org.junit.rules.ExpectedException; -import java.util.HashSet; - public class GoCountingTest { @Rule @@ -42,6 +35,7 @@ public void blackCorner5x5BoardTest() { assertEquals(territory, gocounting.getTerritory(0, 1)); } + @Ignore("Remove to run test") @Test public void whiteCenter5x5BoardTest() { GoCounting gocounting = new GoCounting(board5x5); @@ -53,6 +47,7 @@ public void whiteCenter5x5BoardTest() { assertEquals(territory, gocounting.getTerritory(2, 3)); } + @Ignore("Remove to run test") @Test public void openCorner5x5BoardTest() { GoCounting gocounting = new GoCounting(board5x5); @@ -66,6 +61,7 @@ public void openCorner5x5BoardTest() { assertEquals(territory, gocounting.getTerritory(1, 4)); } + @Ignore("Remove to run test") @Test public void stoneNotTerritory5x5Board() { GoCounting gocounting = new GoCounting(board5x5); @@ -76,6 +72,7 @@ public void stoneNotTerritory5x5Board() { assertEquals(territory, gocounting.getTerritory(1, 1)); } + @Ignore("Remove to run test") @Test public void invalidXTooLow5x5Board() { GoCounting gocounting = new GoCounting(board5x5); @@ -86,6 +83,7 @@ public void invalidXTooLow5x5Board() { gocounting.getTerritory(-1, 1); } + @Ignore("Remove to run test") @Test public void invalidXTooHigh5x5Board() { GoCounting gocounting = new GoCounting(board5x5); @@ -96,6 +94,7 @@ public void invalidXTooHigh5x5Board() { gocounting.getTerritory(5, 1); } + @Ignore("Remove to run test") @Test public void invalidYTooLow5x5Board() { GoCounting gocounting = new GoCounting(board5x5); @@ -106,6 +105,7 @@ public void invalidYTooLow5x5Board() { gocounting.getTerritory(1, -1); } + @Ignore("Remove to run test") @Test public void invalidYTooHigh5x5Board() { GoCounting gocounting = new GoCounting(board5x5); @@ -116,6 +116,7 @@ public void invalidYTooHigh5x5Board() { gocounting.getTerritory(1, 5); } + @Ignore("Remove to run test") @Test public void oneTerritoryIsWholeBoardTest() { GoCounting gocounting = new GoCounting(" "); @@ -133,6 +134,7 @@ public void oneTerritoryIsWholeBoardTest() { assertEquals(territories, gocounting.getTerritories()); } + @Ignore("Remove to run test") @Test public void twoTerritoryRectangularBoardTest() { GoCounting gocounting = new GoCounting(" BW \n BW "); @@ -155,6 +157,7 @@ public void twoTerritoryRectangularBoardTest() { assertEquals(territories, gocounting.getTerritories()); } + @Ignore("Remove to run test") @Test public void twoRegionRectangularBoardTest() { GoCounting gocounting = new GoCounting(" B "); From 138627cba8ada58ffe9310b29c07914717883f73 Mon Sep 17 00:00:00 2001 From: Jeffrey Sander Date: Wed, 7 Mar 2018 15:49:23 -0500 Subject: [PATCH 4/6] go-counting: add new exercise --- exercises/go-counting/src/main/java/.keep | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 exercises/go-counting/src/main/java/.keep diff --git a/exercises/go-counting/src/main/java/.keep b/exercises/go-counting/src/main/java/.keep new file mode 100644 index 000000000..e69de29bb From a08f87cdba6f5ac34ab41f456a7f881ac1e03cd6 Mon Sep 17 00:00:00 2001 From: Jeffrey Sander Date: Fri, 9 Mar 2018 14:29:15 -0500 Subject: [PATCH 5/6] go-counting: add exercise --- config.json | 26 +++--- .../.meta/src/reference/java/GoCounting.java | 80 +++++++++---------- .../.meta/src/reference/java/Player.java | 3 + .../go-counting/src/main/java/Player.java | 3 + .../src/test/java/GoCountingTest.java | 8 +- 5 files changed, 63 insertions(+), 57 deletions(-) create mode 100644 exercises/go-counting/.meta/src/reference/java/Player.java create mode 100644 exercises/go-counting/src/main/java/Player.java diff --git a/config.json b/config.json index 08b3200b6..1fb98d025 100644 --- a/config.json +++ b/config.json @@ -651,19 +651,6 @@ "unlocked_by": "triangle", "uuid": "210bf628-b385-443b-8329-3483cc6e8d7e" }, - { - "core": false, - "difficulty": 5, - "slug": "go-counting", - "topics": [ - "algorithms", - "loops", - "conditionals", - "games" - ], - "unlocked_by": "scrabble-score", - "uuid": "2e760ae2-fadd-4d31-9639-c4554e2826e9" - }, { "core": false, "difficulty": 6, @@ -1102,6 +1089,19 @@ "unlocked_by": "linked-list", "uuid": "25d2c7a5-1ec8-464a-b3de-ad1b27464ef1" }, + { + "core": false, + "difficulty": 7, + "slug": "go-counting", + "topics": [ + "algorithms", + "loops", + "conditionals", + "games" + ], + "unlocked_by": "scrabble-score", + "uuid": "2e760ae2-fadd-4d31-9639-c4554e2826e9" + }, { "core": false, "difficulty": 8, diff --git a/exercises/go-counting/.meta/src/reference/java/GoCounting.java b/exercises/go-counting/.meta/src/reference/java/GoCounting.java index 3cda09763..743e4112b 100644 --- a/exercises/go-counting/.meta/src/reference/java/GoCounting.java +++ b/exercises/go-counting/.meta/src/reference/java/GoCounting.java @@ -5,25 +5,25 @@ import java.util.Set; class GoCounting { - int NONE = 0; - int WHITE = 1; - int BLACK = 2; + //private int NONE = 0; + //private int WHITE = 1; + //private int BLACK = 2; - int[][] board; + private Player[][] board; GoCounting (String boardString) { String[] lines = boardString.split("\n"); - board = new int[lines[0].length()][lines.length]; + board = new Player[lines[0].length()][lines.length]; - for(int i = 0; i < lines.length; i++) { - for(int j = 0; j < lines[i].length(); j++) { - if(lines[i].charAt(j) == 'B') { - board[j][i] = BLACK; - } else if(lines[i].charAt(j) == 'W') { - board[j][i] = WHITE; + for (int i = 0; i < lines.length; i++) { + for (int j = 0; j < lines[i].length(); j++) { + if (lines[i].charAt(j) == 'B') { + board[j][i] = Player.BLACK; + } else if (lines[i].charAt(j) == 'W') { + board[j][i] = Player.WHITE; } else { - board[j][i] = NONE; + board[j][i] = Player.NONE; } } } @@ -31,29 +31,29 @@ class GoCounting { private ArrayList getAdjacent(Point p) { ArrayList adjacent = new ArrayList<>(); - if(p.x > 0) { + if (p.x > 0) { adjacent.add(new Point(p.x - 1, p.y)); } - if(p.x < board.length - 1) { + if (p.x < board.length - 1) { adjacent.add(new Point(p.x + 1, p.y)); } - if(p.y > 0) { + if (p.y > 0) { adjacent.add(new Point(p.x, p.y - 1)); } - if(p.y < board[0].length - 1) { + if (p.y < board[0].length - 1) { adjacent.add(new Point(p.x, p.y + 1)); } return adjacent; } - int getTerritoryOwner(int x, int y) { + Player getTerritoryOwner(int x, int y) { - if(x < 0 || x >= board.length || y < 0 || y >= board[0].length) { + if (x < 0 || x >= board.length || y < 0 || y >= board[0].length) { throw new IllegalArgumentException("Invalid coordinate"); } - if(board[x][y] == BLACK || board[x][y] == WHITE) { - return NONE; + if (board[x][y] == Player.BLACK || board[x][y] == Player.WHITE) { + return Player.NONE; } ArrayList visited = new ArrayList<>(); @@ -64,17 +64,17 @@ int getTerritoryOwner(int x, int y) { tovisit.add(new Point(x, y)); - while(tovisit.size() > 0) { + while (tovisit.size() > 0) { Point current = tovisit.remove(0); - if(board[current.x][current.y] == NONE) { + if (board[current.x][current.y] == Player.NONE) { visited.add(current); territory.add(current); ArrayList adjacent = getAdjacent(current); - for(Point p : adjacent) { - if(!visited.contains(p) && !tovisit.contains(p)) { + for (Point p : adjacent) { + if (!visited.contains(p) && !tovisit.contains(p)) { tovisit.add(p); } } @@ -83,14 +83,14 @@ int getTerritoryOwner(int x, int y) { } } - if(edges.size() == 0) { - return NONE; + if (edges.size() == 0) { + return Player.NONE; } - int owner = board[edges.get(0).x][edges.get(0).y]; - for(int i = 0; i < edges.size(); i++) { - if(owner != board[edges.get(i).x][edges.get(i).y]) { - owner = NONE; + Player owner = board[edges.get(0).x][edges.get(0).y]; + for (int i = 0; i < edges.size(); i++) { + if (owner != board[edges.get(i).x][edges.get(i).y]) { + owner = Player.NONE; } } @@ -99,7 +99,7 @@ int getTerritoryOwner(int x, int y) { Set getTerritory(int x, int y) { - if(x < 0 || x >= board.length || y < 0 || y >= board[0].length) { + if (x < 0 || x >= board.length || y < 0 || y >= board[0].length) { throw new IllegalArgumentException("Invalid coordinate"); } @@ -110,17 +110,17 @@ Set getTerritory(int x, int y) { tovisit.add(new Point(x, y)); - while(tovisit.size() > 0) { + while (tovisit.size() > 0) { Point current = tovisit.remove(0); - if(board[current.x][current.y] == NONE) { + if (board[current.x][current.y] == Player.NONE) { visited.add(current); territory.add(current); ArrayList adjacent = getAdjacent(current); - for(Point p : adjacent) { - if(!visited.contains(p) && !tovisit.contains(p)) { + for (Point p : adjacent) { + if (!visited.contains(p) && !tovisit.contains(p)) { tovisit.add(p); } } @@ -137,12 +137,12 @@ HashMap> getTerritories() { territories.put("BLACK", new HashSet()); territories.put("NONE", new HashSet()); - for(int i = 0; i < board[0].length; i++) { - for(int j = 0; j < board.length; j++) { - if(board[j][i] == NONE) { - if(getTerritoryOwner(j, i) == NONE) { + for (int i = 0; i < board[0].length; i++) { + for (int j = 0; j < board.length; j++) { + if (board[j][i] == Player.NONE) { + if (getTerritoryOwner(j, i) == Player.NONE) { territories.get("NONE").add(new Point(j, i)); - } else if(getTerritoryOwner(j, i) == BLACK) { + } else if (getTerritoryOwner(j, i) == Player.BLACK) { territories.get("BLACK").add(new Point(j, i)); } else { territories.get("WHITE").add(new Point(j, i)); diff --git a/exercises/go-counting/.meta/src/reference/java/Player.java b/exercises/go-counting/.meta/src/reference/java/Player.java new file mode 100644 index 000000000..7c236a71c --- /dev/null +++ b/exercises/go-counting/.meta/src/reference/java/Player.java @@ -0,0 +1,3 @@ +enum Player { + NONE, BLACK, WHITE +} diff --git a/exercises/go-counting/src/main/java/Player.java b/exercises/go-counting/src/main/java/Player.java new file mode 100644 index 000000000..7c236a71c --- /dev/null +++ b/exercises/go-counting/src/main/java/Player.java @@ -0,0 +1,3 @@ +enum Player { + NONE, BLACK, WHITE +} diff --git a/exercises/go-counting/src/test/java/GoCountingTest.java b/exercises/go-counting/src/test/java/GoCountingTest.java index 35270329e..af3eeea52 100644 --- a/exercises/go-counting/src/test/java/GoCountingTest.java +++ b/exercises/go-counting/src/test/java/GoCountingTest.java @@ -31,7 +31,7 @@ public void blackCorner5x5BoardTest() { territory.add(new Point(0, 1)); territory.add(new Point(1, 0)); - assertEquals(gocounting.BLACK, gocounting.getTerritoryOwner(0, 1)); + assertEquals(Player.BLACK, gocounting.getTerritoryOwner(0, 1)); assertEquals(territory, gocounting.getTerritory(0, 1)); } @@ -43,7 +43,7 @@ public void whiteCenter5x5BoardTest() { Set territory = new HashSet<>(); territory.add(new Point(2, 3)); - assertEquals(gocounting.WHITE, gocounting.getTerritoryOwner(2, 3)); + assertEquals(Player.WHITE, gocounting.getTerritoryOwner(2, 3)); assertEquals(territory, gocounting.getTerritory(2, 3)); } @@ -57,7 +57,7 @@ public void openCorner5x5BoardTest() { territory.add(new Point(0, 4)); territory.add(new Point(1, 4)); - assertEquals(gocounting.NONE, gocounting.getTerritoryOwner(1, 4)); + assertEquals(Player.NONE, gocounting.getTerritoryOwner(1, 4)); assertEquals(territory, gocounting.getTerritory(1, 4)); } @@ -68,7 +68,7 @@ public void stoneNotTerritory5x5Board() { Set territory = new HashSet<>(); - assertEquals(gocounting.NONE, gocounting.getTerritoryOwner(1, 1)); + assertEquals(Player.NONE, gocounting.getTerritoryOwner(1, 1)); assertEquals(territory, gocounting.getTerritory(1, 1)); } From f60e7298900e367a176cf41075c17d01f355f8f1 Mon Sep 17 00:00:00 2001 From: Jeffrey Sander Date: Mon, 12 Mar 2018 09:27:45 -0400 Subject: [PATCH 6/6] go-counting: add new exercise --- .../go-counting/.meta/src/reference/java/GoCounting.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/exercises/go-counting/.meta/src/reference/java/GoCounting.java b/exercises/go-counting/.meta/src/reference/java/GoCounting.java index 743e4112b..5f6b123f8 100644 --- a/exercises/go-counting/.meta/src/reference/java/GoCounting.java +++ b/exercises/go-counting/.meta/src/reference/java/GoCounting.java @@ -5,10 +5,6 @@ import java.util.Set; class GoCounting { - //private int NONE = 0; - //private int WHITE = 1; - //private int BLACK = 2; - private Player[][] board; GoCounting (String boardString) {