// 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);
        }
        public static double Score(GoBoard goBoard)
        {
            int Score = goBoard.CapturedStoneCnt[1] - goBoard.CapturedStoneCnt[0];

            for (int i = 0; i < goBoard.Coord.BoardArea; i++)
            {
                SafetyStatus lSafetyStatus = goBoard.GetSafetyStatus(i);

                if (lSafetyStatus.IsDead)
                {
                    if (lSafetyStatus.IsBlack)
                    {
                        Score--;
                    }
                    else
                    {
                        Score++;
                    }
                }
                else
                if (lSafetyStatus.IsTerritory)
                {
                    if (lSafetyStatus.IsBlack)
                    {
                        Score++;
                    }
                    else
                    {
                        Score--;
                    }
                }
            }

            return(Convert.ToDouble(Score) - goBoard.Komi);
        }
 public void Execute(GoBoard goBoard, Coordinate originPoint, int transform, MoveList moves)
 {
     foreach (PatternActionOperand lPatternActionOperand in Code)
     {
         lPatternActionOperand.Execute(goBoard, originPoint, transform, moves);
     }
 }
        // 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);
        }
Example #5
0
 public PatternMap(GoBoard goBoard, Color player)
 {
     Board            = goBoard;
     Player           = player;
     Patterns         = new List <PatternHit>();
     MergedAttributes = new PatternMergedAttributes[Board.Coord.BoardArea];
 }
        public override void Undo(GoBoard goBoard)
        {
            goBoard.Cells[Index].AssignCell(EmptyBlock);

            EmptyBlock.Members.Add(Index);
            EmptyBlock.MemberList.Add(Index);
        }
        public static int ConsoleGTP(MemFile file, bool loadSilent)
        {
            GoBoard        lGoBoard        = new GoBoard(9);
            GTPGoBoard     lGTPGoBoard     = new GTPGoBoard(lGoBoard);
            GTPCommConsole lGTPCommConsole = new GTPCommConsole();
            GTPEngine      lGTPEngine      = new GTPEngine(lGTPGoBoard, lGTPCommConsole);

            lGTPCommConsole.Silent = loadSilent;

            if (file != null)
            {
                while (!file.EOF)
                {
                    string lLine = file.ReadLine();
                    Console.Error.WriteLine(lLine.Trim('\n'));
                    lGTPCommConsole.SendToEngine(lLine + '\n');
                }
            }

            lGTPCommConsole.Silent = false;

            lGTPCommConsole.Listen();

            return(0);
        }
 public override void Undo(GoBoard goBoard)
 {
     foreach (int lIndex in EmptyBlock.MemberList)
     {
         goBoard.Cells[lIndex].AssignCell(EmptyBlock);
     }
 }
Example #9
0
        public override void Undo(GoBoard goBoard)
        {
            goBoard.Cells[Index].AssignCell(EmptyBlock);

            EmptyBlock.Members.Add(Index);
            EmptyBlock.MemberList.Add(Index);
        }
Example #10
0
        public void Start(GoBoard goBoard, Color playerToMove, SearchOptions searchOptions, SearchMethodType searchMethodType, OnCompletion onCompletion)
        {
            // stop existing search, if any
            if (SearchInterface != null)
            {
                Stop();
            }

            if (SearchMethodType != searchMethodType)
            {
                SearchMethodType = searchMethodType;
                SearchInterface  = SearchMethodFactory.CreateFactory(searchMethodType);
            }

            // make a private copy of the board
            Board = goBoard.Clone();

            // make a private copy of the search options
            SearchOptions = searchOptions.Clone();

            // set player to move
            PlayerToMove = playerToMove;

            // set the Nag Coordinator
            SearchInterface.SetNagCoordinator(NagCoordinator);

            // initialize the search parameters
            SearchInterface.Initialize(Board, PlayerToMove, SearchOptions, onCompletion);

            // start search
            SearchThread = new Thread(this.StartThread);
            SearchThread.Start();
        }
