diff --git a/src/ui/flutter_app/lib/mill/recorder.dart b/src/ui/flutter_app/lib/mill/recorder.dart index f47d121d..57eb7c7b 100644 --- a/src/ui/flutter_app/lib/mill/recorder.dart +++ b/src/ui/flutter_app/lib/mill/recorder.dart @@ -38,7 +38,7 @@ class GameRecorder { _history = newHistory; } - String parseWmdNotation(String wmd) { + String wmdNotationToMoveString(String wmd) { String move = ""; if (wmd.length == 3 && wmd[0] == "x") { @@ -66,7 +66,97 @@ class GameRecorder { return move; } + String playOkNotationToMoveString(String playOk) { + String move = ""; + + if (playOk.length == 0) { + return ""; + } + + var iDash = playOk.indexOf('-'); + var iX = playOk.indexOf('x'); + + if (iDash == -1 && iX == -1) { + // 12 + var val = int.parse(playOk); + if (val >= 1 && val <= 24) { + move = playOkNotationToMove[playOk]!; + return move; + } else { + print("$tag Parse PlayOK notation $playOk failed."); + return ""; + } + } + + if (iX == 0) { + // x12 + var sub = playOk.substring(1); + var val = int.parse(sub); + if (val >= 1 && val <= 24) { + move = "-" + playOkNotationToMove[sub]!; + return move; + } else { + print("$tag Parse PlayOK notation $playOk failed."); + return ""; + } + } + if (iDash != -1 && iX == -1) { + // 12-13 + var sub1 = playOk.substring(0, iDash); + var val1 = int.parse(sub1); + if (val1 >= 1 && val1 <= 24) { + move = playOkNotationToMove[sub1]!; + } else { + print("$tag Parse PlayOK notation $playOk failed."); + return ""; + } + + var sub2 = playOk.substring(iDash + 1); + var val2 = int.parse(sub2); + if (val2 >= 1 && val2 <= 24) { + move = move + "->" + playOkNotationToMove[sub2]!; + return move; + } else { + print("$tag Parse PlayOK notation $playOk failed."); + return ""; + } + } + + print("$tag Not support parsing format oo-ooxo PlayOK notation."); + return ""; + } + + bool isPlayOkMoveList(String text) { + if (text.length >= 4 && + text.substring(0, 3) == "1. " && + int.tryParse(text.substring(3, 4)) != null) { + return true; + } + + if (text[0] == '[') { + return true; + } + + return false; + } + + bool isPlayOkNotation(String text) { + if (int.tryParse(text.substring(3, 4)) != null) { + return true; + } + + return false; + } + + String playOkToWmdMoveList(String playOk) { + return ""; + } + String import(String moveList) { + if (isPlayOkMoveList(moveList)) { + return importPlayOk(moveList); + } + List newHistory = []; List list = moveList .toLowerCase() @@ -104,14 +194,14 @@ class GameRecorder { if (i.length > 0 && !i.endsWith(".")) { if (i.length == 5 && i[2] == 'x') { // "a1xc3" - String m1 = parseWmdNotation(i.substring(0, 2)); + String m1 = wmdNotationToMoveString(i.substring(0, 2)); if (m1 != "") { newHistory.add(Move(m1)); } else { print("Cannot import $i"); return i; } - String m2 = parseWmdNotation(i.substring(2)); + String m2 = wmdNotationToMoveString(i.substring(2)); if (m2 != "") { newHistory.add(Move(m2)); } else { @@ -120,14 +210,14 @@ class GameRecorder { } } else if (i.length == 8 && i[2] == '-' && i[5] == 'x') { // "a1-b2xc3" - String m1 = parseWmdNotation(i.substring(0, 5)); + String m1 = wmdNotationToMoveString(i.substring(0, 5)); if (m1 != "") { newHistory.add(Move(m1)); } else { print("Cannot import $i"); return i; } - String m2 = parseWmdNotation(i.substring(5)); + String m2 = wmdNotationToMoveString(i.substring(5)); if (m2 != "") { newHistory.add(Move(m2)); } else { @@ -136,7 +226,7 @@ class GameRecorder { } } else { // no x - String m = parseWmdNotation(i); + String m = wmdNotationToMoveString(i); if (m != "") { newHistory.add(Move(m)); } else { @@ -154,6 +244,59 @@ class GameRecorder { return ""; } + String importPlayOk(String moveList) { + List newHistory = []; + + List list = moveList + .replaceAll('\n', ' ') + .replaceAll(' 1/2-1/2', '') + .replaceAll(' 1-0', '') + .replaceAll(' 0-1', '') + .replaceAll('TXT', '') + .split(' '); + + for (var i in list) { + i = i.trim(); + + if (i.length > 0 && + !i.endsWith(".") && + !i.startsWith("[") && + !i.endsWith("]")) { + var iX = i.indexOf('x'); + if (iX == -1) { + String m = playOkNotationToMoveString(i); + if (m != "") { + newHistory.add(Move(m)); + } else { + print("Cannot import $i"); + return i; + } + } else if (iX != -1) { + String m1 = playOkNotationToMoveString(i.substring(0, iX)); + if (m1 != "") { + newHistory.add(Move(m1)); + } else { + print("Cannot import $i"); + return i; + } + String m2 = playOkNotationToMoveString(i.substring(iX)); + if (m2 != "") { + newHistory.add(Move(m2)); + } else { + print("Cannot import $i"); + return i; + } + } + } + } + + if (newHistory.length > 0) { + setHistory(newHistory); + } + + return ""; + } + void jumpToHead() { cur = 0; } diff --git a/src/ui/flutter_app/lib/mill/types.dart b/src/ui/flutter_app/lib/mill/types.dart index a0bc4022..b2c0ab85 100644 --- a/src/ui/flutter_app/lib/mill/types.dart +++ b/src/ui/flutter_app/lib/mill/types.dart @@ -397,4 +397,64 @@ Map wmdNotationToMove = { Map moveToWmdNotation = wmdNotationToMove.map((k, v) => MapEntry(v, k)); +Map wmdToPlayOkNotation = { + "a7": "1", + "d7": "2", + "g7": "3", + "b6": "4", + "d6": "5", + "f6": "6", + "c5": "7", + "d5": "8", + "e5": "9", + "a4": "10", + "b4": "11", + "c4": "12", + "e4": "13", + "f4": "14", + "g4": "15", + "c3": "16", + "d3": "17", + "e3": "18", + "b2": "19", + "d2": "20", + "f2": "21", + "a1": "22", + "d1": "23", + "g1": "24", +}; + +Map playOkToWmdNotation = + wmdToPlayOkNotation.map((k, v) => MapEntry(v, k)); + +Map playOkNotationToMove = { + "8": "(1,1)", + "9": "(1,2)", + "13": "(1,3)", + "18": "(1,4)", + "17": "(1,5)", + "16": "(1,6)", + "12": "(1,7)", + "7": "(1,8)", + "5": "(2,1)", + "6": "(2,2)", + "14": "(2,3)", + "21": "(2,4)", + "20": "(2,5)", + "19": "(2,6)", + "11": "(2,7)", + "4": "(2,8)", + "2": "(3,1)", + "3": "(3,2)", + "15": "(3,3)", + "24": "(3,4)", + "23": "(3,5)", + "22": "(3,6)", + "10": "(3,7)", + "1": "(3,8)", +}; + +Map moveToplayOkNotation = + playOkNotationToMove.map((k, v) => MapEntry(v, k)); + enum GameResult { pending, win, lose, draw, none }