目标:用WF+WPF完成24点游戏程序。
1.界面,新建一个WPF项目,4个Label+2个按钮,注意:4个Label分别设置Tag值为:0~3
- <Windowx:Class="Point24.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- Title="24点游戏"Height="204"Width="394"FontSize="12"ResizeMode="NoResize"WindowStartupLocation="CenterScreen">
- <GridName="gridMain">
- <LabelContent="0"Height="93"HorizontalAlignment="Left"Margin="12,12,0,0"Name="lblNum1"VerticalAlignment="Top"Width="82"FontSize="60"HorizontalContentAlignment="Center"VerticalContentAlignment="Center"Tag="0"/>
- <LabelContent="0"FontSize="60"Height="93"HorizontalAlignment="Left"HorizontalContentAlignment="Center"Margin="100,12,0,0"Name="lblNum2"VerticalAlignment="Top"VerticalContentAlignment="Center"Width="82"Tag="1"/>
- <LabelContent="0"FontSize="60"Height="93"HorizontalAlignment="Left"HorizontalContentAlignment="Center"Margin="188,12,0,0"Name="lblNum3"VerticalAlignment="Top"VerticalContentAlignment="Center"Width="82"Tag="2"/>
- <LabelContent="0"FontSize="60"Height="93"HorizontalAlignment="Left"HorizontalContentAlignment="Center"Margin="276,12,0,0"Name="lblNum4"VerticalAlignment="Top"VerticalContentAlignment="Center"Width="82"Tag="3"/>
- <ButtonContent="New_Game"Height="23"HorizontalAlignment="Left"Margin="71,130,0,0"Name="btnNewGame"VerticalAlignment="Top"Width="100"FontSize="12"Click="btnNewGame_Click"/>
- <ButtonContent="AutoCalculate"FontSize="12"Height="23"HorizontalAlignment="Left"Margin="197,130,0,0"Name="btnCalculate"VerticalAlignment="Top"Width="100"Click="btnCalculate_Click"/>
- </Grid>
- </Window>
2.创建“自动生成随机数的活动”,新建项->Workflow->活动,名称:CreateRandomNumberActivity.xaml
活动输入:无
活动输出:4个随机数,这里用List<int>集合


3.创建“自动计算的活动”,新建项->Workflow->代码活动,名称:CalculateActivity.cs
- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.Linq;
- usingSystem.Text;
- usingSystem.Activities;
- usingSystem.Data;
- namespacePoint24{
- publicsealedclassCalculateActivity:CodeActivity{
- publicInArgument<List<int>>Nums{get;set;}
- publicOutArgument<string>Result{get;set;}
- protectedoverridevoidExecute(CodeActivityContextcontext){
- List<int>nums=context.GetValue(Nums);
- stringresult=string.Empty;
- if(!Calculate(nums,refresult)){
- result="此题无解";
- }
- context.SetValue(Result,result);
- }
- privateboolCalculate(List<int>nums,refstringresult){
- DataTabledt=newDataTable();
- string[]opts={"+","-","*","/"};
- for(inta=0;a<4;a++){
- for(intb=0;b<4;b++){
- if(a==b)continue;
- for(intc=0;c<4;c++){
- if(a==c||b==c)continue;
- for(intd=0;d<4;d++){
- if(a==d||b==d||c==d)continue;
- for(inte=0;e<4;e++){
- for(intf=0;f<4;f++){
- for(intg=0;g<4;g++){
- try{
- stringstrExpression=string.Format("(({0}{4}{1}){5}{2}){6}{3}",
- nums[a],nums[b],nums[c],nums[d],
- opts[e],opts[f],opts[g]);
- doubler=Convert.ToDouble(dt.Compute(strExpression,null));
- if(r==24.0){
- result=strExpression;
- returntrue;
- }
- strExpression=string.Format("({0}{4}{1}){5}({2}{6}{3})",
- nums[a],nums[b],nums[c],nums[d],
- opts[e],opts[f],opts[g]);
- r=Convert.ToDouble(dt.Compute(strExpression,null));
- if(r==24.0){
- result=strExpression;
- returntrue;
- }
- }catch{}
- }
- }
- }
- }
- }
- }
- }
- returnfalse;
- }
- }
- }
活动输入:随机数集合List<int>
活动输出:计算结果String
简单讲解程序:首先创建一个表达式,然后用DataTable.Computer方法计算表达式的值是否是24,最终将结果返回。
其中前4个for用于生成随机数的排列,for中if用以判断一个数字不能重复使用;后面3个for用以生成表达式中任意三个操作符。然后计算:((数1?数2)?数3)?数4 ,第二种表达式模式是:(数1?数2)?(数3?数4),其中?表示某种操作符。
4.WPF中给按钮添加事件
- List<int>nums;
- privatevoidbtnNewGame_Click(objectsender,RoutedEventArgse){
- IDictionary<string,object>output=WorkflowInvoker.Invoke(newCreateRandomNumberActivity());
- nums=output["Nums"]asList<int>;
- /*
- *遍历子控件
- *注意:
- *1.设置Grid的Name
- *2.设置Label的Tag分别为0~3
- */
- intcount=VisualTreeHelper.GetChildrenCount(gridMain);
- for(inti=0;i<count;i++){
- objectctrl=VisualTreeHelper.GetChild(gridMain,i);
- if(ctrlisLabel){
- Labellbl=ctrlasLabel;
- if(lbl.Tag!=null){
- inttag=Convert.ToInt32(lbl.Tag);
- lbl.Content=nums[tag].ToString();
- }
- }
- }
- }
- privatevoidbtnCalculate_Click(objectsender,RoutedEventArgse){
- IDictionary<string,object>input=newDictionary<string,object>{
- {"Nums",nums}
- };
- IDictionary<string,object>output=WorkflowInvoker.Invoke(newCalculateActivity(),input);
- stringmessage=output["Result"]asstring;
- MessageBox.Show(message);
- }
最后看看效果图,还挺智能的!
下载地址:http://u.163.com/48Ozl
提取码:veziqgsa
2560

被折叠的 条评论
为什么被折叠?



