/// <summary> /// AI处理结果 /// </summary> /// <param name="chessTree">棋子树</param> /// <param name="depth">搜索的深度</param> /// <returns></returns> public Struct_State Ai_Result(ref ChessTree chessTree, int depth) { ChessTree child_tree = chessTree; Depth_Iteration(ref chessTree, depth); return(chessTree.Max_Node_Search(chessTree)); }
public void Depth_Iteration(ref ChessTree chessTree, int depth) { ChessTree child_tree = chessTree; ChessTree temp_tree = new ChessTree(); if (depth > 0) { --depth; Extend_Depth(child_tree); //if ((depth == ChessLoad.Depth - 2)&&(ChessLoad.same_action_state.Count==2)) //{ // if ((child_tree.State.category == ChessLoad.same_action_state.Peek().category) && // (child_tree.State.origin_position == ChessLoad.same_action_state.Peek().origin_position) && // (child_tree.State.target_position == ChessLoad.same_action_state.Peek().target_position)) { } // else // { // for (int i = 0; i < child_tree.ChessNodeList.Count; i++) // { // temp_tree = child_tree.ChessNodeList[i]; // Depth_Iteration(ref temp_tree, depth); // } // } //} //else for (int i = 0; i < child_tree.ChessNodeList.Count; i++) { temp_tree = child_tree.ChessNodeList[i]; Depth_Iteration(ref temp_tree, depth); } } }
///<summary> ///深度搜索遍历方法 ///</summary> ///<param name="Di_tree_content"></param> ///<param name="tree"></param> private void Dps_search(ref Dictionary <Guid, Struct_Simple_State> Di_tree_content, ChessTree tree) { Stack <ChessTree> stack_tree = new Stack <ChessTree>(); Dictionary <Guid, Struct_Simple_State> leaf_node = new Dictionary <Guid, Struct_Simple_State>(); stack_tree.Push(tree); ChessTree temp_tree = new ChessTree(); Struct_Simple_State simple_state = new Struct_Simple_State(); while (stack_tree.Count != 0) { temp_tree = stack_tree.Pop(); simple_state.id = temp_tree.State.id; simple_state.origin_position = temp_tree.State.origin_position; simple_state.parent_id = temp_tree.State.parent_id; simple_state.target_position = temp_tree.State.target_position; simple_state.score = temp_tree.State.score; simple_state.category = temp_tree.State.category; Di_tree_content.Add(simple_state.id, simple_state); if (temp_tree.ChessNodeList.Count == 0) { leaf_node.Add(temp_tree.State.id, simple_state); } for (int counts = 0; counts < temp_tree.ChessNodeList.Count; ++counts) { stack_tree.Push(temp_tree.ChessNodeList[counts]); } } Search_Leaf_Node(ref Di_tree_content, leaf_node); }
public Struct_State Max_Node_Search(ChessTree tree) { Dictionary <Guid, Struct_Simple_State> Di_All_Node = new Dictionary <Guid, Struct_Simple_State>(); //深度搜索获得所有节点 Dps_search(ref Di_All_Node, tree); return(Select_Score_Max(Di_All_Node)); }
/// <summary> /// 扩展深度 /// </summary> /// <param name="chessTree">搜索节点</param> public void Extend_Depth(ChessTree chessTree) { ChessTree temp_Tree = new ChessTree(); Score_Design design = new Score_Design(cut_size); Dictionary <Guid, Struct_Simple_State> di_score = new Dictionary <Guid, Struct_Simple_State>(); //遍历棋盘上的棋子 for (int counts = 52; counts <= 204; counts++) { if (chessTree.State.isRed == false) { if (chessTree.State.array_chess[counts] < 0) { temp_Tree.State.category = chessTree.State.array_chess[counts]; temp_Tree.State.origin_position = counts; temp_Tree.State.isRed = false; temp_Tree.State.array_chess = chessTree.State.array_chess; temp_Tree.State.id = chessTree.State.id; //获得走位点和分值 design.Design(temp_Tree.State, ref di_score); } } else { if (chessTree.State.array_chess[counts] > 0) { temp_Tree.State.category = chessTree.State.array_chess[counts]; temp_Tree.State.origin_position = counts; temp_Tree.State.isRed = true; temp_Tree.State.array_chess = chessTree.State.array_chess; temp_Tree.State.id = chessTree.State.id; //获得走位点和分值 design.Design(temp_Tree.State, ref di_score); } } } foreach (Guid key in di_score.Keys) { ChessTree child = new ChessTree(); child.State.array_chess = new int[chessTree.State.array_chess.Length]; child.State.category = di_score[key].category; child.State.origin_position = di_score[key].origin_position; chessTree.State.array_chess.CopyTo(child.State.array_chess, 0); child.State.target_position = di_score[key].target_position; child.State.score = di_score[key].score; child.State.isRed = ChangeBool(chessTree.State.isRed); child.State.array_chess[child.State.origin_position] = 0; child.State.array_chess[child.State.target_position] = child.State.category; child.State.id = key; child.State.parent_id = di_score[key].parent_id; chessTree.AddChild(child, chessTree); } di_score.Clear(); }
/// <summary> /// 需要修改的孩子节点 /// </summary> /// <param name="new_state">修改后的状态</param> /// <param name="alter_tree">需要修改的节点</param> /// <returns></returns> public bool AlterChild(Struct_State new_state, ChessTree alter_tree) { if (new_state.category != 0) { alter_tree.State = new_state;//更新节点信息 return(true); } else { return(false); } }
//public Guid ParentNodeId; //public Guid Id; /// <summary> /// 节点添加 /// </summary> /// <param name="ChildTree">子节点</param> /// <param name="tree">父节点</param> /// <returns></returns> public bool AddChild(ChessTree ChildTree, ChessTree tree) { if (ChildTree.State.category != 0) { tree.ChessNodeList.Add(ChildTree); return(true); } else { return(false); } }
/// <summary> /// 删除子节点 /// </summary> /// <param name="child_tree">需要删除的孩子节点</param> /// <returns></returns> public bool DeleteChild(ChessTree child_tree) { bool isExist = false; for (int count = 0; count < ChessNodeList.Count; count++) { if (ChessNodeList[count] == child_tree) { isExist = true; ChessNodeList.RemoveAt(count); break; } } return(isExist); }