Skip to content

Commit

Permalink
Implement MoveData summary parser
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierBlanvillain committed Sep 17, 2018
1 parent 5b0e755 commit 191c54b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
17 changes: 16 additions & 1 deletion src/main/java/featurecat/lizzie/analysis/MoveData.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Holds the data from Leelaz's pondering mode
Expand Down Expand Up @@ -58,6 +60,19 @@ public static MoveData fromInfo(String line) throws ArrayIndexOutOfBoundsExcepti
* @param line line of summary output
*/
public static MoveData fromSummary(String summary) {
return new MoveData(); // TODO
Matcher match = summaryPattern.matcher(summary.trim());
if (!match.matches()) {
throw new IllegalArgumentException("Unexpected summary format: " + summary);
} else {
MoveData result = new MoveData();
result.coordinate = match.group(1);
result.playouts = Integer.parseInt(match.group(2));
result.winrate = Double.parseDouble(match.group(3));
result.variation = Arrays.asList(match.group(4).split(" "));
return result;
}
}

private static Pattern summaryPattern = Pattern.compile(
"^ *(\\w\\d*) -> *(\\d+) \\(V: ([^%)]+)%\\) \\([^\\)]+\\) PV: (.+).*$");
}
37 changes: 33 additions & 4 deletions src/test/java/featurecat/lizzie/analysis/MoveDataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,40 @@ public class MoveDataTest {
assertEquals(moveData.coordinate, "R5");
assertEquals(moveData.playouts, 38);
assertEquals(moveData.winrate, 54.04, 0.01);
assertEquals(moveData.order, 0);
assertEquals(moveData.variation, Arrays.asList(
"R5", "Q5", "R6", "S4", "Q10", "C3", "D3", "C4", "C6", "C5", "D5"));
}

private void testSummary(String summary, String coordinate, int playouts, double winrate, List<String> variation) {
MoveData moveData = MoveData.fromSummary(summary);
assertEquals(moveData.coordinate, coordinate);
assertEquals(moveData.playouts, playouts);
assertEquals(moveData.winrate, winrate, 0.01);
assertEquals(moveData.variation, variation);
}

@Test public void summaryLine1() {
testSummary(" P16 -> 4 (V: 50.94%) (N: 5.79%) PV: P16 N18 R5 Q5",
"P16", 4, 50.94, Arrays.asList("P16", "N18", "R5", "Q5"));
}

List<String> expected = Arrays.asList(
"R5", "Q5", "R6", "S4", "Q10", "C3", "D3", "C4", "C6", "C5", "D5");
@Test public void summaryLine2() {
testSummary(" D9 -> 59 (V: 60.61%) (N: 52.59%) PV: D9 D12 E9 C13 C15 F17",
"D9", 59, 60.61, Arrays.asList("D9", "D12", "E9", "C13", "C15", "F17"));
}

@Test public void summaryLine3() {
testSummary(" B2 -> 1 (V: 46.52%) (N: 86.74%) PV: B2",
"B2", 1, 46.52, Arrays.asList("B2"));
}

@Test public void summaryLine4() {
testSummary(" D16 -> 33 (V: 53.63%) (N: 27.64%) PV: D16 D4 Q16 O4 C3 C4",
"D16", 33, 53.63, Arrays.asList("D16", "D4", "Q16", "O4", "C3", "C4"));
}

assertEquals(moveData.variation, expected);
@Test public void summaryLine5() {
testSummary(" Q16 -> 0 (V: 0.00%) (N: 0.52%) PV: Q16\n",
"Q16", 0, 0.0, Arrays.asList("Q16"));
}
}

0 comments on commit 191c54b

Please sign in to comment.