Exemplo n.º 1
0
        public static int ComputerVSComputer(EvaluateFunction fun1, EvaluateFunction fun2)
        {
            // 经过指定步数则为和棋
            int        step   = Settings.DRAW_STEPS;
            GameState  gs     = new GameState();
            ChessPiece winner = ChessPiece.EMPTY;

            while (step-- != 0)
            {
                winner = gs.WhoIsWin();
                if (winner == ChessPiece.EMPTY)
                {
                    if (step == Settings.DRAW_STEPS - 1)
                    {
                        gs.PlaceChessInCenter();

                        Debug.WriteLine("{0} {1} {2}", gs.X, gs.Y, GameState.AnotherPlayer(gs.turn).ToString());
                    }
                    else if (step % 2 == 1)
                    {
                        PlaceChessAI(gs, fun1);
                        Debug.WriteLine("{0} {1} {2}", gs.X, gs.Y, GameState.AnotherPlayer(gs.turn).ToString());
                    }
                    else if (step % 2 == 0)
                    {
                        PlaceChessAI(gs, fun2);
                        Debug.WriteLine("{0} {1} {2}", gs.X, gs.Y, GameState.AnotherPlayer(gs.turn).ToString());
                    }
                }
                else if (winner == ChessPiece.CROSS)
                {
                    return(Settings.WIN_FITNESS);
                }
                else if (winner == ChessPiece.NOUGHT)
                {
                    return(Settings.LOSE_FITNESS);
                }
            }
            return(Settings.DRAW_FITNESS);
        }
Exemplo n.º 2
0
        /// <summary>
        /// MIN_MAX博弈的核心
        /// </summary>
        /// <param name="gs">游戏状态</param>
        /// <param name="depth">搜素深度</param>
        /// <param name="min">允许的最小值</param>
        /// <param name="max">允许的最大值</param>
        /// <param name="cp">评判的是哪一方</param>
        /// <param name="ef">可调整的评价函数</param>
        /// <returns></returns>
        public static double MinMax(GameState gs, int depth, double min, double max, ChessPiece cp, EvaluateFunction ef)
        {
            ChessPiece winner = gs.WhoIsWin();

            if (winner == cp)
            {
                return(Settings.WIN_POINT);
            }
            else if (winner == GameState.AnotherPlayer(cp))
            {
                return(Settings.LOSE_POINT);
            }

            if (depth == 0)
            {
                return(ef(gs));
            }

            double value, temp_value;

            // 当前为极大节点
            if (gs.NextPlayer() == cp)
            {
                // 检查其所有子节点
                value = min;
                IEnumerator <GameState> ie_gs = gs.GetNextSiblingStates();
                while (ie_gs.MoveNext())
                {
                    if (IsNessarySearch(ie_gs.Current, ie_gs.Current.X, ie_gs.Current.Y))
                    {
                        temp_value = MinMax(ie_gs.Current, depth - 1, value, max, cp, ef);

                        // alpha pruning
                        if (temp_value > value)
                        {
                            value = temp_value;
                        }
                        if (value > max)
                        {
                            return(max);
                        }
                    }
                }
                return(value);
            }
            // 当前为极小节点
            else
            {
                // 检查其所有子节点
                value = max;
                IEnumerator <GameState> ie_gs = gs.GetNextSiblingStates();
                while (ie_gs.MoveNext())
                {
                    if (IsNessarySearch(ie_gs.Current, ie_gs.Current.X,
                                        ie_gs.Current.Y))
                    {
                        // beta pruning
                        temp_value = MinMax(ie_gs.Current, depth - 1, min, value, cp, ef);
                        if (temp_value < value)
                        {
                            value = temp_value;
                        }
                        if (value < min)
                        {
                            return(min);
                        }
                    }
                }
                return(value);
            }
        }