Example #11
0
        public void Start(GoBoard goBoard, Color playerToMove, SearchOptions searchOptions, SearchMethodType searchMethodType, OnCompletion onCompletion)
        {
            // stop existing search, if any
            if (SearchInterface != null)
                Stop();

            if (SearchMethodType != searchMethodType)
            {
                SearchMethodType = searchMethodType;
                SearchInterface = SearchMethodFactory.CreateFactory(searchMethodType);
            }

            // make a private copy of the board
            Board = goBoard.Clone();

            // make a private copy of the search options
            SearchOptions = searchOptions.Clone();

            // set player to move
            PlayerToMove = playerToMove;

            // set the Nag Coordinator
            SearchInterface.SetNagCoordinator(NagCoordinator);

            // initialize the search parameters
            SearchInterface.Initialize(Board, PlayerToMove, SearchOptions, onCompletion);

            // start search
            SearchThread = new Thread(this.StartThread);
            SearchThread.Start();
        }
        public static int IsSafeXMove(GoBoard goBoard, Color player, PatternFunctionParameters <int> parameters)
        {
            Color lPlayer = player.Opposite;

            return(((goBoard.IsLegal(parameters[0], lPlayer)) &&
                    (LibertyCountAfterMove(goBoard, lPlayer, parameters[0]) > 1)) ? TRUE : FALSE);
        }
Example #13
0
        public new void Initialize(GoBoard goBoard, Color playerToMove, SearchOptions searchOptions, OnCompletion onCompletion)
        {
            base.Initialize(goBoard, playerToMove, searchOptions, onCompletion);

            // setup distributed search coordinator & initialize workers
            if (NagCoordinator == null)
            {
                NagCoordinator = new NagCoordinator(9999, SearchOptions.PatternDetector.Patterns);                      // default port
            }
            NagCoordinator.SetNagCallBack(SetNag);
            NagCoordinator.Initialize(goBoard);

            if (TranspositionTable == null)
            {
                TranspositionTablePrimary = new TranspositionTablePlus(SearchOptions.TranspositionTableSize);
            }
            else
            if (TranspositionTable.Size != SearchOptions.TranspositionTableSize)
            {
                TranspositionTablePrimary = new TranspositionTablePlus(SearchOptions.TranspositionTableSize);
            }

            if (TranspositionTableEndGame == null)
            {
                TranspositionTableEndGame = new TranspositionTablePlus(1024 * 1024);
            }

            TranspositionTable = TranspositionTablePrimary;
        }
Example #14
0
        protected static void SetAlphaBeta(GoBoard goBoard, SearchEngine searchEngine, List <string> parameters, string id, Worker.SendResponse proxy)
        {
            searchEngine.SearchOptions.AlphaValue = Convert.ToInt32(parameters[0]);
            searchEngine.SearchOptions.BetaValue  = Convert.ToInt32(parameters[1]);

            Respond(proxy, id);
        }
