public void TestSuicideIsIllegal() { GoBoard lGoBoard = new GoBoard(19); lGoBoard.PlayStone("B1", Color.Black, true); lGoBoard.PlayStone("A2", Color.Black, true); Assert.IsTrue(lGoBoard.IsSuicide(lGoBoard.Coord.At("A1"), Color.White), lGoBoard.ToString()); }
// marked to be depreciated public static int FindAnySafeInOwnTerritory(GoBoard goBoard, Color playerToMove) { // useful for force moves in endgame for (int lPoint = 0; lPoint < goBoard.Coord.BoardArea; lPoint++) { SafetyStatus lSafetyStatus = goBoard.GetSafetyStatus(lPoint); if ((lSafetyStatus.IsTerritory) && (lSafetyStatus.Player == playerToMove)) { if (goBoard.IsLegal(lPoint, playerToMove)) { goBoard.PlayStone(lPoint, playerToMove, true); SafetyStatus lSafetyStatusAfter = goBoard.GetSafetyStatus(lPoint); goBoard.Undo(); // it's still safe after move, so return it if ((lSafetyStatus.IsAlive) && (lSafetyStatus.Player == playerToMove)) { return(lPoint); } } } } return(CoordinateSystem.PASS); }
// marked to be depreciated public static List <int> GetSafeMovesInOwnTerritory(GoBoard goBoard, Color playerToMove) { // useful for force moves in endgame List <int> lMoves = new List <int>(); for (int lPoint = 0; lPoint < goBoard.Coord.BoardArea; lPoint++) { SafetyStatus lSafetyStatus = goBoard.GetSafetyStatus(lPoint); if ((lSafetyStatus.IsTerritory) && (lSafetyStatus.Player == playerToMove)) { if (goBoard.IsLegal(lPoint, playerToMove)) { goBoard.PlayStone(lPoint, playerToMove, true); SafetyStatus lSafetyStatusAfter = goBoard.GetSafetyStatus(lPoint); // check if still safe after move if (lSafetyStatusAfter.IsAlive) // && (lSafetyStatus.Player == playerToMove)) { lMoves.Add(lPoint); } goBoard.Undo(); } } } return(lMoves); }
public void StartPonder(int lMove, Color playerToMove) { Console.Error.WriteLine("Pondering >>>"); Stop(); Board.PlayStone(lMove, playerToMove, true); Start(Board, playerToMove.Opposite, SearchOptions, SearchMethodType, null); }
public static bool Apply(GameRecord gameRecord, GoBoard goBoard, bool undoable, int moves) { int lMoves = (moves <= 0) ? gameRecord.Count : moves; goBoard.SetBoardSize(gameRecord.BoardSize); // will automatically clear for (int i = 0; i < lMoves; i++) if (!goBoard.PlayStone(gameRecord[i].Move, gameRecord[i].Player, undoable)) return false; return true; }
protected static void PlaySequence(GoBoard goBoard, SearchEngine searchEngine, List <string> parameters, string id, Worker.SendResponse proxy) { if (parameters.Count > 0) { string[] lMoves = parameters[0].Split(' '); for (int i = 0; i < lMoves.Length / 2; i++) { goBoard.PlayStone(lMoves[i * 2 + 1], Color.ToColor(lMoves[(i * 2)]), false); } } Respond(proxy, id); }
public static bool Apply(GameRecord gameRecord, GoBoard goBoard, bool undoable, int moves) { int lMoves = (moves <= 0) ? gameRecord.Count : moves; goBoard.SetBoardSize(gameRecord.BoardSize); // will automatically clear for (int i = 0; i < lMoves; i++) { if (!goBoard.PlayStone(gameRecord[i].Move, gameRecord[i].Player, undoable)) { return(false); } } return(true); }
public static int LibertyCountAfterMove(GoBoard goBoard, Color player, int move) { if (!goBoard.GetColor(move).IsEmpty) { return(0); // invalid } if (goBoard.IsSuicide(move, player)) { return(0); } goBoard.PlayStone(move, player, true); int lLiberities = goBoard.GetBlockLibertyCount(move); goBoard.Undo(); return(lLiberities); }
public static List <Result> Filter(GameRecords gameRecords, FilterFunction filterFunction) { List <Result> lResults = new List <Result>(); foreach (GameRecord lGameRecord in gameRecords) { CoordinateSystem lCoord = new CoordinateSystem(lGameRecord.BoardSize); GoBoard lGoBoard = new GoBoard(lGameRecord.BoardSize); for (int i = 0; i < lGameRecord.Count - 1; i++) { lGoBoard.PlayStone(lGameRecord[i].Move, lGameRecord[i].Player, false); int lNextMove; Color lNextPlayer; if (i >= lGameRecord.Count) { lNextMove = CoordinateSystem.PASS; lNextPlayer = Color.Empty; } else { lNextMove = lGameRecord[i + 1].Move; lNextPlayer = lGameRecord[i + 1].Player; } if (filterFunction(lGoBoard, lGameRecord[i].Player, lGameRecord[i].Move, lNextPlayer, lNextMove)) { // { // Console.WriteLine(lGameRecord.GameName + " (" + i.ToString() + ") -> " + lCoord.ToString(lNextMove)); lResults.Add(new Result(lGameRecord, i)); } // } } } return(lResults); }
public static List<Result> Filter(GameRecords gameRecords, FilterFunction filterFunction) { List<Result> lResults = new List<Result>(); foreach (GameRecord lGameRecord in gameRecords) { CoordinateSystem lCoord = new CoordinateSystem(lGameRecord.BoardSize); GoBoard lGoBoard = new GoBoard(lGameRecord.BoardSize); for (int i = 0; i < lGameRecord.Count - 1; i++) { lGoBoard.PlayStone(lGameRecord[i].Move, lGameRecord[i].Player, false); int lNextMove; Color lNextPlayer; if (i >= lGameRecord.Count) { lNextMove = CoordinateSystem.PASS; lNextPlayer = Color.Empty; } else { lNextMove = lGameRecord[i + 1].Move; lNextPlayer = lGameRecord[i + 1].Player; } if (filterFunction(lGoBoard, lGameRecord[i].Player, lGameRecord[i].Move, lNextPlayer, lNextMove)) // { // Console.WriteLine(lGameRecord.GameName + " (" + i.ToString() + ") -> " + lCoord.ToString(lNextMove)); lResults.Add(new Result(lGameRecord, i)); // } } } return lResults; }
// marked to be depreciated public static int FindAnySafeInOwnTerritory(GoBoard goBoard, Color playerToMove) { // useful for force moves in endgame for (int lPoint = 0; lPoint < goBoard.Coord.BoardArea; lPoint++) { SafetyStatus lSafetyStatus = goBoard.GetSafetyStatus(lPoint); if ((lSafetyStatus.IsTerritory) && (lSafetyStatus.Player == playerToMove)) if (goBoard.IsLegal(lPoint, playerToMove)) { goBoard.PlayStone(lPoint, playerToMove, true); SafetyStatus lSafetyStatusAfter = goBoard.GetSafetyStatus(lPoint); goBoard.Undo(); // it's still safe after move, so return it if ((lSafetyStatus.IsAlive) && (lSafetyStatus.Player == playerToMove)) return lPoint; } } return CoordinateSystem.PASS; }
// marked to be depreciated public static List<int> GetSafeMovesInOwnTerritory(GoBoard goBoard, Color playerToMove) { // useful for force moves in endgame List<int> lMoves = new List<int>(); for (int lPoint = 0; lPoint < goBoard.Coord.BoardArea; lPoint++) { SafetyStatus lSafetyStatus = goBoard.GetSafetyStatus(lPoint); if ((lSafetyStatus.IsTerritory) && (lSafetyStatus.Player == playerToMove)) if (goBoard.IsLegal(lPoint, playerToMove)) { goBoard.PlayStone(lPoint, playerToMove, true); SafetyStatus lSafetyStatusAfter = goBoard.GetSafetyStatus(lPoint); // check if still safe after move if (lSafetyStatusAfter.IsAlive) // && (lSafetyStatus.Player == playerToMove)) lMoves.Add(lPoint); goBoard.Undo(); } } return lMoves; }
//[Test] public void Test() { GoBoard lGoBoard = new GoBoard(13); lGoBoard.PlayStone("A2", Color.Black, false); lGoBoard.PlayStone("B1", Color.Black, false); lGoBoard.PlayStone("B2", Color.Black, false); lGoBoard.PlayStone("C2", Color.Black, false); lGoBoard.PlayStone("D2", Color.Black, false); lGoBoard.PlayStone("E2", Color.Black, false); lGoBoard.PlayStone("F2", Color.Black, false); lGoBoard.PlayStone("F1", Color.Black, false); lGoBoard.PlayStone("D1", Color.Black, false); lGoBoard.PlayStone("G8", Color.Black, false); lGoBoard.PlayStone("H7", Color.Black, false); lGoBoard.PlayStone("H9", Color.Black, false); lGoBoard.PlayStone("J8", Color.Black, false); lGoBoard.PlayStone("H2", Color.Black, false); lGoBoard.PlayStone("J1", Color.Black, false); lGoBoard.PlayStone("K2", Color.Black, false); lGoBoard.Dump(); string lString = "Pattern LinkPattern4\n\n??o??\n?XoX?\n..X*.\n-----\n\n:\n\n??o??\n?Xoa?\n.*X*.\n-----\n\n;libertycount(a)>1\n\n"; Pattern lPattern = new Pattern(lString); lPattern.Dump(); PatternTest[] lPatternTests = { // new PatternTest( "1+2*3", true, true, 7, true, -1 ), // bug: right to left (instead of left to right), but okay for now new PatternTest( "", true, true, 1, false, -1 ), new PatternTest( "", true, true, true, true, -1 ), new PatternTest( "1", true, true, 1, true, -1 ), new PatternTest( "1+1", true, true, 2, true, -1 ), new PatternTest( "(1+1)", true, true, 2, true, -1 ), new PatternTest( "1>2", true, true, false, true, -1 ), new PatternTest( "(1>2)", true, true, false, true, -1 ), new PatternTest( "((1)>(1+2))", true, true, false, true, -1 ), new PatternTest( "1+2+3+4+5+6+7+8+9", true, true, 45, true, -1 ), new PatternTest( "1+2+(3+4)+5+6+7+(8+9)", true, true, 45, true, -1 ), new PatternTest( "(1+2+(3+4)+5+6+7+(8+9))", true, true, 45, true, -1 ), new PatternTest( "0&&1&&1", true, true, false, true, -1 ), new PatternTest( "1||1||0", true, true, true, true, -1 ), new PatternTest( "0&&1&&1||1", true, true, false, true, -1 ), new PatternTest( "libertycount(a)", true, true, 4, false, -1 ), new PatternTest( "libertycount(a) > 1", true, true, true, false, -1 ), new PatternTest( "1", true, true, 1, true, -1 ), new PatternTest( "1+1", true, true, 2, true, -1 ), new PatternTest( "((1)>(1+2))", true, true, false, true, -1 ), new PatternTest( " (1+2) > libertycount(a)", true, true, 0, false, -1 ), new PatternTest( " (1+2) > libertycount(a,b)", true, true, 0, false, -1 ), new PatternTest( " (1+2) > libertycount()", true, false, 0, false, -1 ), new PatternTest( " (1+2) != libertycount()+1", true, false, 0, false, -1 ), new PatternTest( " (1+2) >= libertycount()+1+2", true, false, 0, false, -1 ), new PatternTest( " (libertycount(a) == 1)", true, true, 0, false, -1 ), new PatternTest( " libertycount(a) == 1 && libertycount(b) == 1", true, true, 0, false, -1 ), new PatternTest( " (libertycount(a) == 1) || (libertycount(b) == 1)", true, true, 0, false, -1 ), new PatternTest( " ((libertycount(a) == 1) || (libertycount(b) == 1))", true, true, 0, false, -1 ), new PatternTest( "1+", true, false, 0, true, -1 ), new PatternTest( " libertycount(a) == 1)", true, false, 0, false, 100 ), new PatternTest( "libertycount(a) >" , true, false, 0, false, 101 ), new PatternTest( "(((1)>(1+2))", true, false, 0, true, -1 ), new PatternTest( "((1)>(1+2)))", true, false, 0, true, -1 ), new PatternTest( " (1+2 > libertycount(a)", true, false, 0, false, -1 ), new PatternTest( " 1+2) > libertycount(a,b)", true, false, 0, false, -1 ), new PatternTest( " libertycount(a) libertycount(b)", true, false, 0, false, -1 ), new PatternTest( " libertycount(a) == 1 libertycount(b)", true, false, 0, false, -1 ), new PatternTest( " libertycount(a) libertycount(b) == 1", true, false, 0, false, -1 ), new PatternTest(" libertycount(a) == 1 & libertycount(b) == 1", false, false, 0, false, -1 ) }; foreach (PatternTest lPatternTest in lPatternTests) { PatternCode lPatternCode = new PatternCode(); // PatternScanner lPatternScanner = new PatternScanner(lPatternTest.Expression, lPatternCode); bool lScan = !lPatternCode.IsError(); Assert.IsTrue(lScan == lPatternTest.ScanFlag, "1:" + lPatternTest.Expression); if (lScan != lPatternTest.ScanFlag) { Console.Error.WriteLine("PatternInterpretor::SelfTest (Failed)"); Console.Error.WriteLine("Failed Test: " + lPatternTest.Expression); Console.Error.WriteLine("Scan : " + (lScan ? "YES" : "NO") + " Expected: " + (lPatternTest.ScanFlag ? "YES" : "NO")); } if (lScan) { PatternSyntax lPatternSyntax = new PatternSyntax(lPatternCode); bool lSyntax = lPatternSyntax.SyntaxCheck(); Assert.IsTrue(lSyntax == lPatternTest.SyntaxFlag, "2:" + lPatternTest.Expression); if (lSyntax != lPatternTest.SyntaxFlag) { Console.Error.WriteLine("PatternInterpretor::SelfTest (Failed)"); Console.Error.WriteLine("Failed Test: " + lPatternTest.Expression); Console.Error.WriteLine("Syntax: " + (lSyntax ? "YES" : "NO") + " Expected: " + (lPatternTest.SyntaxFlag ? "YES" : "NO")); } if ((lSyntax) && (lPatternTest.ExecuteFlag)) { PatternInterpretor lPatternInterpretor = new PatternInterpretor(lPatternCode, lPattern); int lResult = lPatternInterpretor.Execute(lGoBoard, Color.Black, new Coordinate('K', 1), 0); Assert.IsTrue(lResult == lPatternTest.Result, "3:" + lPatternTest.Expression + " Got: " + lResult.ToString() + " Expected: " + lPatternTest.Result); Assert.IsTrue(!lPatternInterpretor.IsError(), "4:Interpretor Error: " + lPatternTest.Expression); if ((lResult != lPatternTest.Result) || (lPatternInterpretor.IsError())) { Console.Error.WriteLine("PatternInterpretor::SelfTest (Failed)"); Console.Error.WriteLine("Failed Test: " + lPatternTest.Expression); Console.Error.Write("Got : " + lResult.ToString()); Console.Error.WriteLine(" Expected: " + lPatternTest.Result); } } if ((lSyntax) && (!lPatternTest.ExecuteFlag)) { PatternInterpretor lPatternInterpretor = new PatternInterpretor(lPatternCode, lPattern); int lResult = lPatternInterpretor.Execute(lGoBoard, Color.Black, new Coordinate('K', 1), 0); Assert.IsTrue(lResult == lPatternTest.Result, "5:" + lPatternTest.Expression); if ((lResult != lPatternTest.Result) || (lPatternInterpretor.IsError())) { Console.Error.WriteLine("PatternInterpretor::SelfTest (Failed)"); Console.Error.WriteLine("Failed Test: " + lPatternTest.Expression); Console.Error.Write("Got : " + lResult.ToString()); Console.Error.WriteLine(" Expected: " + lPatternTest.Result); } } } } }
public static int LibertyCountAfterMove(GoBoard goBoard, Color player, int move) { if (!goBoard.GetColor(move).IsEmpty) return 0; // invalid if (goBoard.IsSuicide(move, player)) return 0; goBoard.PlayStone(move, player, true); int lLiberities = goBoard.GetBlockLibertyCount(move); goBoard.Undo(); return lLiberities; }
//[Test] public void Test() { GoBoard lGoBoard = new GoBoard(13); lGoBoard.PlayStone("A2", Color.Black, false); lGoBoard.PlayStone("B1", Color.Black, false); lGoBoard.PlayStone("B2", Color.Black, false); lGoBoard.PlayStone("C2", Color.Black, false); lGoBoard.PlayStone("D2", Color.Black, false); lGoBoard.PlayStone("E2", Color.Black, false); lGoBoard.PlayStone("F2", Color.Black, false); lGoBoard.PlayStone("F1", Color.Black, false); lGoBoard.PlayStone("D1", Color.Black, false); lGoBoard.PlayStone("G8", Color.Black, false); lGoBoard.PlayStone("H7", Color.Black, false); lGoBoard.PlayStone("H9", Color.Black, false); lGoBoard.PlayStone("J8", Color.Black, false); lGoBoard.PlayStone("H2", Color.Black, false); lGoBoard.PlayStone("J1", Color.Black, false); lGoBoard.PlayStone("K2", Color.Black, false); lGoBoard.Dump(); string lString = "Pattern LinkPattern4\n\n??o??\n?XoX?\n..X*.\n-----\n\n:\n\n??o??\n?Xoa?\n.*X*.\n-----\n\n;libertycount(a)>1\n\n"; Pattern lPattern = new Pattern(lString); lPattern.Dump(); PatternTest[] lPatternTests = { // new PatternTest( "1+2*3", true, true, 7, true, -1 ), // bug: right to left (instead of left to right), but okay for now new PatternTest("", true, true, 1, false, -1), new PatternTest("", true, true, true, true, -1), new PatternTest("1", true, true, 1, true, -1), new PatternTest("1+1", true, true, 2, true, -1), new PatternTest("(1+1)", true, true, 2, true, -1), new PatternTest("1>2", true, true, false, true, -1), new PatternTest("(1>2)", true, true, false, true, -1), new PatternTest("((1)>(1+2))", true, true, false, true, -1), new PatternTest("1+2+3+4+5+6+7+8+9", true, true, 45, true, -1), new PatternTest("1+2+(3+4)+5+6+7+(8+9)", true, true, 45, true, -1), new PatternTest("(1+2+(3+4)+5+6+7+(8+9))", true, true, 45, true, -1), new PatternTest("0&&1&&1", true, true, false, true, -1), new PatternTest("1||1||0", true, true, true, true, -1), new PatternTest("0&&1&&1||1", true, true, false, true, -1), new PatternTest("libertycount(a)", true, true, 4, false, -1), new PatternTest("libertycount(a) > 1", true, true, true, false, -1), new PatternTest("1", true, true, 1, true, -1), new PatternTest("1+1", true, true, 2, true, -1), new PatternTest("((1)>(1+2))", true, true, false, true, -1), new PatternTest(" (1+2) > libertycount(a)", true, true, 0, false, -1), new PatternTest(" (1+2) > libertycount(a,b)", true, true, 0, false, -1), new PatternTest(" (1+2) > libertycount()", true, false, 0, false, -1), new PatternTest(" (1+2) != libertycount()+1", true, false, 0, false, -1), new PatternTest(" (1+2) >= libertycount()+1+2", true, false, 0, false, -1), new PatternTest(" (libertycount(a) == 1)", true, true, 0, false, -1), new PatternTest(" libertycount(a) == 1 && libertycount(b) == 1", true, true, 0, false, -1), new PatternTest(" (libertycount(a) == 1) || (libertycount(b) == 1)", true, true, 0, false, -1), new PatternTest(" ((libertycount(a) == 1) || (libertycount(b) == 1))", true, true, 0, false, -1), new PatternTest("1+", true, false, 0, true, -1), new PatternTest(" libertycount(a) == 1)", true, false, 0, false, 100), new PatternTest("libertycount(a) >", true, false, 0, false, 101), new PatternTest("(((1)>(1+2))", true, false, 0, true, -1), new PatternTest("((1)>(1+2)))", true, false, 0, true, -1), new PatternTest(" (1+2 > libertycount(a)", true, false, 0, false, -1), new PatternTest(" 1+2) > libertycount(a,b)", true, false, 0, false, -1), new PatternTest(" libertycount(a) libertycount(b)", true, false, 0, false, -1), new PatternTest(" libertycount(a) == 1 libertycount(b)", true, false, 0, false, -1), new PatternTest(" libertycount(a) libertycount(b) == 1", true, false, 0, false, -1), new PatternTest(" libertycount(a) == 1 & libertycount(b) == 1", false, false, 0, false, -1) }; foreach (PatternTest lPatternTest in lPatternTests) { PatternCode lPatternCode = new PatternCode(); // PatternScanner lPatternScanner = new PatternScanner(lPatternTest.Expression, lPatternCode); bool lScan = !lPatternCode.IsError(); Assert.IsTrue(lScan == lPatternTest.ScanFlag, "1:" + lPatternTest.Expression); if (lScan != lPatternTest.ScanFlag) { Console.Error.WriteLine("PatternInterpretor::SelfTest (Failed)"); Console.Error.WriteLine("Failed Test: " + lPatternTest.Expression); Console.Error.WriteLine("Scan : " + (lScan ? "YES" : "NO") + " Expected: " + (lPatternTest.ScanFlag ? "YES" : "NO")); } if (lScan) { PatternSyntax lPatternSyntax = new PatternSyntax(lPatternCode); bool lSyntax = lPatternSyntax.SyntaxCheck(); Assert.IsTrue(lSyntax == lPatternTest.SyntaxFlag, "2:" + lPatternTest.Expression); if (lSyntax != lPatternTest.SyntaxFlag) { Console.Error.WriteLine("PatternInterpretor::SelfTest (Failed)"); Console.Error.WriteLine("Failed Test: " + lPatternTest.Expression); Console.Error.WriteLine("Syntax: " + (lSyntax ? "YES" : "NO") + " Expected: " + (lPatternTest.SyntaxFlag ? "YES" : "NO")); } if ((lSyntax) && (lPatternTest.ExecuteFlag)) { PatternInterpretor lPatternInterpretor = new PatternInterpretor(lPatternCode, lPattern); int lResult = lPatternInterpretor.Execute(lGoBoard, Color.Black, new Coordinate('K', 1), 0); Assert.IsTrue(lResult == lPatternTest.Result, "3:" + lPatternTest.Expression + " Got: " + lResult.ToString() + " Expected: " + lPatternTest.Result); Assert.IsTrue(!lPatternInterpretor.IsError(), "4:Interpretor Error: " + lPatternTest.Expression); if ((lResult != lPatternTest.Result) || (lPatternInterpretor.IsError())) { Console.Error.WriteLine("PatternInterpretor::SelfTest (Failed)"); Console.Error.WriteLine("Failed Test: " + lPatternTest.Expression); Console.Error.Write("Got : " + lResult.ToString()); Console.Error.WriteLine(" Expected: " + lPatternTest.Result); } } if ((lSyntax) && (!lPatternTest.ExecuteFlag)) { PatternInterpretor lPatternInterpretor = new PatternInterpretor(lPatternCode, lPattern); int lResult = lPatternInterpretor.Execute(lGoBoard, Color.Black, new Coordinate('K', 1), 0); Assert.IsTrue(lResult == lPatternTest.Result, "5:" + lPatternTest.Expression); if ((lResult != lPatternTest.Result) || (lPatternInterpretor.IsError())) { Console.Error.WriteLine("PatternInterpretor::SelfTest (Failed)"); Console.Error.WriteLine("Failed Test: " + lPatternTest.Expression); Console.Error.Write("Got : " + lResult.ToString()); Console.Error.WriteLine(" Expected: " + lPatternTest.Result); } } } } }
protected static void PlaySequence(GoBoard goBoard, SearchEngine searchEngine, List<string> parameters, string id, Worker.SendResponse proxy) { if (parameters.Count > 0) { string[] lMoves = parameters[0].Split(' '); for (int i = 0; i < lMoves.Length / 2; i++) goBoard.PlayStone(lMoves[i * 2 + 1], Color.ToColor(lMoves[(i * 2)]), false); } Respond(proxy, id); }
public bool PlayStone(string move, Color color) { int lMove = CoordinateSystem.At(move, Board.BoardSize); return(Board.PlayStone(lMove, color, false)); }