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

C#实现的算24点游戏的算法

程序员文章站 2022-04-28 12:37:57
...
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;
  
        /// 
        /// 符号优先级
        /// 
        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;
            }
        }
  
        /// 
        /// 基本单元构造函数
        /// 
        /// 单元类型,数值或符号
        /// 数值
        /// 符号
        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;
        }
  
        /// 
        /// 在第n位置放置一个单元
        /// 
        /// 
        void PutCell(int n)
        {
            if (n >= 7)
            {
                if (Calculate())
                {
                    m_stop = true;
                    Formate();
                }
                return;
            }
            int end = 8;
            if (n 
        /// 检查当前放置是否合理
        /// 
        /// 
        /// 
        bool CheckCell(int n)
        {
            int nums = 0, sigs = 0;
            for (int i = 0; i 
        /// 计算表达式是否为24
        /// 
        /// 返回值true为24,否则不为24
        bool Calculate()
        {
            double[] dblStack = new double[4];
            int indexStack = -1;
            for (int i = 0; i 
        /// 后缀表达式到中缀表达式
        /// 
        void Formate()
        {
            Cell[] c = new Cell[7];
            for (int i = 0; i  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);
            }
        }
    }
}