Пример #1
0
 /// <summary>
 /// 反向传播更新节点信息(回溯)
 /// </summary>
 /// <param name="Leaf_Value">子节点Q值</param>
 public void BackPropagation(double Leaf_Value)
 {
     if (FatherNode != null)//非根节点
     {
         FatherNode.BackPropagation(-Leaf_Value);
     }
     UpdateInfo(Leaf_Value);
 }
Пример #2
0
        /// <summary>
        /// 进行一次模拟(Simluation)
        /// </summary>
        /// <param name="InitChessBoard">当前决策节点局面</param>
        /// <param name="RootNode">根节点</param>
        public static void SimluationOnce(ChessBoard InitChessBoard, MonteCartoTreeNode RootNode)
        {
            #region 暂存挡板数量
            int Board1Save = InitChessBoard.NumPlayer1Board;
            int Board2Save = InitChessBoard.NumPlayer2Board;
            #endregion

            if (RootNode.SonNode.Count == 0)               //初始根节点
            {
                RootNode.Expand(InitChessBoard, RootNode); //先拓展一次
            }

            ChessBoard SimluationChessBoard = new ChessBoard();
            ChessBoard.SaveChessBoard(ref SimluationChessBoard, InitChessBoard);//相当于拷贝了
            MonteCartoTreeNode NextExpandNode = RootNode;
            while (true)
            {
                #region 提前终止局面检测
                if (NextExpandNode.SonNode.Count == 1)
                {
                    if (NextExpandNode.SonNode[0].IfWin)
                    {
                        double leaf_value = -1;
                        if (JudgePlayer != NextExpandNode.SonNode[0].NodePlayer)
                        {
                            leaf_value = 1;
                        }
                        NextExpandNode.BackPropagation(leaf_value);
                        break;
                    }
                }
                #endregion
                /*选择*/
                NextExpandNode = Select(NextExpandNode);

                #region 模拟落子
                string Hint = NowQuoridor.QuoridorRule.Action(ref SimluationChessBoard
                                                              , NextExpandNode.ActionLocation.X, NextExpandNode.ActionLocation.Y, NextExpandNode.NodeAction);
                try
                {
                    if (Hint != "OK")
                    {
                        Exception e = new Exception();
                    }
                }
                catch (Exception)
                {
                    throw;
                }

                if (NextExpandNode.NodePlayer == EnumNowPlayer.Player1)
                {
                    if (NextExpandNode.NodeAction == NowAction.Action_PlaceVerticalBoard ||
                        NextExpandNode.NodeAction == NowAction.Action_PlaceHorizontalBoard)
                    {
                        SimluationChessBoard.NumPlayer1Board -= 2;
                    }
                }
                else if (NextExpandNode.NodePlayer == EnumNowPlayer.Player2)
                {
                    if (NextExpandNode.NodeAction == NowAction.Action_PlaceVerticalBoard ||
                        NextExpandNode.NodeAction == NowAction.Action_PlaceHorizontalBoard)
                    {
                        SimluationChessBoard.NumPlayer2Board -= 2;
                    }
                }
                #endregion

                //SimluationChessBoard.DrawNowChessBoard(ref Form1.Gr, Form1.form1.ChessWhitePB, Form1.form1.ChessBlackPB);
                //Form1.form1.ChessBoardPB.Refresh();
                //System.Threading.Thread.Sleep(500);
                string SucessHint = RuleEngine.CheckResult(SimluationChessBoard);
                if (SucessHint != "No success")//搜索到胜利节点了
                {
                    double leaf_value = -1;
                    if (JudgePlayer == EnumNowPlayer.Player1 && SucessHint == "Player1 Success!")
                    {
                        leaf_value = 1;
                    }
                    if (JudgePlayer == EnumNowPlayer.Player2 && SucessHint == "Player2 Success!")
                    {
                        leaf_value = 1;
                    }

                    NextExpandNode.BackPropagation(leaf_value);
                    break;
                }

                double dis_player1 = RuleEngine.AstarEngine.AstarRestart(SimluationChessBoard, EnumNowPlayer.Player1
                                                                         , SimluationChessBoard.Player1Location.X, SimluationChessBoard.Player1Location.Y);
                double dis_player2 = RuleEngine.AstarEngine.AstarRestart(SimluationChessBoard, EnumNowPlayer.Player2
                                                                         , SimluationChessBoard.Player2Location.X, SimluationChessBoard.Player2Location.Y);

                EnumNowPlayer Winner = EnumNowPlayer.Player1;
                # region 必赢必输局面检测
                //if (dis_player1 >= 14 || dis_player2 >= 14)//某人步数过大
                //{
                //    if (dis_player2 - dis_player1 >= 5)
                //    {
                //        Winner = EnumNowPlayer.Player2;
                //    }
                //}
                //else if (SimluationChessBoard.NumPlayer2Board == 0 && SimluationChessBoard.NumPlayer1Board == 0)//挡板已用完
                //{
                //    #region 是否存在跳棋检测(未写)
                //    #endregion
                //    if (dis_player2 - dis_player1 > 0)
                //    {
                //        Winner = EnumNowPlayer.Player2;
                //    }
                //}

                //double leaf_value2 = -1;
                //if (JudgePlayer == EnumNowPlayer.Player1 && Winner == EnumNowPlayer.Player1)//下一步是P2走
                //{
                //    leaf_value2 = 1;
                //}
                //if (JudgePlayer == EnumNowPlayer.Player2 && Winner == EnumNowPlayer.Player2)//下一步是P1走
                //{
                //    leaf_value2 = 1;
                //}
                //NextExpandNode.BackPropagation(leaf_value2);

                #endregion

                /*拓展*/
                NextExpandNode.Expand(SimluationChessBoard, NextExpandNode);
            }