// 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);
        }
        // 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);
        }
예제 #4
0
        protected void Update(GoBlockBase goBlock, SafetyFlag safetyFlag)
        {
            SafetyStatus lSafetyStatus = new SafetyStatus(safetyFlag);

            foreach (int lIndex in goBlock.MemberList)
            {
                Safety[lIndex] = lSafetyStatus;
            }
        }
        /// <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));
        }
        /// <summary>
        /// Gets the score.
        /// </summary>
        /// <param name="goBoard">The go board.</param>
        /// <returns></returns>
        public static int GetScore2(GoBoard goBoard)
        {
            // Score should be positive if black is winning
            int lStone    = 0;
            int lCaptures = goBoard.CapturedStoneCnt[1] - goBoard.CapturedStoneCnt[0];

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

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

            return(lStone + lCaptures);
        }
        // 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);
        }
예제 #8
0
        public override TriState IsGoalAccomplished()
        {
            Color        lColor  = Board.GetColor(BlockPosition);
            SafetyStatus lSafety = Board.GetSafetyStatus(BlockPosition);

            if (lColor == Player)
            {
                if (lSafety.IsAlive)
                {
                    return(TriState.True);
                }

                if (lSafety.IsDead)
                {
                    return(TriState.False);
                }
            }
            else
            if (lColor == Player.Opposite)
            {
                if (lSafety.IsAlive)
                {
                    return(TriState.False);
                }

                if (lSafety.IsDead)
                {
                    return(TriState.True);                              // well, maybe (turned into safe territory)
                }
            }
            else
            if (lColor == Color.Empty)
            {
                if (lSafety.IsTerritory && lSafety.Player == Player)
                {
                    return(TriState.True);                                      // well, maybe (turned into safe territory)
                }
                if (lSafety.IsTerritory && lSafety.Player == Player.Opposite)
                {
                    return(TriState.False);
                }
            }

            return(TriState.Unknown);
        }
        // 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);
        }
        // marked to be depreciated
        public static List <int> GetLegalSafeMoves(GoBoard goBoard, Color playerToMove)
        {
            List <int> lMoves = new List <int>();

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

                    if (lSafetyStatus.IsUndecided || (lSafetyStatus.IsUnsurroundable && !lSafetyStatus.IsDame))
                    {
                        lMoves.Add(i);
                    }
                }
            }

            return(lMoves);
        }
        public static List <int> GetMovesInOpponentsTerritory(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.Opposite))
                {
                    if (goBoard.IsLegal(lPoint, playerToMove))
                    {
                        lMoves.Add(lPoint);
                    }
                }
            }

            return(lMoves);
        }
예제 #12
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));
                    }
                }
            }
        }
예제 #13
0
        protected void MarkDamePoints()
        {
            for (int lIndex = 0; lIndex < Board.Coord.BoardArea; lIndex++)
            {
                if (Safety[lIndex].IsUnsurroundable)
                {
                    bool lDame = true;

                    foreach (GoCell lGoCell in Board.GetNeighboringCells(lIndex))
                    {
                        if (Safety[lGoCell.Index].IsUndecided)
                        {
                            lDame = false;
                            break;
                        }
                    }

                    if (lDame)
                    {
                        Safety[lIndex] = new SafetyStatus(SafetyFlag.Dame);
                    }
                }
            }
        }
예제 #14
0
        protected void MarkUnsurroundablePoints()
        {
            for (int lIndex = 0; lIndex < Board.Coord.BoardArea; lIndex++)
            {
                if (Safety[lIndex].IsUndecided)
                {
                    bool[] lFoundAlive = new bool[2];
                    lFoundAlive[0] = lFoundAlive[1] = false;

                    foreach (GoCell lGoCell in Board.GetNeighboringCells(lIndex))
                    {
                        if (Safety[lGoCell.Index].IsAlive)
                        {
                            lFoundAlive[lGoCell.Color.ToInteger()] = true;
                        }
                    }

                    if (lFoundAlive[0] && lFoundAlive[1])
                    {
                        Safety[lIndex] = new SafetyStatus(SafetyFlag.Unsurroundable);
                    }
                }
            }
        }
예제 #15
0
        protected void MarkDamePoints()
        {
            for (int lIndex = 0; lIndex < Board.Coord.BoardArea; lIndex++)
                if (Safety[lIndex].IsUnsurroundable)
                {
                    bool lDame = true;

                    foreach (GoCell lGoCell in Board.GetNeighboringCells(lIndex))
                        if (Safety[lGoCell.Index].IsUndecided)
                        {
                            lDame = false;
                            break;
                        }

                    if (lDame)
                        Safety[lIndex] = new SafetyStatus(SafetyFlag.Dame);
                }
        }
예제 #16
0
        protected void MarkUnsurroundablePoints()
        {
            for (int lIndex = 0; lIndex < Board.Coord.BoardArea; lIndex++)
                if (Safety[lIndex].IsUndecided)
                {
                    bool[] lFoundAlive = new bool[2];
                    lFoundAlive[0] = lFoundAlive[1] = false;

                    foreach (GoCell lGoCell in Board.GetNeighboringCells(lIndex))
                        if (Safety[lGoCell.Index].IsAlive)
                            lFoundAlive[lGoCell.Color.ToInteger()] = true;

                    if (lFoundAlive[0] && lFoundAlive[1])
                        Safety[lIndex] = new SafetyStatus(SafetyFlag.Unsurroundable);
                }
        }