Example #15
0
 public void Solve(GoBoard goBoard, Color color)
 {
     Board = goBoard;
     Color = color;
     SafetySolver.Solve(goBoard, color);
     ProtectedLiberitiesLeft = GoBoardHelper.GetProtectedLiberites(Board, color);
 }
 public override void Undo(GoBoard goBoard)
 {
     foreach (GoBlock lGoBlock in Blocks)
     {
         lGoBlock.Liberties.Add(Index);
     }
 }
        public static void GamePlayTestSolver(int tests, bool withSolver)
        {
            GoBoard    lGoBoard    = new GoBoard(19);
            GameRecord lGameRecord = new GameRecord();

            SGFCollection lSGFCollection = new SGFCollection();

            lSGFCollection.LoadSGFFromMemory(SGFGameSamples.GAME_1993_ZHONG_JIALIN_HANE_YASUMASA_1);
            lSGFCollection.RetrieveGame(lGameRecord, 0);

            SimpleTimer lSimpleTimer = new SimpleTimer();

            GameRecordBoardAdapter.Apply(lGameRecord, lGoBoard, true);
            //lGameRecord.Apply(lGoBoard, true);

            for (int i = 0; i < tests; i++)
            {
                if (withSolver)
                {
                    lGoBoard.SafetyStatusMap = null;
                    int lSafePoints = lGoBoard.CountSafePoints(Color.Black);
                    lSafePoints += lGoBoard.CountSafePoints(Color.White);
                }
            }

            lSimpleTimer.Stop();

            Console.Write("19x19 Game - Solver [ ");
            Console.Write("] # " + tests.ToString() + " times. ");
            Console.Write("Elapsed: " + lSimpleTimer.MilliSecondsElapsed.ToString() + " ms ");
            Console.WriteLine("Avg.: " + (lSimpleTimer.MilliSecondsElapsed / tests).ToString() + " ms ");

            return;
        }
Example #18
0
        public PatternMap FindPatterns(GoBoard goBoard, Color playerToMove)
        {
            PatternMap lPatternMap = new PatternMap(goBoard, playerToMove);

            if (DFAMatrix == null)
            {
                return(lPatternMap);
            }

            for (int lOrigin = 0; lOrigin < goBoard.Coord.BoardArea; lOrigin++)
            {
                if (goBoard.GetSafetyStatus(lOrigin).IsUndecided)
                {
                    if (goBoard.IsLegal(lOrigin, playerToMove))
                    {
                        Coordinate lStart = new Coordinate(goBoard.Coord.GetColumn(lOrigin), goBoard.Coord.GetRow(lOrigin));

                        int lState = 1;

                        Coordinate lSpiral = new Coordinate(0, 0);

                        while (true)
                        {
                            Coordinate lAt = lStart + lSpiral;
                            lSpiral.SpiralNext();

                            char c = '#';

                            if (lAt.IsOnBoard(goBoard.BoardSize))
                            {
                                c = goBoard.GetColor(goBoard.Coord.At(lAt.X, lAt.Y)).ToChar();

                                if (playerToMove.IsBlack)
                                {
                                    // patterns are stored in white moves next
                                    // so flip colors when is black's turn
                                    if (c == 'O')
                                    {
                                        c = 'X';
                                    }
                                    else if (c == 'X')
                                    {
                                        c = 'O';
                                    }
                                }
                            }

                            lState = DFAMatrix.GetPatterns(lState, c, lPatternMap, lStart, lOrigin);

                            if (lState == 0)
                            {
                                break;
                            }
                        }
                    }
                }
            }
            return(lPatternMap);
        }
        public SafetyMap(GoBoard goBoard, SafetySolverType pSafetySolverType)
        {
            Board  = goBoard;
            Safety = new SafetyStatus[Board.Coord.BoardArea];

            SafetySolverType = pSafetySolverType;
            Execute();
        }
Example #20
0
        public SafetyMap(GoBoard goBoard, SafetySolverType pSafetySolverType)
        {
            Board = goBoard;
            Safety = new SafetyStatus[Board.Coord.BoardArea];

            SafetySolverType = pSafetySolverType;
            Execute();
        }
Example #21
0
        public ColorEnclosedRegions(GoBoard goBoard)
        {
            Board = goBoard;
            Regions = new List<ColorEnclosedRegion>[2];

            foreach (Color lColor in Color.Colors)
                CreateRegions(lColor);
        }
Example #22
0
        public GoEmptyBlock(GoBoard goBoard)
            : base(goBoard, Color.Empty)
        {
            Members.AddAll();

            for (int i = 0; i < Board.Coord.BoardArea; i++)
                MemberList.Add(i);
        }
Example #23
0
 public GTPGoBoard(GoBoard goBoard)
 {
     RegisteredCommands = new Dictionary<string, GTPFunction>();
     AnalyzeCommands = new List<AnalyzeCommand>();
     Directory = string.Empty;
     RegisterBuildInCommands();
     Board = goBoard;
     SearchEngine = new SearchEngine(Board);
 }
        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());
        }
Example #25
0
//		public int NeighborCnt;

        public GoCell(int index, GoBoard goBoard, GoEmptyBlock goEmptyBlock)
        {
            Board = goBoard;
            Color = Color.Empty;
            Block = goEmptyBlock;
            Index = index;

            Board.ZobristHash.Delta(Color, Index);
        }
Example #26
0
        protected static void WriteLine(GoBoard goBoard, SearchEngine searchEngine, List <string> parameters, string id, Worker.SendResponse proxy)
        {
            foreach (string lString in parameters)
            {
                Console.Error.WriteLine("DEBUG: " + lString);
            }

            Respond(proxy, id);
        }
Example #27
0
        //        public int NeighborCnt;
        public GoCell(int index, GoBoard goBoard, GoEmptyBlock goEmptyBlock)
        {
            Board = goBoard;
            Color = Color.Empty;
            Block = goEmptyBlock;
            Index = index;

            Board.ZobristHash.Delta(Color, Index);
        }
Example #28
0
        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());
        }
Example #29
0
        protected static void Search(GoBoard goBoard, SearchEngine searchEngine, List <string> parameters, string id, Worker.SendResponse proxy)
        {
            Color color = Color.ToColor(parameters[0]);

            AsyncResponseHelper lAsyncResponseHelper = new AsyncResponseHelper(proxy, id);

            Respond(proxy, id);

            searchEngine.SimpleAsyncSearch(color, lAsyncResponseHelper.OnCompletion);
        }
Example #30
0
        public void Run()
        {
            Board        = new GoBoard(9);
            SearchEngine = new SearchEngine(Board);

            SearchEngine.SearchOptions.EarlyTimeOut = false;
            SearchEngine.SearchOptions.MaxSeconds   = 60 * 60 * 5;              // five hours max.

            Server.Run(ProcessLine);
        }
Example #31
0
        public GoEmptyBlock(GoBoard goBoard)
            : base(goBoard, Color.Empty)
        {
            Members.AddAll();

            for (int i = 0; i < Board.Coord.BoardArea; i++)
            {
                MemberList.Add(i);
            }
        }
Example #32
0
        public GoBoard(GoBoard goBoard, bool includeUndo)
        {
            Coord = CoordinateSystem.GetCoordinateSystem(goBoard.BoardSize);
            SafetySolver = goBoard.SafetySolver;
            Komi = goBoard.Komi;
            Clear();

            foreach (KeyValuePair<Color, int> lMove in goBoard.MoveList)
                PlayStone(lMove.Value, lMove.Key, includeUndo);
        }
 public override void Undo(GoBoard goBoard)
 {
     foreach (GoBlock lBlock in OldBlocks)
     {
         foreach (int lIndex in lBlock.MemberList)
         {
             goBoard.Cells[lIndex].AssignCell(lBlock);
         }
     }
 }
        public ColorEnclosedRegions(GoBoard goBoard)
        {
            Board   = goBoard;
            Regions = new List <ColorEnclosedRegion> [2];

            foreach (Color lColor in Color.Colors)
            {
                CreateRegions(lColor);
            }
        }
Example #35
0
        // marked to be depreciated
        public static List<int> CaptureDeadStones(GoBoard goBoard, Color playerToMove)
        {
            List<int> lMoves = new List<int>();
            for (int i = 0; i < goBoard.Coord.BoardArea; i++)
                if (goBoard.GetSafetyStatus(i).IsDead)
                    if (goBoard.Cells[i].Color != playerToMove)
                        lMoves.Add(i);

            return lMoves;
        }
