WF4学习-24点游戏

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

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

Code:
  1. <Windowx: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. <GridName="gridMain">
  6. <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"/>
  7. <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"/>
  8. <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"/>
  9. <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"/>
  10. <ButtonContent="New_Game"Height="23"HorizontalAlignment="Left"Margin="71,130,0,0"Name="btnNewGame"VerticalAlignment="Top"Width="100"FontSize="12"Click="btnNewGame_Click"/>
  11. <ButtonContent="AutoCalculate"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. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.Linq;
  4. usingSystem.Text;
  5. usingSystem.Activities;
  6. usingSystem.Data;
  7. namespacePoint24{
  8. publicsealedclassCalculateActivity:CodeActivity{
  9. publicInArgument<List<int>>Nums{get;set;}
  10. publicOutArgument<string>Result{get;set;}
  11. protectedoverridevoidExecute(CodeActivityContextcontext){
  12. List<int>nums=context.GetValue(Nums);
  13. stringresult=string.Empty;
  14. if(!Calculate(nums,refresult)){
  15. result="此题无解";
  16. }
  17. context.SetValue(Result,result);
  18. }
  19. privateboolCalculate(List<int>nums,refstringresult){
  20. DataTabledt=newDataTable();
  21. string[]opts={"+","-","*","/"};
  22. for(inta=0;a<4;a++){
  23. for(intb=0;b<4;b++){
  24. if(a==b)continue;
  25. for(intc=0;c<4;c++){
  26. if(a==c||b==c)continue;
  27. for(intd=0;d<4;d++){
  28. if(a==d||b==d||c==d)continue;
  29. for(inte=0;e<4;e++){
  30. for(intf=0;f<4;f++){
  31. for(intg=0;g<4;g++){
  32. try{
  33. stringstrExpression=string.Format("(({0}{4}{1}){5}{2}){6}{3}",
  34. nums[a],nums[b],nums[c],nums[d],
  35. opts[e],opts[f],opts[g]);
  36. doubler=Convert.ToDouble(dt.Compute(strExpression,null));
  37. if(r==24.0){
  38. result=strExpression;
  39. returntrue;
  40. }
  41. strExpression=string.Format("({0}{4}{1}){5}({2}{6}{3})",
  42. nums[a],nums[b],nums[c],nums[d],
  43. opts[e],opts[f],opts[g]);
  44. r=Convert.ToDouble(dt.Compute(strExpression,null));
  45. if(r==24.0){
  46. result=strExpression;
  47. returntrue;
  48. }
  49. }catch{}
  50. }
  51. }
  52. }
  53. }
  54. }
  55. }
  56. }
  57. returnfalse;
  58. }
  59. }
  60. }

活动输入:随机数集合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. privatevoidbtnNewGame_Click(objectsender,RoutedEventArgse){
  3. IDictionary<string,object>output=WorkflowInvoker.Invoke(newCreateRandomNumberActivity());
  4. nums=output["Nums"]asList<int>;
  5. /*
  6. *遍历子控件
  7. *注意:
  8. *1.设置Grid的Name
  9. *2.设置Label的Tag分别为0~3
  10. */
  11. intcount=VisualTreeHelper.GetChildrenCount(gridMain);
  12. for(inti=0;i<count;i++){
  13. objectctrl=VisualTreeHelper.GetChild(gridMain,i);
  14. if(ctrlisLabel){
  15. Labellbl=ctrlasLabel;
  16. if(lbl.Tag!=null){
  17. inttag=Convert.ToInt32(lbl.Tag);
  18. lbl.Content=nums[tag].ToString();
  19. }
  20. }
  21. }
  22. }
  23. privatevoidbtnCalculate_Click(objectsender,RoutedEventArgse){
  24. IDictionary<string,object>input=newDictionary<string,object>{
  25. {"Nums",nums}
  26. };
  27. IDictionary<string,object>output=WorkflowInvoker.Invoke(newCalculateActivity(),input);
  28. stringmessage=output["Result"]asstring;
  29. MessageBox.Show(message);
  30. }

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

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

提取码:veziqgsa









评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值