C#實(shí)現(xiàn)的算24點(diǎn)游戲算法實(shí)例分析
更新時(shí)間:2015年04月27日 14:22:57 作者:lele
這篇文章主要介紹了C#實(shí)現(xiàn)的算24點(diǎn)游戲算法,實(shí)例分析了算24點(diǎn)游戲相關(guān)的運(yùn)算技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了C#實(shí)現(xiàn)的算24點(diǎn)游戲算法。分享給大家供大家參考。具體如下:
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> /// 符號(hào)優(yōu)先級(jí) /// </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> /// 基本單元構(gòu)造函數(shù) /// </summary> /// <param name="t">單元類(lèi)型,數(shù)值或符號(hào)</param> /// <param name="num">數(shù)值</param> /// <param name="sig">符號(hào)</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位置放置一個(gè)單元 /// </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> /// 檢查當(dāng)前放置是否合理 /// </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) //數(shù)值不能重復(fù),但是符號(hào)可以重復(fù) { 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> /// 計(jì)算表達(dá)式是否為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> /// 后綴表達(dá)式到中綴表達(dá)式 /// </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); } } } }
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
c#制作簡(jiǎn)單啟動(dòng)畫(huà)面的方法
這篇文章主要介紹了c#制作簡(jiǎn)單啟動(dòng)畫(huà)面的方法,涉及C#實(shí)現(xiàn)桌面程序啟動(dòng)畫(huà)面的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04c# Newtonsoft 六個(gè)值得使用的特性(上)
這篇文章主要介紹了c# Newtonsoft 六個(gè)值得使用的特性,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-06-06解析使用enumerator模式簡(jiǎn)化異步操作的詳解
本篇文章是對(duì)使用enumerator模式簡(jiǎn)化異步操作進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05C#中使用HttpPost調(diào)用WebService的方法
這篇文章介紹了C#中使用HttpPost調(diào)用WebService的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03關(guān)于finalize機(jī)制和引用、引用隊(duì)列的用法詳解
下面小編就為大家?guī)?lái)一篇關(guān)于finalize機(jī)制和引用、引用隊(duì)列的用法詳解。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-09-09C#檢查指定對(duì)象是否存在于ArrayList集合中的方法
這篇文章主要介紹了C#檢查指定對(duì)象是否存在于ArrayList集合中的方法,涉及C#中Contains方法的使用技巧,需要的朋友可以參考下2015-04-04