Example #36
0
        public static int ConnectToCGOS(string name, string pPwd, int pNbr)
        {
            GoBoard lGoBoard = new GoBoard(9);
            GTPGoBoard lGTPGoBoard = new GTPGoBoard(lGoBoard);
            GTPCommCGOS lGTPCommCGOS = new GTPCommCGOS("cgos.boardspace.net", 6867, name, pPwd, pNbr, true);
            GTPEngine lGTPEngine = new GTPEngine(lGTPGoBoard, lGTPCommCGOS);

            lGTPCommCGOS.Run();

            return 0;
        }
        public int BlockNbr;                                    // used for debugging

        public GoBlockBase(GoBoard goBoard, Color blockColor)
        {
            Board          = goBoard;
            BlockColor     = blockColor;
            AdjacentBlocks = new GoBlocksAdjacentCollection(this);

            Members    = new Region(Board.BoardSize);
            MemberList = new List <int>();

            BlockNbr = ++BlockNbrCounter;
        }
Example #38
0
        protected static int BlockNbrCounter = 0; // used for debugging

        #endregion Fields

        #region Constructors

        public GoBlockBase(GoBoard goBoard, Color blockColor)
        {
            Board = goBoard;
            BlockColor = blockColor;
            AdjacentBlocks = new GoBlocksAdjacentCollection(this);

            Members = new Region(Board.BoardSize);
            MemberList = new List<int>();

            BlockNbr = ++BlockNbrCounter;
        }
Example #39
0
        public override void Undo(GoBoard goBoard)
        {
            Block.MemberList.Remove(Index);
            Block.Members.Remove(Index);
            Block.Liberties.Add(Index);

            foreach (int lIndex in Block.Board.Coord.GetNeighbors(Index))
                if (Block.Board.Cells[lIndex].Color.IsEmpty)
                    if (!Block.IsNeighbor(lIndex))
                        Block.Liberties.Remove(lIndex);
        }
Example #40
0
        public static int ConnectToCGOS(string name, string pPwd, int pNbr)
        {
            GoBoard     lGoBoard     = new GoBoard(9);
            GTPGoBoard  lGTPGoBoard  = new GTPGoBoard(lGoBoard);
            GTPCommCGOS lGTPCommCGOS = new GTPCommCGOS("cgos.boardspace.net", 6867, name, pPwd, pNbr, true);
            GTPEngine   lGTPEngine   = new GTPEngine(lGTPGoBoard, lGTPCommCGOS);

            lGTPCommCGOS.Run();

            return(0);
        }
 /// <summary>
 /// Gets the score.
 /// </summary>
 /// <param name="goBoard">The go board.</param>
 /// <param name="color">The color.</param>
 /// <returns></returns>
 public static int GetScore(GoBoard goBoard, Color color)
 {
     if (color.IsBlack)
     {
         return(GetScore2(goBoard));
     }
     else
     {
         return(-GetScore2(goBoard));
     }
 }
Example #42
0
        protected static int GetEndGameMove(GoBoard goBoard, Color playerToMove)
        {
            Random     lRandom = new Random();
            List <int> lList   = GoBoardHelper.GetMovesInOpponentsTerritory(goBoard, playerToMove);

            if (lList.Count != 0)
            {
                return(lList[lRandom.Next(lList.Count - 1)]);
            }

            return(CoordinateSystem.PASS);
        }
Example #43
0
        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;
        }
Example #44
0
        public GoBoard(GoBoard goBoard, bool includeUndo)
        {
            Coord        = CoordinateSystem.GetCoordinateSystem(goBoard.BoardSize);
            SafetySolver = goBoard.SafetySolver;
            Komi         = goBoard.Komi;
            Clear();

            foreach (KeyValuePair <Color, int> lMove in goBoard.MoveList)
            {
                PlayStone(lMove.Value, lMove.Key, includeUndo);
            }
        }
