WF4学习-24点游戏

目标:用WF+WPF完成24点游戏程序。

1.界面,新建一个WPF项目,4个Label+2个按钮,注意:4个Label分别设置Tag值为:0~3

Code:
  1. <Window x:Class="Point24.MainWindow"  
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.         Title="24点游戏" Height="204" Width="394" FontSize="12" ResizeMode="NoResize" WindowStartupLocation="CenterScreen">   
  5.     <Grid Name="gridMain">   
  6.         <Label Content="0" Height="93" HorizontalAlignment="Left" Margin="12,12,0,0" Name="lblNum1" VerticalAlignment="Top" Width="82" FontSize="60" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Tag="0" />   
  7.         <Label Content="0" FontSize="60" Height="93" HorizontalAlignment="Left" HorizontalContentAlignment="Center" Margin="100,12,0,0" Name="lblNum2" VerticalAlignment="Top" VerticalContentAlignment="Center" Width="82" Tag="1" />   
  8.         <Label Content="0" FontSize="60" Height="93" HorizontalAlignment="Left" HorizontalContentAlignment="Center" Margin="188,12,0,0" Name="lblNum3" VerticalAlignment="Top" VerticalContentAlignment="Center" Width="82" Tag="2" />   
  9.         <Label Content="0" FontSize="60" Height="93" HorizontalAlignment="Left" HorizontalContentAlignment="Center" Margin="276,12,0,0" Name="lblNum4" VerticalAlignment="Top" VerticalContentAlignment="Center" Width="82" Tag="3" />   
  10.         <Button Content="New _Game" Height="23" HorizontalAlignment="Left" Margin="71,130,0,0" Name="btnNewGame" VerticalAlignment="Top" Width="100" FontSize="12" Click="btnNewGame_Click" />   
  11.         <Button Content="Auto Calculate" FontSize="12" Height="23" HorizontalAlignment="Left" Margin="197,130,0,0" Name="btnCalculate" VerticalAlignment="Top" Width="100" Click="btnCalculate_Click" />   
  12.     </Grid>   
  13. </Window>   

2.创建“自动生成随机数的活动”,新建项->Workflow->活动,名称:CreateRandomNumberActivity.xaml

活动输入:无
活动输出:4个随机数,这里用List<int>集合



3.创建“自动计算的活动”,新建项->Workflow->代码活动,名称:CalculateActivity.cs

Code:
  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.Linq;   
  4. using System.Text;   
  5. using System.Activities;   
  6. using System.Data;   
  7.   
  8. namespace Point24 {   
  9.   
  10.     public sealed class CalculateActivity : CodeActivity {   
  11.   
  12.         public InArgument<List<int>> Nums { getset; }   
  13.         public OutArgument<string> Result { getset; }   
  14.   
  15.         protected override void Execute(CodeActivityContext context) {   
  16.             List<int> nums = context.GetValue(Nums);   
  17.             string result=string.Empty;   
  18.             if (!Calculate(nums, ref result)) {   
  19.                 result = "此题无解";   
  20.             }   
  21.             context.SetValue(Result, result);   
  22.         }   
  23.   
  24.         private bool Calculate(List<int> nums, ref string result) {   
  25.             DataTable dt = new DataTable();   
  26.             string[] opts = { "+""-""*""/" };   
  27.   
  28.             for (int a = 0; a < 4; a++) {   
  29.                 for (int b = 0; b < 4; b++) {   
  30.                     if (a == b) continue;   
  31.                     for (int c = 0; c < 4; c++) {   
  32.                         if (a == c || b == c) continue;   
  33.                         for (int d = 0; d < 4; d++) {   
  34.                             if (a == d || b == d || c == d) continue;   
  35.                             for (int e = 0; e < 4; e++) {   
  36.                                 for (int f = 0; f < 4; f++) {   
  37.                                     for (int g = 0; g < 4; g++) {   
  38.                                         try {   
  39.                                             string strExpression = string.Format("(({0}{4}{1}){5}{2}){6}{3}",   
  40.                                                                                 nums[a], nums[b], nums[c], nums[d],   
  41.                                                                                 opts[e], opts[f], opts[g]);   
  42.                                             double r = Convert.ToDouble(dt.Compute(strExpression, null));   
  43.                                             if (r == 24.0) {   
  44.                                                 result = strExpression;   
  45.                                                 return true;   
  46.                                             }   
  47.   
  48.                                             strExpression = string.Format("({0}{4}{1}){5}({2}{6}{3})",   
  49.                                                 nums[a], nums[b], nums[c], nums[d],   
  50.                                                 opts[e], opts[f], opts[g]);   
  51.                                             r = Convert.ToDouble(dt.Compute(strExpression, null));   
  52.                                             if (r == 24.0) {   
  53.                                                 result = strExpression;   
  54.                                                 return true;   
  55.                                             }   
  56.                                         } catch {}   
  57.                                     }   
  58.                                 }   
  59.                             }   
  60.                         }   
  61.                     }   
  62.                 }   
  63.             }   
  64.   
  65.             return false;   
  66.         }   
  67.     }   
  68. }   

活动输入:随机数集合List<int>
活动输出:计算结果String

简单讲解程序:首先创建一个表达式,然后用DataTable.Computer方法计算表达式的值是否是24,最终将结果返回。
其中前4个for用于生成随机数的排列,for中if用以判断一个数字不能重复使用;后面3个for用以生成表达式中任意三个操作符。然后计算:((数1?数2)?数3)?数4 ,第二种表达式模式是:(数1?数2)?(数3?数4),其中?表示某种操作符。

4.WPF中给按钮添加事件

Code:
  1. List<int> nums;   
  2. private void btnNewGame_Click(object sender, RoutedEventArgs e) {   
  3.     IDictionary<string,object> output = WorkflowInvoker.Invoke(new CreateRandomNumberActivity());   
  4.     nums = output["Nums"as List<int>;   
  5.   
  6.     /*  
  7.         * 遍历子控件  
  8.         * 注意:  
  9.         * 1.设置Grid的Name  
  10.         * 2.设置Label的Tag分别为0~3  
  11.         */  
  12.     int count = VisualTreeHelper.GetChildrenCount(gridMain);   
  13.     for (int i = 0; i < count; i++) {   
  14.         object ctrl = VisualTreeHelper.GetChild(gridMain, i);   
  15.         if (ctrl is Label) {   
  16.             Label lbl = ctrl as Label;   
  17.             if (lbl.Tag != null) {   
  18.                 int tag = Convert.ToInt32(lbl.Tag);   
  19.                 lbl.Content = nums[tag].ToString();   
  20.             }   
  21.         }   
  22.     }   
  23. }   
  24.   
  25. private void btnCalculate_Click(object sender, RoutedEventArgs e) {   
  26.     IDictionary<string,object> input=new Dictionary<string,object>{   
  27.         {"Nums",nums}   
  28.     };   
  29.     IDictionary<string,object> output = WorkflowInvoker.Invoke(new CalculateActivity(), input);   
  30.     string message = output["Result"as string;   
  31.     MessageBox.Show(message);   
  32. }  

最后看看效果图,还挺智能的!

下载地址:http://u.163.com/48Ozl

提取码:veziqgsa









 

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值