///<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); }
/// <summary> /// 给出哈希树的叶子节点,并进行剪枝操作,黑色棋子作为AI时 /// </summary> /// <param name="Di_tree_Content">哈希树字典内容</param> /// <param name="leaf_node">叶子节点哈希表</param> public void Search_Leaf_Node (ref Dictionary <Guid, Struct_Simple_State> Di_tree_Content, Dictionary <Guid, Struct_Simple_State> leaf_node) { bool isChildNode = true; while (isChildNode) { //用于计算父节点的分值,进行比较选出最大或最小的分数情况 Dictionary <Guid, int> di_temp_score = new Dictionary <Guid, int>(); //id临时值,避免频繁声明 Guid temp_guid; Struct_Simple_State temp_simple_struct = new Struct_Simple_State(); //循环叶节点,改变父节点临时节点字典的分值 foreach (Guid key in leaf_node.Keys) { //获取父节点id temp_guid = leaf_node[key].parent_id; //如果父节点是根节点则停止搜索 if (temp_guid == new Guid()) { isChildNode = false; //Di_tree_Content = leaf_node; break; } if (!di_temp_score.ContainsKey(temp_guid)) { di_temp_score.Add(temp_guid, leaf_node[key].score); } else { if (di_temp_score[temp_guid] < leaf_node[key].score) { di_temp_score[temp_guid] = leaf_node[key].score; } } //删除哈希树的节点 Di_tree_Content.Remove(key); } leaf_node.Clear(); if (isChildNode == true) { //父节点和与临时节点进行分值相减,对父节点分值进行重新赋值,再将叶节点进行更新 foreach (Guid key in di_temp_score.Keys) { temp_simple_struct = Di_tree_Content[key]; temp_simple_struct.score = Di_tree_Content[key].score - di_temp_score[key]; Di_tree_Content[key] = temp_simple_struct; leaf_node.Add(key, temp_simple_struct); } di_temp_score.Clear(); } } }
/// <summary> /// 棋力评估函数 /// </summary> /// <param name="state">棋子状态</param> /// <param name="di_position_score">引用数据字典 棋局状态ID-棋子简单状态信息</param> public void Design(Struct_State state, ref Dictionary <Guid, Struct_Simple_State> di_position_score) { int category = state.category; //记录当前分数字段 int temp_score = 0; //Dictionary<int, int> di_position_score = new Dictionary<int, int>(); //规则结构信息 Str_ChessInfo chessinfo = new Str_ChessInfo(); //简单记录结构信息,用于记录数据字典 Struct_Simple_State simple_state = new Struct_Simple_State(); //黑方走棋 if (state.isRed == false) { //边界判断值,在边界内除16余数在4-12之间 int judge_int = 0; for (int i = 52; i <= 204; i++) { judge_int = i % size; if (state.array_chess[i] >= 0 && judge_int >= 4 && judge_int <= 12) { chessinfo.category = category; chessinfo.origin_position = state.origin_position; chessinfo.target = i; if (ChessLoad.rule.Rule_Judge(ref state.array_chess, chessinfo)) { if (Math.Abs(chessinfo.category) >= 12 && Math.Abs(chessinfo.category) <= 16) { temp_score = ChessLoad.DiPawn_Black[chessinfo.target]; } if (state.array_chess[chessinfo.target] > 0) { temp_score = ChessLoad.DiScore[state.array_chess[chessinfo.target]]; } if (temp_score >= cut_size_score) { simple_state.target_position = chessinfo.target; simple_state.score = temp_score; simple_state.origin_position = chessinfo.origin_position; simple_state.id = Guid.NewGuid(); simple_state.category = chessinfo.category; simple_state.parent_id = state.id; di_position_score.Add(simple_state.id, simple_state); } } } } } //红方走棋 else { int judge_int = 0; for (int i = 52; i <= 204; i++) { //if (state.array_chess[i] <= 0) //{ judge_int = i % size; if (state.array_chess[i] <= 0 && judge_int >= 4 && judge_int <= 12) { chessinfo.category = category; chessinfo.origin_position = state.origin_position; chessinfo.target = i; if (ChessLoad.rule.Rule_Judge(ref state.array_chess, chessinfo)) { if (Math.Abs(chessinfo.category) >= 12 && Math.Abs(chessinfo.category) <= 16) { temp_score = ChessLoad.DiPawn_Red[chessinfo.target]; } if (state.array_chess[chessinfo.target] < 0) { temp_score = ChessLoad.DiScore[state.array_chess[chessinfo.target]]; } if (temp_score >= cut_size_score) { simple_state.target_position = chessinfo.target; simple_state.score = temp_score; simple_state.origin_position = chessinfo.origin_position; simple_state.id = Guid.NewGuid(); simple_state.category = chessinfo.category; simple_state.parent_id = state.id; di_position_score.Add(simple_state.id, simple_state); } } } } } }