Example #45
0
        public void Execute(GoBoard goBoard, Coordinate originPoint, int transform, MoveList moves)
        {
            PatternFunctionParameters<int> lPatternFunctionParameters = new PatternFunctionParameters<int>();

            foreach (Coordinate lCoordinate in Parameters)
            {
                Coordinate lCoordinate2 = originPoint + (lCoordinate - Origin).Transform(transform);

                lPatternFunctionParameters.Add(goBoard.Coord.At(lCoordinate2.X, lCoordinate2.Y));
            }

            Function(moves, lPatternFunctionParameters);
        }
Example #46
0
        // marked to be depreciated
        public static List<int> FillDame(GoBoard goBoard, Color playerToMove)
        {
            List<int> lMoves = new List<int>();
            for (int i = 0; i < goBoard.Coord.BoardArea; i++)
            {
                SafetyStatus lSafetyStatus = goBoard.GetSafetyStatus(i);

                if (lSafetyStatus.IsDame || lSafetyStatus.IsUnsurroundable)
                    lMoves.Add(i);
            }

            return lMoves;
        }
Example #47
0
        public GoBoardUndoState(GoBoard goBoard)
        {
            Turn = goBoard.Turn;
            SimpleKoPoint = goBoard.SimpleKoPoint;
            Komi = goBoard.Komi;

            CapturedStoneCnt0 = goBoard.CapturedStoneCnt[0];
            CapturedStoneCnt1 = goBoard.CapturedStoneCnt[1];
            LastMove = goBoard.LastMove;

            SafetyStatusMap = goBoard.SafetyStatusMap;
            GameOver = goBoard.GameOver;
            ColorEnclosedRegions = goBoard.ColorEnclosedRegions;
        }
Example #48
0
        // marked to be depreciated
        public static int FindAnyMoveInOpponentsTerritory(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.Opposite))
                    if (goBoard.IsLegal(lPoint, playerToMove))
                        return lPoint;
            }

            return CoordinateSystem.PASS;
        }
Example #49
0
        public NagNode(GoBoard goBoard, int alpha, int beta, Color playerToMove, int startDepth, int depth, int permutationNbr)
        {
            foreach (KeyValuePair<Color, int> lMove in goBoard.MoveList)
                MoveList.Add(new KeyValuePair<Color, int>(lMove.Key, lMove.Value));

            Alpha = alpha;
            Beta = beta;
            PlayerToMove = playerToMove;
            Depth = depth;
            ZobristHash = goBoard.ZobristHash.Clone();
            NarrowedAlpha = alpha;
            NarrowedBeta = beta;
            StartDepth = startDepth;
            PermutationNbr = 1;
        }
Example #50
0
        public int Execute(GoBoard goBoard, Color player, Coordinate originPoint, int transform)
        {
            Board = goBoard;
            Player = player;
            OriginPoint = originPoint;
            Transform = transform;
            ClearErrorMessages();

            Position = 0;

            if (PatternOperands.Count == 0)
                return TRUE;

            return EvaulateExpression();
        }
Example #51
0
        public override void Undo(GoBoard goBoard)
        {
            GoEmptyBlock lEmptyBlock = (GoEmptyBlock)goBoard.Cells[Block.MemberList[0]].Block;

            // clear liberties in adjacent blocks
            foreach (int lStone in Block.MemberList)
                foreach (GoBlock lGoBlock in lEmptyBlock.AdjacentBlocks.StoneBlocks)
            //					if (lGoBlock.IsNeighbor(lStone))
            //						if (lGoBlock.BlockColor != Block.BlockColor)
                            lGoBlock.Liberties.Remove(lStone);

            // placing stones back onto the board
            foreach (int lStone in Block.MemberList)
                goBoard.Cells[lStone].AssignCell(Block);
        }
Example #52
0
        public new void Initialize(GoBoard goBoard, Color playerToMove, SearchOptions searchOptions, OnCompletion onCompletion)
        {
            base.Initialize(goBoard, playerToMove, searchOptions, onCompletion);

            if (TranspositionTable == null)
                TranspositionTablePrimary = new TranspositionTablePlus(SearchOptions.TranspositionTableSize);
            else
                if (TranspositionTable.Size != SearchOptions.TranspositionTableSize)
                    TranspositionTablePrimary = new TranspositionTablePlus(SearchOptions.TranspositionTableSize);

            if (TranspositionTableEndGame == null)
                TranspositionTableEndGame = new TranspositionTablePlus(1024 * 1024);

            TranspositionTable = TranspositionTablePrimary;
        }
Example #53
0
        public static void UpdateTerritory(GameRecord gameRecord, GoBoard goBoard)
        {
            if (goBoard.BoardSize != gameRecord.BoardSize)
                throw new ApplicationException("BUG");

            gameRecord.ClearTerritory();

            for (int x = 0; x < goBoard.BoardSize; x++)
                for (int y = 0; y < goBoard.BoardSize; y++)
                {
                    SafetyStatus lSafetyStatus = goBoard.GetSafetyStatus((goBoard.Coord.At(x, y)));

                    if (lSafetyStatus.IsAlive || lSafetyStatus.IsTerritory)
                        gameRecord.SetTerritory(lSafetyStatus.Player, goBoard.Coord.At(x, y));
                }
        }
Example #54
0
        public override void Undo(GoBoard goBoard)
        {
            goBoard.Turn = Turn;
            goBoard.SimpleKoPoint = SimpleKoPoint;
            goBoard.Komi = Komi;
            goBoard.MoveNbr--;
            goBoard.LastMove = LastMove;
            //            goBoard.LastMove = (goBoard.MoveNbr == 0) ? CoordinateSystem.PASS : goBoard.MoveList[goBoard.MoveNbr];
            goBoard.MoveList.RemoveAt(goBoard.MoveList.Count - 1);

            goBoard.CapturedStoneCnt[0] = CapturedStoneCnt0;
            goBoard.CapturedStoneCnt[1] = CapturedStoneCnt1;

            goBoard.ColorEnclosedRegions = ColorEnclosedRegions;
            goBoard.SafetyStatusMap = SafetyStatusMap;

            goBoard.GameOver = GameOver;
        }
Example #55
0
        /// <summary>
        /// Evaulates the board position.
        /// </summary>
        /// <param name="goBoard">The go board.</param>
        /// <param name="color">The color.</param>
        /// <returns></returns>
        public static int EvaulateBoardPosition(GoBoard goBoard)
        {
            int[] StoneCaptured = new int[2];
            int[] AliveStones = new int[2];
            int[] DeadStones = new int[2];
            int[] Territory = new int[2];
            //			int[] UnknownStones = new int[2];

            // Score should be positive if black is winning
            StoneCaptured[0] = goBoard.CapturedStoneCnt[0];
            StoneCaptured[1] = goBoard.CapturedStoneCnt[1];

            for (int i = 0; i < goBoard.Coord.BoardArea; i++)
            {
                SafetyStatus lSafetyStatus = goBoard.GetSafetyStatus(i);

                if (lSafetyStatus.IsAlive)
                    AliveStones[lSafetyStatus.Player.ToInteger()]++;
                else
                    if (lSafetyStatus.IsDead)
                        DeadStones[lSafetyStatus.Player.ToInteger()]++;
                    else
                        if (lSafetyStatus.IsTerritory)
                            Territory[lSafetyStatus.Player.ToInteger()]++;
                //						else
                //							if ((lSafetyStatus.IsUndecided) && (lSafetyStatus.Player != Color.Empty))
                //								UnknownStones[lSafetyStatus.Player.ToInteger()]++;
            }

            double lScore = Territory[0] - Territory[1] + AliveStones[0] - AliveStones[1] - DeadStones[0] + DeadStones[1];

            if (StoneCaptured[0] != StoneCaptured[1])
                if (StoneCaptured[0] > StoneCaptured[1])
                    lScore = lScore + 0.5;
                else
                    lScore = lScore - 0.5;

            return Convert.ToInt32(lScore * 10);
        }
