C#实现的算24点游戏算法实例分析
作者:lele 发布时间:2021-12-01 04:13:21
标签:C#,游戏,算法
本文实例讲述了C#实现的算24点游戏算法。分享给大家供大家参考。具体如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Calc24Points
{
public class Cell
{
public enum Type
{
Number,
Signal
}
public int Number;
public char Signal;
public Type Typ;
public Cell Right;
public Cell Left;
/// <summary>
/// 符号优先级
/// </summary>
public int Priority
{
get
{
if (Typ == Type.Signal)
{
switch (Signal)
{
case '+': return 0;
case '-': return 0;
case '*': return 1;
case '/': return 1;
default: return -1;
}
}
return -1;
}
}
/// <summary>
/// 基本单元构造函数
/// </summary>
/// <param name="t">单元类型,数值或符号</param>
/// <param name="num">数值</param>
/// <param name="sig">符号</param>
public Cell(Type t, int num, char sig)
{
Right = null;
Left = null;
Typ = t;
Number = num;
Signal = sig;
}
public Cell()
{
Right = null;
Left = null;
Number = 0;
Typ = Type.Number;
}
public Cell(Cell c)
{
Right = null;
Left = null;
Number = c.Number;
Signal = c.Signal;
Typ = c.Typ;
}
}
public class Calc24Points
{
string m_exp;
bool m_stop;
Cell[] m_cell;
int[] m_express;
StringWriter m_string;
public Calc24Points(int n1, int n2, int n3, int n4)
{
m_cell = new Cell[8];
m_cell[0] = new Cell(Cell.Type.Number, n1, '?');
m_cell[1] = new Cell(Cell.Type.Number, n2, '?');
m_cell[2] = new Cell(Cell.Type.Number, n3, '?');
m_cell[3] = new Cell(Cell.Type.Number, n4, '?');
m_cell[4] = new Cell(Cell.Type.Signal, 0, '+');
m_cell[5] = new Cell(Cell.Type.Signal, 0, '-');
m_cell[6] = new Cell(Cell.Type.Signal, 0, '*');
m_cell[7] = new Cell(Cell.Type.Signal, 0, '/');
m_stop = false;
m_express = new int[7];
m_string = new StringWriter();
m_exp = null;
}
public override string ToString()
{
if (m_exp == null)
{
PutCell(0);
m_exp = m_string.ToString();
}
if (m_exp != "") return m_exp;
return null;
}
/// <summary>
/// 在第n位置放置一个单元
/// </summary>
/// <param name="n"></param>
void PutCell(int n)
{
if (n >= 7)
{
if (Calculate())
{
m_stop = true;
Formate();
}
return;
}
int end = 8;
if (n < 2) end = 4;
for (int i = 0; i < end; ++i)
{
m_express[n] = i;
if (CheckCell(n)) PutCell(n + 1);
if (m_stop) break;
}
}
/// <summary>
/// 检查当前放置是否合理
/// </summary>
/// <param name="n"></param>
/// <returns></returns>
bool CheckCell(int n)
{
int nums = 0, sigs = 0;
for (int i = 0; i <= n; ++i)
{
if (m_cell[m_express[i]].Typ == Cell.Type.Number) ++nums;
else ++sigs;
}
if (nums - sigs < 1) return false;
if (m_cell[m_express[n]].Typ == Cell.Type.Number)
//数值不能重复,但是符号可以重复
{
for (int i = 0; i < n; ++i) if (m_express[i] == m_express[n]) return false;
}
if (n == 6)
{
if (nums != 4 || sigs != 3) return false;
if (m_cell[m_express[6]].Typ != Cell.Type.Signal) return false;
return true;
}
return true;
}
/// <summary>
/// 计算表达式是否为24
/// </summary>
/// <returns>返回值true为24,否则不为24</returns>
bool Calculate()
{
double[] dblStack = new double[4];
int indexStack = -1;
for (int i = 0; i < 7; ++i)
{
if (m_cell[m_express[i]].Typ == Cell.Type.Number)
{
++indexStack;
dblStack[indexStack] = m_cell[m_express[i]].Number;
}
else
{
switch (m_cell[m_express[i]].Signal)
{
case '+':
dblStack[indexStack - 1] = dblStack[indexStack - 1] + dblStack[indexStack];
break;
case '-':
dblStack[indexStack - 1] = dblStack[indexStack - 1]-+ dblStack[indexStack];
break;
case '*':
dblStack[indexStack - 1] = dblStack[indexStack - 1] * dblStack[indexStack];
break;
case '/':
dblStack[indexStack - 1] = dblStack[indexStack - 1] / dblStack[indexStack];
break;
}
--indexStack;
}
}
if (Math.Abs(dblStack[indexStack] - 24) < 0.1) return true;
return false;
}
/// <summary>
/// 后缀表达式到中缀表达式
/// </summary>
void Formate()
{
Cell[] c = new Cell[7];
for (int i = 0; i < 7; ++i) c[i] = new Cell(m_cell[m_express[i]]);
int[] cStack = new int[4];
int indexStack = -1;
for (int i = 0; i < 7; ++i)
{
if (c[i].Typ == Cell.Type.Number)
{
++indexStack;
cStack[indexStack] = i;
}
else
{
c[i].Right = c[cStack[indexStack]];
--indexStack;
c[i].Left = c[cStack[indexStack]];
cStack[indexStack] = i;
}
}
ToStringFormate(c[cStack[indexStack]]);
}
void ToStringFormate(Cell root)
{
if (root.Left.Typ == Cell.Type.Number)
{
m_string.Write(root.Left.Number);
m_string.Write(root.Signal);
}
else
{
if (root.Priority > root.Left.Priority)
{
m_string.Write("(");
ToStringFormate(root.Left);
m_string.Write(")");
}
else ToStringFormate(root.Left);
m_string.Write(root.Signal);
}
if (root.Right.Typ == Cell.Type.Number) m_string.Write(root.Right.Number);
else
{
if (root.Priority >= root.Right.Priority)
{
m_string.Write("(");
ToStringFormate(root.Right);
m_string.Write(")");
}
else ToStringFormate(root.Right);
}
}
}
}
希望本文所述对大家的C#程序设计有所帮助。