예제 #17
0
        protected void Update(GoBlockBase goBlock, SafetyFlag safetyFlag)
        {
            SafetyStatus lSafetyStatus = new SafetyStatus(safetyFlag);

            foreach (int lIndex in goBlock.MemberList)
                Safety[lIndex] = lSafetyStatus;
        }
예제 #18
0
        /// <summary>
        /// Gets a list of legal moves.
        /// </summary>
        /// <param name="color">The color.</param>
        /// <returns></returns>
        public MoveList GetMoveList(Color playerToMove, bool forceEndGameMoves)
        {
            MoveList lMoves = new MoveList(Board.BoardSize);

            if (!forceEndGameMoves)
            {
                for (int lMove = 0; lMove < Board.Coord.BoardArea; lMove++)
                {
                    if (Board.IsLegal(lMove, playerToMove))
                    {
                        SafetyStatus lSafetyStatus = Board.GetSafetyStatus(lMove);

                        if (lSafetyStatus.IsUndecided || (lSafetyStatus.IsUnsurroundable && !lSafetyStatus.IsDame))
                        {
                            lMoves.Add(lMove);
                        }
                    }
                }

                lMoves.Add(CoordinateSystem.PASS);

                return(lMoves);
            }

            // during the very final end game - when players fill dame and capture dead stones

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

                if ((lSafetyStatus.IsUnsurroundable) || (lSafetyStatus.IsDame))
                {
                    if (Board.IsLegal(lMove, playerToMove))
                    {
                        if (lSafetyStatus.IsUnsurroundable)
                        {
                            lMoves.Add(lMove);
                        }
                        else
                        if (lSafetyStatus.IsDame)
                        {
                            lMoves.Add(lMove);
                        }
                    }
                }
            }

            if (lMoves.Count != 0)
            {
                return(lMoves);
            }

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

                if ((lSafetyStatus.IsDead) && (Board.Cells[lMove].Color == playerToMove.Opposite))
                {
                    foreach (int lMove2 in ((GoBlock)Board.Cells[lMove].Block).Liberties)
                    {
                        if (Board.IsLegal(lMove2, playerToMove))
                        {
                            lMoves.Add(lMove2);
                        }
                    }
                }
            }

            // just in case / should still be fast
            lMoves.Add(CoordinateSystem.PASS);

            return(lMoves);
        }
예제 #19
0
        public static int CZD()
        {
            GameRecords lGameRecords = new GameRecords();

            lGameRecords.Load(@"Regression\CZD\Source", @"CZD_*.sgf", true);

            Dictionary <SafetyStatus, int> lSafetyUsage = new Dictionary <SafetyStatus, int>();

            foreach (GameRecord lGameRecord in lGameRecords.Games)
            {
                GoBoard lGoBoard = new GoBoard(19);
                GameRecordBoardAdapter.Apply(lGameRecord, lGoBoard);
                //lGameRecord.Apply(lGoBoard);

                foreach (GoCell lCell in lGoBoard.Cells)
                {
                    SafetyStatus lSafetyStatus = lGoBoard.GetSafetyStatus(lCell.Index);

                    if (lSafetyUsage.ContainsKey(lSafetyStatus))
                    {
                        lSafetyUsage[lSafetyStatus] += 1;
                    }
                    else
                    {
                        lSafetyUsage[lSafetyStatus] = 1;
                    }
                }

                Console.Write(".");
            }
            Console.WriteLine();
            foreach (SafetyStatus lSafetyStatus in lSafetyUsage.Keys)
            {
                Console.Write(lSafetyStatus.ToInteger());
                Console.Write(" | {0}", lSafetyStatus);
                Console.WriteLine(" | " + lSafetyUsage[lSafetyStatus].ToString());
            }

            MemFile lMemFile = new MemFile();

            lMemFile.WriteLine(lGameRecords.ToString());
            lMemFile.SaveFile(@"Regression\CZD\CZD-combined.sgf");

            SafetySolverType lSafetySolverType = SafetySolverType.Muller04;

            for (int i = 0; i < lGameRecords.Games.Count; i++)
            {
                GoBoard lGoBoard = new GoBoard(19);
                GameRecordBoardAdapter.Apply(lGameRecords[i], lGoBoard);
                //lGameRecords[i].Apply(lGoBoard);
                GameRecordBoardAdapter.UpdateTerritory(lGameRecords[i], lGoBoard);
                //lGameRecords[i].UpdateTerritory(lGoBoard);
                //Console.WriteLine(lGoBoard.ToString());
                Console.Write(i.ToString());
                Console.Write(" : ");
                Console.Write(lGoBoard.CountSafePoints(Color.Both, lSafetySolverType).ToString());
                Console.Write(" ");
                Console.Write(lGoBoard.CountSafePoints(Color.Black, lSafetySolverType).ToString());
                Console.Write(" ");
                Console.WriteLine(lGoBoard.CountSafePoints(Color.White, lSafetySolverType).ToString());
            }

            for (int i = 0; i < lGameRecords.Games.Count; i++)
            {
                MemFile lMemFile2 = new MemFile();
                lMemFile2.WriteLine(lGameRecords[i].ToString());
                lMemFile2.SaveFile(@"Regression\CZD\" + lGameRecords[i].GameName.Insert(3, "-") + ".sgf");
            }

            return(0);
        }