Example #56
0
        public static void GamePlayTest(int tests, bool withUndo, bool withSolver)
        {
            GoBoard lGoBoard = new GoBoard(19);
            GameRecord lGameRecord = new GameRecord();

            SGFCollection lSGFCollection = new SGFCollection();
            lSGFCollection.LoadSGFFromMemory(SGFGameSamples.GAME_1993_ZHONG_JIALIN_HANE_YASUMASA_1);
            lSGFCollection.RetrieveGame(lGameRecord, 0);

            SimpleTimer lSimpleTimer = new SimpleTimer();

            for (int i = 0; i < tests; i++)
            {
                GameRecordBoardAdapter.Apply(lGameRecord, lGoBoard, true);
                //lGameRecord.Apply(lGoBoard, true);

                if (withSolver)
                {
                    int lSafePoints = lGoBoard.CountSafePoints(Color.Black);
                    lSafePoints += lGoBoard.CountSafePoints(Color.White);
                }

                if (withUndo)
                    while (lGoBoard.CanUndo())
                        lGoBoard.Undo();
            }

            lSimpleTimer.Stop();

            Console.Write("19x19 Game [ ");
            Console.Write((withUndo ? "+Undo " : ""));
            Console.Write((withSolver ? "+Solver " : ""));
            Console.Write("] # "+tests.ToString()+" times. ");
            Console.Write("Elapsed: " + lSimpleTimer.MilliSecondsElapsed.ToString() + " ms ");
            Console.WriteLine("Avg.: " + (lSimpleTimer.MilliSecondsElapsed / tests).ToString() + " ms ");

            return;
        }
Example #57
0
        public static int ConsoleGTP(MemFile file, bool loadSilent)
        {
            GoBoard lGoBoard = new GoBoard(9);
            GTPGoBoard lGTPGoBoard = new GTPGoBoard(lGoBoard);
            GTPCommConsole lGTPCommConsole = new GTPCommConsole();
            GTPEngine lGTPEngine = new GTPEngine(lGTPGoBoard, lGTPCommConsole);

            lGTPCommConsole.Silent = loadSilent;

            if (file != null)
                while (!file.EOF)
                {
                    string lLine = file.ReadLine();
                    Console.Error.WriteLine(lLine.Trim('\n'));
                    lGTPCommConsole.SendToEngine(lLine + '\n');
                }

            lGTPCommConsole.Silent = false;

            lGTPCommConsole.Listen();

            return 0;
        }
Example #58
0
        public static double Score(GoBoard goBoard)
        {
            int Score = 0;

            for (int i = 0; i < goBoard.Coord.BoardArea; i++)
            {
                SafetyStatus lSafetyStatus = goBoard.GetSafetyStatus(i);

                if (lSafetyStatus.IsAlive)
                    if (lSafetyStatus.IsBlack)
                        Score++;
                    else
                        Score--;
                else
                    if (lSafetyStatus.IsTerritory)
                        if (lSafetyStatus.IsBlack)
                            Score++;
                        else
                            Score--;
            }

            return Convert.ToDouble(Score) - goBoard.Komi;
        }
Example #59
0
        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;
        }
Example #60
0
        public static bool Execute(string command, string id, List<string> parameters, GoBoard goBoard, SearchEngine searchEngine, Worker.SendResponse proxy)
        {
            WorkerFunction lFunction = GetFunction(command, parameters.Count);

            if (lFunction == null)
                return false;

            try
            {
                lFunction(goBoard, searchEngine, parameters, id, proxy);
                return true;
            }
            catch (Exception e)
            {
                Console.Error.Write("ERROR: " + e.ToString());
                Respond(proxy, id, false, command, true); // async response
                return false;
            }
        }