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); } } } }