目标:用WF+WPF完成24点游戏程序。
1.界面,新建一个WPF项目,4个Label+2个按钮,注意:4个Label分别设置Tag值为:0~3
- <Window x: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">
- <Grid Name="gridMain">
- <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" />
- <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" />
- <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" />
- <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" />
- <Button Content="New _Game" Height="23" HorizontalAlignment="Left" Margin="71,130,0,0" Name="btnNewGame" VerticalAlignment="Top" Width="100" FontSize="12" Click="btnNewGame_Click" />
- <Button Content="Auto Calculate" 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
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Activities;
- using System.Data;
- namespace Point24 {
- public sealed class CalculateActivity : CodeActivity {
- public InArgument<List<int>> Nums { get; set; }
- public OutArgument<string> Result { get; set; }
- protected override void Execute(CodeActivityContext context) {
- List<int> nums = context.GetValue(Nums);
- string result=string.Empty;
- if (!Calculate(nums, ref result)) {
- result = "此题无解";
- }
- context.SetValue(Result, result);
- }
- private bool Calculate(List<int> nums, ref string result) {
- DataTable dt = new DataTable();
- string[] opts = { "+", "-", "*", "/" };
- for (int a = 0; a < 4; a++) {
- for (int b = 0; b < 4; b++) {
- if (a == b) continue;
- for (int c = 0; c < 4; c++) {
- if (a == c || b == c) continue;
- for (int d = 0; d < 4; d++) {
- if (a == d || b == d || c == d) continue;
- for (int e = 0; e < 4; e++) {
- for (int f = 0; f < 4; f++) {
- for (int g = 0; g < 4; g++) {
- try {
- string strExpression = string.Format("(({0}{4}{1}){5}{2}){6}{3}",
- nums[a], nums[b], nums[c], nums[d],
- opts[e], opts[f], opts[g]);
- double r = Convert.ToDouble(dt.Compute(strExpression, null));
- if (r == 24.0) {
- result = strExpression;
- return true;
- }
- 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;
- return true;
- }
- } catch {}
- }
- }
- }
- }
- }
- }
- }
- return false;
- }
- }
- }
活动输入:随机数集合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;
- private void btnNewGame_Click(object sender, RoutedEventArgs e) {
- IDictionary<string,object> output = WorkflowInvoker.Invoke(new CreateRandomNumberActivity());
- nums = output["Nums"] as List<int>;
- /*
- * 遍历子控件
- * 注意:
- * 1.设置Grid的Name
- * 2.设置Label的Tag分别为0~3
- */
- int count = VisualTreeHelper.GetChildrenCount(gridMain);
- for (int i = 0; i < count; i++) {
- object ctrl = VisualTreeHelper.GetChild(gridMain, i);
- if (ctrl is Label) {
- Label lbl = ctrl as Label;
- if (lbl.Tag != null) {
- int tag = Convert.ToInt32(lbl.Tag);
- lbl.Content = nums[tag].ToString();
- }
- }
- }
- }
- private void btnCalculate_Click(object sender, RoutedEventArgs e) {
- IDictionary<string,object> input=new Dictionary<string,object>{
- {"Nums",nums}
- };
- IDictionary<string,object> output = WorkflowInvoker.Invoke(new CalculateActivity(), input);
- string message = output["Result"] as string;
- MessageBox.Show(message);
- }
最后看看效果图,还挺智能的!
下载地址:http://u.163.com/48Ozl
提取码:veziqgsa