Exemplo n.º 1
0
        /// <summary>
        /// 指定したポイントについて、全てのリバティーが開いているスペースを探します。
        ///
        /// 再帰します。
        ///
        /// Gnugo1.2 では findopen関数。
        /// </summary>
        /// <param name="try3Locations">(mutable)隣接する東西南北の最大3方向がこの配列に入るはず。Gnugo1.2 では、i配列、j配列。</param>
        /// <param name="curLocation">Gnugo1.2では、 カレント 行番号 m = 0〜18、列番号 n = 0〜18。</param>
        /// <param name="color">黒 or 白</param>
        /// <param name="liberty123OfPiece">Gnugo1.2 では minlib 引数。3以下のリバティー</param>
        /// <param name="taikyoku"></param>
        /// <returns></returns>
        public static bool FindOpen3Locations
        (
            List <GobanPoint> try3Locations,
            GobanPoint curLocation,
            StoneColor color,
            int liberty123OfPiece,
            Taikyoku taikyoku
        )
        {
            bool  result;
            Board ban = taikyoku.Goban; // 碁盤

            // この位置はもう調べた、というフラグを立てます。
            taikyoku.MarkingBoard.Done_Current(curLocation);

            // 北ネイバー
            if (!curLocation.IsNorthEnd())//北端でなければ
            {
                if
                (
                    // 北隣が空っぽで。
                    ban.NorthOf(curLocation) == StoneColor.Empty
                    &&
                    // 北隣が、石を取った場所(コウになるかもしれない)でなければ。
                    !taikyoku.MyKo.Is_NorthOf(curLocation)
                )
                {
                    // 北隣を候補として追加。
                    try3Locations.Add(curLocation.ToNorth());
                    if (try3Locations.Count == liberty123OfPiece)
                    {
                        // リバティーの数分追加したなら正常終了。
                        result = true;
                        goto gt_EndMethod;
                    }
                }
                else if
                (
                    // 指定したポイントの北隣が指定の色で。
                    ban.NorthOf(curLocation) == color
                    &&
                    taikyoku.MarkingBoard.CanDo_North(curLocation) // 北側をまだ調べていないなら
                )
                {
                    if
                    (
                        Util_FindOpen.FindOpen3Locations(try3Locations, curLocation.ToNorth(), color, liberty123OfPiece, taikyoku)
                        &&
                        try3Locations.Count == liberty123OfPiece
                    )
                    {
                        result = true;
                        goto gt_EndMethod;
                    }
                }
            }

            // 南ネイバーを調べます。
            if (!curLocation.IsSouthEnd(taikyoku.GobanBounds))
            {
                if
                (
                    ban.SouthOf(curLocation) == StoneColor.Empty
                    &&
                    // 南隣が、取った石(コウかもしれない)でなければ。
                    !taikyoku.MyKo.Is_SouthOf(curLocation)
                )
                {
                    try3Locations.Add(curLocation.ToSouth());
                    if (try3Locations.Count == liberty123OfPiece)
                    {
                        result = true;
                        goto gt_EndMethod;
                    }
                }
                else
                {
                    if
                    (
                        ban.SouthOf(curLocation) == color
                        &&
                        taikyoku.MarkingBoard.CanDo_South(curLocation)
                    )
                    {
                        if
                        (
                            Util_FindOpen.FindOpen3Locations(try3Locations, curLocation.ToSouth(), color, liberty123OfPiece, taikyoku)
                            &&
                            try3Locations.Count == liberty123OfPiece
                        )
                        {
                            result = true;
                            goto gt_EndMethod;
                        }
                    }
                }
            }

            // 西ネイバーを調べます。
            if (curLocation.J != 0)
            {
                if
                (
                    ban.WestOf(curLocation) == StoneColor.Empty
                    &&
                    // 西隣が、取った石(コウかもしれない)でなければ。
                    taikyoku.MyKo.Is_WestOf(curLocation)
                )
                {
                    try3Locations.Add(curLocation.ToWest());
                    if (try3Locations.Count == liberty123OfPiece)
                    {
                        result = true;
                        goto gt_EndMethod;
                    }
                }
                else if
                (
                    ban.WestOf(curLocation) == color
                    &&
                    taikyoku.MarkingBoard.CanDo_West(curLocation)
                )
                {
                    if
                    (
                        Util_FindOpen.FindOpen3Locations(try3Locations, curLocation.ToWest(), color, liberty123OfPiece, taikyoku)
                        &&
                        try3Locations.Count == liberty123OfPiece
                    )
                    {
                        result = true;
                        goto gt_EndMethod;
                    }
                }
            }

            // 東ネイバーを調べます。
            if (!curLocation.IsEastEnd(taikyoku.GobanBounds))
            {
                if
                (
                    ban.EastOf(curLocation) == StoneColor.Empty
                    &&
                    // 東隣が、取った石(コウかもしれない)でなければ。
                    taikyoku.MyKo.Is_EastOf(curLocation)
                )
                {
                    try3Locations.Add(curLocation.ToEast());
                    if (try3Locations.Count == liberty123OfPiece)
                    {
                        result = true;
                        goto gt_EndMethod;
                    }
                }
                else if
                (
                    ban.EastOf(curLocation) == color
                    &&
                    taikyoku.MarkingBoard.CanDo_East(curLocation)
                )
                {
                    if
                    (
                        Util_FindOpen.FindOpen3Locations(try3Locations, curLocation.ToEast(), color, liberty123OfPiece, taikyoku)
                        &&
                        try3Locations.Count == liberty123OfPiece
                    )
                    {
                        result = true;
                        goto gt_EndMethod;
                    }
                }
            }

            // 開いているポイントを見つけるのに失敗したら
            result = false;

gt_EndMethod:
            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 弱っている石を助ける手を探します。
        /// 助けるというのは、(襲われそうな)自分の石の 横にツケることです。
        ///
        /// 位置m,nを含むグループから、
        /// 上下左右に隣接する石(または連)の呼吸点を調べ、
        /// 最もスコアの高い石(または連)の場所 i,j と、評価値を探します。(連の場合、どこか1つの石の場所)
        ///
        /// Gnugo1.2 では findnextmove 関数。
        /// </summary>
        /// <param name="out_bestLocation">次の動きの   行、列番号</param>
        /// <param name="out_bestScore">Gnugo1.2では val という名前のポインター変数。次の指し手の評価値</param>
        /// <param name="myStone_location">(脅かされているかもしれない)コンピューターの石の 行番号 m、列番号 n</param>
        /// <param name="currentLiberty">現在のリバティーの数。1〜3を、脅かされていると考えます。Gnugo1.2 では minlib 変数。</param>
        /// <param name="taikyoku"></param>
        /// <returns></returns>
        public static bool FindStone_LibertyWeak
        (
            out GobanPoint out_bestLocation,
            out int out_bestScore,
            GobanPoint myStone_location,
            int currentLiberty,
            Taikyoku taikyoku
        )
        {
            out_bestLocation = new GobanPointImpl(-1, -1);
            out_bestScore    = -1;

            // カレント位置をマークします。
            taikyoku.MarkingBoard.Done_Current(myStone_location);

            //
            // 東西南北のどこかに 空きスペース (がないと助けられません)があるはずです。
            //

            //--------------------------------------------------------------------------
            // 北隣を調べます。
            {
                GobanPoint tryLocation = new GobanPointImpl(0, 0); // Gnugo1.2 では ti,tj という変数名。// 初期値は 2015-11-26 追加
                int        tryScore    = 0;                        // Gnugo1.2 では tval 変数。 隣位置の評価値。    // 2015-11-26 追加 0 で初期化。
                bool       found       = false;

                if (!myStone_location.IsNorthEnd()) // 北端でない石のみ。
                {
                    if (taikyoku.Goban.NorthOf(myStone_location) == StoneColor.Empty)
                    {
                        // わたしの石の北隣にある空きスペースの位置。
                        tryLocation.SetLocation(myStone_location.ToNorth());

                        // 空きスペースに石を置いたと考えて、石を置いた局面のその自分の石(または連)の呼吸点を数えます。
                        int futureLiberty; // Gnugo1.2 では、グローバル変数 lib = 0 でした。
                        Util_CountLiberty.Count(out futureLiberty, tryLocation, taikyoku.MyColor, taikyoku);

                        // 評価値計算
                        tryScore = Util_SasiteNext.Evaluate_LibertyWeak(futureLiberty, currentLiberty);

                        found = true;
                    }
                    else if
                    (
                        taikyoku.Goban.NorthOf(myStone_location) == taikyoku.MyColor // 北隣がコンピューターの石で、
                        &&
                        taikyoku.MarkingBoard.CanDo_North(myStone_location)          // 北隣のマーキングが 0 なら
                    )
                    {
                        if (Util_SasiteNext.FindStone_LibertyWeak(out tryLocation, out tryScore, myStone_location.ToNorth(), currentLiberty, taikyoku))    // 再帰的に検索
                        {
                            found = true;
                        }
                    }
                }

                if (found)  // 見つかったら1
                {
                    found = false;
                    if (out_bestScore < tryScore && !Util_OwnEye.IsThis(tryLocation, taikyoku))
                    {
                        out_bestScore = tryScore;                  // 高い方の評価値を残している?
                        out_bestLocation.SetLocation(tryLocation); // 高い方の交点i,jを残している?
                    }
                }
            }

            //--------------------------------------------------------------------------
            // 南ネイバーを調べます。
            {
                GobanPoint tryLocation = new GobanPointImpl(0, 0); // Gnugo1.2 では ti,tj という変数名。// 初期値は 2015-11-26 追加
                int        tryScore    = 0;                        // Gnugo1.2 では tval 変数。 隣位置の評価値。    // 2015-11-26 追加 0 で初期化。
                bool       found       = false;

                if (!myStone_location.IsSouthEnd(taikyoku.GobanBounds))    // 南端でなければ。
                {
                    if (taikyoku.Goban.SouthOf(myStone_location) == StoneColor.Empty)
                    {
                        // 南隣の石(または連)の呼吸点の数を調べ、
                        // 期待する呼吸点の数より 大きいほど高い評価値を付けます。
                        tryLocation.SetLocation(myStone_location.ToSouth());

                        int futureLiberty; // Gnugo1.2 では、グローバル変数 lib = 0 でした。
                        Util_CountLiberty.Count(out futureLiberty, tryLocation, taikyoku.MyColor, taikyoku);
                        tryScore = Util_SasiteNext.Evaluate_LibertyWeak(futureLiberty, currentLiberty);
                        found    = true;
                    }
                    else if
                    (
                        taikyoku.Goban.SouthOf(myStone_location) == taikyoku.MyColor
                        &&
                        taikyoku.MarkingBoard.CanDo_South(myStone_location) // 南側
                    )
                    {
                        if (Util_SasiteNext.FindStone_LibertyWeak(out tryLocation, out tryScore, myStone_location.ToSouth(), currentLiberty, taikyoku))
                        {
                            found = true;
                        }
                    }
                }

                if (found)  // 見つかったら1
                {
                    found = false;
                    if (out_bestScore < tryScore && !Util_OwnEye.IsThis(tryLocation, taikyoku))
                    {
                        out_bestScore = tryScore;
                        out_bestLocation.SetLocation(tryLocation);
                    }
                }
            }

            //--------------------------------------------------------------------------
            // 西ネイバーを調べます。
            {
                GobanPoint tryLocation = new GobanPointImpl(0, 0); // Gnugo1.2 では ti,tj という変数名。// 初期値は 2015-11-26 追加
                int        tryScore    = 0;                        // Gnugo1.2 では tval 変数。 隣位置の評価値。    // 2015-11-26 追加 0 で初期化。
                bool       found       = false;

                if (!myStone_location.IsWestEnd()) // 西端でなければ。
                {
                    if (taikyoku.Goban.WestOf(myStone_location) == StoneColor.Empty)
                    {
                        tryLocation.SetLocation(myStone_location.ToWest());

                        int futureLiberty; // Gnugo1.2 では、グローバル変数 lib = 0 でした。
                        Util_CountLiberty.Count(out futureLiberty, tryLocation, taikyoku.MyColor, taikyoku);
                        tryScore = Util_SasiteNext.Evaluate_LibertyWeak(futureLiberty, currentLiberty);
                        found    = true;
                    }
                    else
                    {
                        if (
                            taikyoku.Goban.WestOf(myStone_location) == taikyoku.MyColor
                            &&
                            taikyoku.MarkingBoard.CanDo_West(myStone_location) // 西側
                            )
                        {
                            if (Util_SasiteNext.FindStone_LibertyWeak(out tryLocation, out tryScore, myStone_location.ToWest(), currentLiberty, taikyoku))
                            {
                                found = true;
                            }
                        }
                    }
                }

                if (found)  // 見つかっていれば 1
                {
                    found = false;
                    if (tryScore > out_bestScore && !Util_OwnEye.IsThis(tryLocation, taikyoku))
                    {
                        out_bestScore = tryScore;
                        out_bestLocation.SetLocation(tryLocation);
                    }
                }
            }

            //--------------------------------------------------------------------------
            // 東ネイバーを調べます。
            {
                GobanPoint tryLocation = new GobanPointImpl(0, 0); // Gnugo1.2 では ti,tj という変数名。// 初期値は 2015-11-26 追加
                int        tryScore    = 0;                        // Gnugo1.2 では tval 変数。 隣位置の評価値。    // 2015-11-26 追加 0 で初期化。
                bool       found       = false;

                if (!myStone_location.IsEastEnd(taikyoku.GobanBounds))    // 東端でなければ。
                {
                    if (taikyoku.Goban.EastOf(myStone_location) == StoneColor.Empty)
                    {
                        tryLocation.SetLocation(myStone_location.ToEast());

                        int futureLiberty; // Gnugo1.2 では、グローバル変数 lib = 0 でした。
                        Util_CountLiberty.Count(out futureLiberty, tryLocation, taikyoku.MyColor, taikyoku);
                        tryScore = Util_SasiteNext.Evaluate_LibertyWeak(futureLiberty, currentLiberty);
                        found    = true;
                    }
                    else
                    {
                        if
                        (
                            taikyoku.Goban.EastOf(myStone_location) == taikyoku.MyColor
                            &&
                            taikyoku.MarkingBoard.CanDo_East(myStone_location) // 東側
                        )
                        {
                            if (Util_SasiteNext.FindStone_LibertyWeak(out tryLocation, out tryScore, myStone_location.ToEast(), currentLiberty, taikyoku))
                            {
                                found = true;
                            }
                        }
                    }
                }

                if (found)  // Gnugo1.2では、見つかっていれば 1 でした。
                {
                    found = false;
                    if (out_bestScore < tryScore && !Util_OwnEye.IsThis(tryLocation, taikyoku))
                    {
                        out_bestScore = tryScore;
                        out_bestLocation.SetLocation(tryLocation);
                    }
                }
            }

            //--------------------------------------------------------------------------
            if (0 < out_bestScore)   // 次の動きを見つけた。
            {
                return(true);
            }
            else    // 次の動きは失敗。
            {
                return(false);
            }
        }
        /// <summary>
        /// 碁盤の指定の交点 i,j にある石を起点に、つながっている同じ色の石の(1つ、または連の)総リバーティを数えます。
        /// 再帰的に呼び出されます。
        /// Countlib関数から呼び出してください。
        ///
        /// Gnugo1.2 では、count関数です。
        /// </summary>
        /// <param name="count">Gnugo1.2 では、グローバル変数 lib でした。</param>
        /// <param name="location">Gnugo1.2では、行番号 i = 0〜18、列番号 j = 0〜18。</param>
        /// <param name="color">黒 or 白</param>
        /// <param name="taikyoku"></param>
        private static void Count_Recursive(
            ref int count,
            GobanPoint location,
            StoneColor color,
            Taikyoku taikyoku
            )
        {
            //----------------------------------------
            // 実装の解説
            //----------------------------------------
            // 石の周り4方向について、
            // 数えていない空き交点であれば リバティーを 1加算します。
            // 数えていなくて、指定した色(主に同じ色)の石であれば、その石から同メソッドを再帰呼び出しします。
            //

            // 指定した位置は、調査済みとしてマークします。
            taikyoku.CountedBoard.Done_Current(location);

            // 北隣の石を調べます。
            if (!location.IsNorthEnd())//北端でなければ
            {
                if
                (
                    taikyoku.Goban.NorthOf(location) == StoneColor.Empty
                    &&
                    taikyoku.CountedBoard.CanDo_North(location)
                )
                {
                    // 北隣が空いていて  まだ数えていないなら、
                    // リバティーを1つ数え上げます。次からは重複して数えません。
                    ++count;
                    taikyoku.CountedBoard.Done_North(location);
                }
                else if
                (
                    taikyoku.Goban.NorthOf(location) == color
                    &&
                    taikyoku.CountedBoard.CanDo_North(location)
                )
                {
                    // 北隣に 指定色の石が置いてあり、まだ数えていないなら、
                    // その石からさらにカウントを続けます。
                    Util_CountLiberty.Count_Recursive(ref count, location.ToNorth(), color, taikyoku);
                }
                // 指定した色でない石が置いてあれば何もしない。
            }

            // 南隣を調べます。
            if (!location.IsSouthEnd(taikyoku.GobanBounds))//南端でなければ
            {
                // もう、だいたい分かるだろう☆(^▽^)
                if
                (
                    taikyoku.Goban.SouthOf(location) == StoneColor.Empty
                    &&
                    taikyoku.CountedBoard.CanDo_South(location)
                )
                {
                    ++count;
                    taikyoku.CountedBoard.Done_South(location);
                }
                else if
                (
                    taikyoku.Goban.SouthOf(location) == color
                    &&
                    taikyoku.CountedBoard.CanDo_South(location)
                )
                {
                    Util_CountLiberty.Count_Recursive(ref count, location.ToSouth(), color, taikyoku);
                }
            }

            // 西隣を調べます。
            if (!location.IsWestEnd())//西端でなければ
            {
                if
                (
                    taikyoku.Goban.WestOf(location) == StoneColor.Empty
                    &&
                    taikyoku.CountedBoard.CanDo_West(location)
                )
                {
                    ++count;
                    taikyoku.CountedBoard.Done_West(location);
                }
                else if
                (
                    taikyoku.Goban.WestOf(location) == color
                    &&
                    taikyoku.CountedBoard.CanDo_West(location)
                )
                {
                    Util_CountLiberty.Count_Recursive(ref count, location.ToWest(), color, taikyoku);
                }
            }

            // 東隣を調べます。
            if (!location.IsEastEnd(taikyoku.GobanBounds))//東端でなければ
            {
                if
                (
                    (taikyoku.Goban.EastOf(location) == StoneColor.Empty)
                    &&
                    taikyoku.CountedBoard.CanDo_East(location)
                )
                {
                    ++count;
                    taikyoku.CountedBoard.Done_East(location);
                }
                else if
                (
                    taikyoku.Goban.EastOf(location) == color
                    &&
                    taikyoku.CountedBoard.CanDo_East(location)
                )
                {
                    Util_CountLiberty.Count_Recursive(ref count, location.ToEast(), color, taikyoku);
                }
            }
        }