SoFunction
Updated on 2025-03-06

Analysis of 24-point game algorithm examples implemented by C#

This article describes the 24-point game algorithm implemented in C#. Share it for your reference. The details are as follows:

using System;
using ;
using ;
using ;
using ;
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>
  /// Symbol Priority  /// </summary>
  public int Priority
  {
   get
   {
    if (Typ == )
    {
     switch (Signal)
     {
      case '+': return 0;
      case '-': return 0;
      case '*': return 1;
      case '/': return 1;
      default: return -1;
     }
    }
    return -1;
   }
  }
  /// <summary>
  /// Basic unit constructor  /// </summary>
  /// <param name="t">Unit type, value or symbol</param>  /// <param name="num">value</param>  /// <param name="sig">Symbol</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 = ;
  }
  public Cell(Cell c)
  {
   Right = null;
   Left = null;
   Number = ;
   Signal = ;
   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(, n1, '?');
   m_cell[1] = new Cell(, n2, '?');
   m_cell[2] = new Cell(, n3, '?');
   m_cell[3] = new Cell(, n4, '?');
   m_cell[4] = new Cell(, 0, '+');
   m_cell[5] = new Cell(, 0, '-');
   m_cell[6] = new Cell(, 0, '*');
   m_cell[7] = new Cell(, 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;
  }
  /// &lt;summary&gt;
  /// Place a unit at nth position  /// &lt;/summary&gt;
  /// &lt;param name="n"&gt;&lt;/param&gt;
  void PutCell(int n)
  {
   if (n &gt;= 7)
   {
    if (Calculate())
    {
     m_stop = true;
     Formate();
    }
    return;
   }
   int end = 8;
   if (n &lt; 2) end = 4;
   for (int i = 0; i &lt; end; ++i)
   {
    m_express[n] = i;
    if (CheckCell(n)) PutCell(n + 1);
    if (m_stop) break;
   }
  }
  /// &lt;summary&gt;
  /// Check whether the current placement is reasonable  /// &lt;/summary&gt;
  /// &lt;param name="n"&gt;&lt;/param&gt;
  /// &lt;returns&gt;&lt;/returns&gt;
  bool CheckCell(int n)
  {
   int nums = 0, sigs = 0;
   for (int i = 0; i &lt;= n; ++i)
   {
    if (m_cell[m_express[i]].Typ == ) ++nums;
    else ++sigs;
   }
   if (nums - sigs &lt; 1) return false;
   if (m_cell[m_express[n]].Typ == )
   //The values ​​cannot be repeated, but the symbols can be repeated   {
    for (int i = 0; i &lt; 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 != ) return false;
    return true;
   }
   return true;
  }
  /// &lt;summary&gt;
  /// Calculate whether the expression is 24  /// &lt;/summary&gt;
  /// <returns>The return value is true to 24, otherwise it will not be 24</returns>  bool Calculate()
  {
   double[] dblStack = new double[4];
   int indexStack = -1;
   for (int i = 0; i &lt; 7; ++i)
   {
    if (m_cell[m_express[i]].Typ == )
    {
     ++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 ((dblStack[indexStack] - 24) &lt; 0.1) return true;
   return false;
  }
  /// &lt;summary&gt;
  /// Suffix expression to infix expression  /// &lt;/summary&gt;
  void Formate()
  {
   Cell[] c = new Cell[7];
   for (int i = 0; i &lt; 7; ++i) c[i] = new Cell(m_cell[m_express[i]]);
   int[] cStack = new int[4];
   int indexStack = -1;
   for (int i = 0; i &lt; 7; ++i)
   {
    if (c[i].Typ == )
    {
     ++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 ( == )
   {
    m_string.Write();
    m_string.Write();
   }
   else
   {
    if ( &gt; )
    {
     m_string.Write("(");
     ToStringFormate();
     m_string.Write(")");
    }
    else ToStringFormate();
    m_string.Write();
   }
   if ( == ) m_string.Write();
   else
   {
    if ( &gt;= )
    {
     m_string.Write("(");
     ToStringFormate();
     m_string.Write(")");
    }
    else ToStringFormate();
   }
  }
 }
}

I hope this article will be helpful to everyone's C# programming.