瀏覽代碼

go-counting: use provided enumeration instead of Strings (#1461)

* go-counting: use provided enumeration instead of Strings

Use of enumeration should be consistent across the test suite.

* update example solution to match new test expectations

* fix return type
Corey McCandless 6 年之前
父節點
當前提交
6eb2d5a0dc

+ 6
- 12
exercises/go-counting/.meta/src/reference/java/GoCounting.java 查看文件

@@ -126,23 +126,17 @@ class GoCounting {
126 126
 		return territory;
127 127
 	}
128 128
 	
129
-	HashMap<String, Set<Point>> getTerritories() {
130
-		HashMap<String, Set<Point>> territories = new HashMap<String, Set<Point>>();
129
+	HashMap<Player, Set<Point>> getTerritories() {
130
+		HashMap<Player, Set<Point>> territories = new HashMap<Player, Set<Point>>();
131 131
 		
132
-		territories.put("WHITE", new HashSet<Point>());
133
-		territories.put("BLACK", new HashSet<Point>());
134
-		territories.put("NONE", new HashSet<Point>());
132
+		territories.put(Player.WHITE, new HashSet<Point>());
133
+		territories.put(Player.BLACK, new HashSet<Point>());
134
+		territories.put(Player.NONE, new HashSet<Point>());
135 135
 		
136 136
 		for (int i = 0; i < board[0].length; i++) {
137 137
 			for (int j = 0; j < board.length; j++) {
138 138
 				if (board[j][i] == Player.NONE) {
139
-					if (getTerritoryOwner(j, i) == Player.NONE) {
140
-						territories.get("NONE").add(new Point(j, i));
141
-					} else if (getTerritoryOwner(j, i) == Player.BLACK) {
142
-						territories.get("BLACK").add(new Point(j, i));
143
-					} else {
144
-						territories.get("WHITE").add(new Point(j, i));
145
-					}
139
+					territories.get(getTerritoryOwner(j, i)).add(new Point(j, i));
146 140
 				}
147 141
 			}
148 142
 		}

+ 12
- 12
exercises/go-counting/src/test/java/GoCountingTest.java 查看文件

@@ -121,15 +121,15 @@ public class GoCountingTest {
121 121
 	public void oneTerritoryIsWholeBoardTest() {
122 122
 		GoCounting gocounting = new GoCounting(" ");
123 123
 		
124
-		HashMap<String, Set<Point>> territories = new HashMap<>();
124
+		HashMap<Player, Set<Point>> territories = new HashMap<>();
125 125
 		Set<Point> blackTerritory = new HashSet<>();
126 126
 		Set<Point> whiteTerritory = new HashSet<>();
127 127
 		Set<Point> noneTerritory = new HashSet<>();
128 128
 		noneTerritory.add(new Point(0, 0));
129 129
 		
130
-		territories.put("BLACK", blackTerritory);
131
-		territories.put("WHITE", whiteTerritory);
132
-		territories.put("NONE", noneTerritory);
130
+		territories.put(Player.BLACK, blackTerritory);
131
+		territories.put(Player.WHITE, whiteTerritory);
132
+		territories.put(Player.NONE, noneTerritory);
133 133
 		
134 134
 		assertEquals(territories, gocounting.getTerritories());
135 135
 	}
@@ -149,10 +149,10 @@ public class GoCountingTest {
149 149
 		
150 150
 		Set<Point> noneTerritory = new HashSet<>();
151 151
 		
152
-		HashMap<String, Set<Point>> territories = new HashMap<>();
153
-		territories.put("BLACK", blackTerritory);
154
-		territories.put("WHITE", whiteTerritory);
155
-		territories.put("NONE", noneTerritory);
152
+		HashMap<Player, Set<Point>> territories = new HashMap<>();
153
+		territories.put(Player.BLACK, blackTerritory);
154
+		territories.put(Player.WHITE, whiteTerritory);
155
+		territories.put(Player.NONE, noneTerritory);
156 156
 		
157 157
 		assertEquals(territories, gocounting.getTerritories());
158 158
 	}
@@ -162,16 +162,16 @@ public class GoCountingTest {
162 162
 	public void twoRegionRectangularBoardTest() {
163 163
 		GoCounting gocounting = new GoCounting(" B ");
164 164
 		
165
-		HashMap<String, Set<Point>> territories = new HashMap<>();
165
+		HashMap<Player, Set<Point>> territories = new HashMap<>();
166 166
 		Set<Point> blackTerritory = new HashSet<>();
167 167
 		blackTerritory.add(new Point(0, 0));
168 168
 		blackTerritory.add(new Point(2, 0));
169 169
 		Set<Point> whiteTerritory = new HashSet<>();
170 170
 		Set<Point> noneTerritory = new HashSet<>();
171 171
 		
172
-		territories.put("BLACK", blackTerritory);
173
-		territories.put("WHITE", whiteTerritory);
174
-		territories.put("NONE", noneTerritory);
172
+		territories.put(Player.BLACK, blackTerritory);
173
+		territories.put(Player.WHITE, whiteTerritory);
174
+		territories.put(Player.NONE, noneTerritory);
175 175
 		
176 176
 		assertEquals(territories, gocounting.getTerritories());
177 177
 	}