void bt_Click(object sender, RoutedEventArgs e) { Button bt = (Button)sender; if (SelectButton.Uid != "") { //棋子移动信息 Str_ChessInfo chessinfo = new Str_ChessInfo(); chessinfo.category = Convert.ToInt32(SelectButton.Uid); if ((chessinfo.category > 0 && IsRed == true) || (chessinfo.category < 0 && IsRed == false)) { Grid origin_grid = (Grid)this.GetType().GetField(SelectButton.Name, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.IgnoreCase).GetValue(this); Grid now_grid = (Grid)this.GetType().GetField(bt.Name, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.IgnoreCase).GetValue(this); int origin_Position = cast_Name(origin_grid.Name); int now_Position = cast_Name(now_grid.Name); chessinfo.origin_position = origin_Position; chessinfo.target = now_Position; if (rule.Rule_Judge(ref ChessLoad.Auxiliary_array, chessinfo)) { ChessLoad.Auxiliary_array[chessinfo.target] = chessinfo.category; ChessLoad.Auxiliary_array[chessinfo.origin_position] = 0; origin_grid.Children.Clear(); now_grid.Children.Clear(); SelectButton.Name = now_grid.Name; SelectButton.Uid = chessinfo.category.ToString(); now_grid.Children.Add(SelectButton); ChessAI ai = new ChessAI(score); ChessTree tree = new ChessTree(); tree.State = new Struct_State(); tree.State.array_chess = ChessLoad.Auxiliary_array; tree.State.isRed = false; tree.State = ai.Ai_Result(ref tree, depth); Button AiButton = new Button(); ChessLoad.Auxiliary_array[tree.State.target_position] = tree.State.category; ChessLoad.Auxiliary_array[tree.State.origin_position] = 0; origin_grid = (Grid)this.GetType().GetField("po_" + tree.State.origin_position, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.IgnoreCase).GetValue(this); now_grid = (Grid)this.GetType().GetField("po_" + tree.State.target_position, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.IgnoreCase).GetValue(this); AiButton = (Button)origin_grid.Children[0]; AiButton.Name = "po_" + tree.State.target_position; AiButton.Uid = tree.State.category.ToString(); //避免重复走棋 //if (ChessLoad.same_action_state.Count == 2) // ChessLoad.same_action_state.Dequeue(); //Struct_Simple_State simple_state = new Struct_Simple_State(); //simple_state.category = tree.State.category; //simple_state.origin_position = tree.State.origin_position; //simple_state.target_position = tree.State.target_position; //ChessLoad.same_action_state.Enqueue(simple_state); origin_grid.Children.Clear(); now_grid.Children.Clear(); now_grid.Children.Add(AiButton); } else { SelectButton = bt; } } } SelectButton = bt; }
private void grid3_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { Str_ChessInfo chessinfo = new Str_ChessInfo(); //棋子移动信息 Grid now_grid = (Grid)sender; //当前的Grid值 if (SelectButton.Uid != "") { //通过反射获取选中棋子的信息 Grid origin_grid = (Grid)this.GetType().GetField(SelectButton.Name, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.IgnoreCase).GetValue(this); int origin_Position = cast_Name(origin_grid.Name); int now_Position = cast_Name(now_grid.Name); chessinfo.category = Convert.ToInt32(SelectButton.Uid); if ((chessinfo.category < 0 && IsRed == false) || chessinfo.category > 0 && IsRed == true) { chessinfo.origin_position = origin_Position; chessinfo.target = now_Position; //棋盘模块判断 if (rule.Rule_Judge(ref ChessLoad.Auxiliary_array, chessinfo)) { ChessLoad.Auxiliary_array[chessinfo.target] = chessinfo.category; ChessLoad.Auxiliary_array[chessinfo.origin_position] = 0; origin_grid.Children.Clear(); SelectButton.Name = now_grid.Name; now_grid.Children.Add(SelectButton); ChessAI ai = new ChessAI(score); ChessTree tree = new ChessTree(); tree.State = new Struct_State(); tree.State.array_chess = new int[ChessLoad.Auxiliary_array.Length]; ChessLoad.Auxiliary_array.CopyTo(tree.State.array_chess, 0); tree.State.isRed = false; tree.State = ai.Ai_Result(ref tree, depth); ChessLoad.Auxiliary_array[tree.State.target_position] = tree.State.category; ChessLoad.Auxiliary_array[tree.State.origin_position] = 0; Button AiButton = new Button(); origin_grid = (Grid)this.GetType().GetField("po_" + tree.State.origin_position, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.IgnoreCase).GetValue(this); now_grid = (Grid)this.GetType().GetField("po_" + tree.State.target_position, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.IgnoreCase).GetValue(this); AiButton = (Button)origin_grid.Children[0]; AiButton.Name = "po_" + tree.State.target_position; AiButton.Uid = tree.State.category.ToString(); //避免重复走棋 //if(ChessLoad.same_action_state.Count==2) //ChessLoad.same_action_state.Dequeue(); //Struct_Simple_State simple_state = new Struct_Simple_State(); //simple_state.category = tree.State.category; //simple_state.origin_position = tree.State.origin_position; //simple_state.target_position = tree.State.target_position; //ChessLoad.same_action_state.Enqueue(simple_state); origin_grid.Children.Clear(); now_grid.Children.Clear(); now_grid.Children.Add(AiButton); } } } }
/// <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); } } } } } }