猜你喜欢
- 一、MyBatis Plus 介绍MyBatis Plus 是国内人员开发的 MyBatis 增强工具,在 MyBatis 的基础上只做增强
- 代码如下所示:package com.hoo.util; import java.awt.Color; import
- 本文实例讲述了Android编程解析XML文件的方法。分享给大家供大家参考,具体如下:前言在学习Android的Framework层源码时,
- 断断续续的总算的把android开发和逆向
- 前言有些业务比较复杂,比如我们需要新建10张表,每张表有10个字段,如果用手工来操作,肯定非常浪费时间,而且随着代码中对实体类的修改,还要同
- java实现拖拽示例Swing中实现拖拽功能,代码很简单,都有注释,自己看,运行效果如下图:package com;import java.
- 一、前言在日常工作中,如果涉及到与第三方进行接口对接,有的会使用WebService的方式,这篇文章主要讲解在.NET Framework中
- 前言Android底层服务,即运行在 linux 下的进程,是 Android 系统运行的基础,完成 Android 或者说计算机最基本的功
- RTF文档即富文本格式(Rich Text Format)的文档。我们在处理文件时,遇到需要对文档格式进行转换时,可以将RTF转为其他格式,
- 一、微服务结构微服务这种方案需要技术框架来落地,全球的互联网公司都在积极尝试自己的微服务落地技术。在国内最知名的就是SpringCloud和
- 写在前面元旦三天在家闲着无事,就看了看Linq的相关内容,也准备系统的学习一下,作为学习Linq的前奏,还是先得说说Lambda与匿名方法的
- 一、前言 验证码可以说在我们生活中已经非常普遍了,任何一个网站,任何一个App都
- 最近项目需要微信支付,然后看了下微信公众号支付,,虽然不难,但是细节还是需要注意的,用了大半天时间写了个demo,并且完整的测试了一下支付流
- @ConfigurationProperties源码分析@ConfigurationProperties主要作用就是将prefix属性指定的
- 从功能上说,可以分为两部分,分布式功能和数据功能。分布式功能主要是节点集群及集群附属功能如restful借口、集群性能检测功能等,数据功能主
- 1、使用JPA 的@Enumerated 注解 ,可以直接将Enum映射到数据库中。但是value的值只有两种方式选择,一种是使用枚举的序号
- 本文实例讲述了Android编程出现Button点击事件无效的解决方法。分享给大家供大家参考,具体如下:遇到这样一个问题,给一个界面上方的按
- 一.枚举和静态常量区别讲到枚举我们首先思考,它和public static final String 修饰的常量有什么不同。我举枚举的两个优
- 前言Unity在运行时可以将一些物体进行合并,从而用一个绘制调用来渲染他们。这一操作,我们称之为“批处理”,能得到越好的渲染性能。Unity
- 近期,Apache SkyWalking 修复了一个隐藏了近4年的Bug - TTL timer 可能失效问题,这个 bug 在 SkyWa