// 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 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);
        }
Exemple #4
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);
        }
Exemple #5
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;
        }
Exemple #6
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;
        }
        /// <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));
        }
Exemple #8
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;
        }
        /// <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);
        }
        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));
                }
        }
        // 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);
        }
        // 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);
        }
Exemple #13
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);
        }
Exemple #17
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));
                    }
                }
            }
        }
        /// <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);
        }
Exemple #19
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;
        }
Exemple #20
0
        // 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;
        }
Exemple #21
0
        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;
        }
Exemple #22
0
        public static int IGS31()
        {
            GameRecords lGameRecords = new GameRecords();
            SGFCollection lSGFCollection = new SGFCollection(@"Regression\IGS_31\Source\IGS_31_counted.sgf");
            lGameRecords.Load(lSGFCollection, 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\IGS_31\IGS_31-combined.sgf");

            SafetySolverType lSafetySolverType = SafetySolverType.Muller97;

            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\IGS_31\IGS_31-" + ((i + 1).ToString()) + ".sgf");
            }

            return 0;
        }
 public static int Safe(GoBoard goBoard, Color player, PatternFunctionParameters <int> parameters)
 {
     return(goBoard.GetSafetyStatus(parameters[0]).IsAlive ? TRUE : FALSE);
 }
Exemple #24
0
 public static int Dead(GoBoard goBoard, Color player, PatternFunctionParameters<int> parameters)
 {
     return goBoard.GetSafetyStatus(parameters[0]).IsDead ? TRUE : FALSE;
 }
        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);
        }
Exemple #26
0
        // 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;
        }
Exemple #27
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);
        }
 public static int Unknown(GoBoard goBoard, Color player, PatternFunctionParameters <int> parameters)
 {
     return(goBoard.GetSafetyStatus(parameters[0]).IsUndecided ? TRUE : FALSE);
 }
Exemple #29
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;
        }
        /// <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;
        }
Exemple #31
0
        // 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;
        }