欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现的算24点游戏算法实例分析

程序员文章站 2024-01-05 11:33:22
本文实例讲述了c#实现的算24点游戏算法。分享给大家供大家参考。具体如下: using system; using system.collections.gen...

本文实例讲述了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#程序设计有所帮助。

上一篇:

下一篇: