C#实现的24点游戏实例详解
作者:北风其凉
这篇文章主要介绍了C#实现的24点游戏,较为详细的分析了24点游戏的算法原理与实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下
本文实例分析了C#实现的24点游戏。分享给大家供大家参考。具体如下:
1. 24点游戏规则及算法
规则:给出4个自然数,找出能够求出24的四则运算式,要求数字不能重复使用
分析:
本算法是一种暴力求解法:
给出任意两个数字,可以进行6种四则运算,求出最多6个值。以数字a和b为例,有:
加(a+b)、减(a-b)、被减(b-a)、乘以(a*b)、除以(a/b)和除(b/a)
abcd共计四个数,如果顺序固定,则有5种计算顺序(★代表上面6种四则运算中的一种):
((a★b)★c)★d、(a★b)★(c★d)、(a★(b★c))★d、a★((b★c)★d)、a★(b★(c★d))
给出的四个数字,n1n2n3n4,有4*3*2=24种全排列的方法
2. 相关函数
/// <summary> /// 判断abcd四个数进行任意四则运算后能不能得出24,每个数字只能用一次 /// </summary> /// <param name="a">数字a</param> /// <param name="b">数字b</param> /// <param name="c">数字c</param> /// <param name="d">数字d</param> /// <param name="expression"></param> /// <returns></returns> private static bool Try24(int a, int b, int c, int d, ref string expression) { //a字头 if (TryEach(a, b, c, d, ref expression)) return true; if (TryEach(a, b, d, c, ref expression)) return true; if (TryEach(a, c, b, d, ref expression)) return true; if (TryEach(a, c, d, b, ref expression)) return true; if (TryEach(a, d, b, c, ref expression)) return true; if (TryEach(a, d, c, b, ref expression)) return true; //b字头 if (TryEach(b, a, c, d, ref expression)) return true; if (TryEach(b, a, d, c, ref expression)) return true; if (TryEach(b, c, a, d, ref expression)) return true; if (TryEach(b, c, d, a, ref expression)) return true; if (TryEach(b, d, a, c, ref expression)) return true; if (TryEach(b, d, c, a, ref expression)) return true; //c字头 if (TryEach(c, a, b, d, ref expression)) return true; if (TryEach(c, a, d, b, ref expression)) return true; if (TryEach(c, b, a, c, ref expression)) return true; if (TryEach(c, b, c, a, ref expression)) return true; if (TryEach(c, d, a, b, ref expression)) return true; if (TryEach(c, d, b, a, ref expression)) return true; //d字头 if (TryEach(d, a, b, c, ref expression)) return true; if (TryEach(d, a, c, b, ref expression)) return true; if (TryEach(d, b, a, c, ref expression)) return true; if (TryEach(d, b, c, a, ref expression)) return true; if (TryEach(d, c, a, b, ref expression)) return true; if (TryEach(d, c, b, a, ref expression)) return true; return false; } /// <summary> /// 判断指定顺序的四个数abcd进行任意四则运算后能不能得出24,每个数字只能用一次 /// </summary> /// <param name="a">数字1</param> /// <param name="b">数字2</param> /// <param name="c">数字3</param> /// <param name="d">数字4</param> /// <param name="expression"></param> /// <returns></returns> private static bool TryEach(int a, int b, int c, int d, ref string expression) { expression = ""; //两个数可以做6种运算:加、减、被减、乘以、除以、除 //四个数共可以进行6*6*6=216种不同次序的四则运算 //初始化数组 for (int i = 0; i < 6 * 6 * 6; i++) { //a与b间的运算符:i / 36 //b与c间的运算符:i % 36 / 6 //c与d间的运算符:i % 6 //1.运算顺序:a和b,再和c,再和d { string expression1 = "", expression2 = ""; int temp1 = ResultOf(a, b, i / 36, ref expression1); int temp2 = ResultOf(temp1, c, i % 36 / 6, ref expression2, expression1); int result = ResultOf(temp2, d, i % 6, ref expression, expression2); if (result == 24) return true; } //2.运算顺序:a和b,c和d,前面部分和后面部分 { string expression1 = "", expression2 = ""; int temp1 = ResultOf(a, b, i / 36, ref expression1); int temp2 = ResultOf(c, d, i % 6, ref expression2); int result = ResultOf(temp1, temp2, i % 36 / 6, ref expression, expression1, expression2); if (result == 24) return true; } //3.运算顺序:b和c运算,再与a运算,再与d运算 { string expression1 = "", expression2 = ""; int temp1 = ResultOf(b, c, i % 36 / 6, ref expression1); int temp2 = ResultOf(a, temp1, i / 36, ref expression2, "", expression1); int result = ResultOf(temp2, d, i % 6, ref expression, expression2); if (result == 24) return true; } //4.运算顺序:b和c运算,再与d运算,再与a运算 { string expression1 = "", expression2 = ""; int temp1 = ResultOf(b, c, i % 36 / 6, ref expression1); int temp2 = ResultOf(temp1, d, i % 6, ref expression2, expression1); int result = ResultOf(a, temp2, i / 36, ref expression, "", expression2); if (result == 24) return true; } //5.运算顺序:c和d运算,再和b运算,再和a运算 { string expression1 = "", expression2 = ""; int temp1 = ResultOf(c, d, i % 6, ref expression1); int temp2 = ResultOf(b, temp1, i % 36 / 6, ref expression2, "", expression1); int result = ResultOf(a, temp2, i / 36, ref expression, "", expression2); if (result == 24) return true; } } expression = "Abandoned"; return false; } /// <summary> /// 求两数进行某一四则运算后的结果 /// </summary> /// <param name="x">数字1</param> /// <param name="y">数字2</param> /// <param name="method">(0-5分别代表:加、减、被减、乘以、除以、除)</param> /// <param name="expression">返回的表达式</param> /// <param name="expressionLeft">数字1表达式</param> /// <param name="expressionRight">数字2表达式</param> /// <returns></returns> private static int ResultOf(int x, int y, int method, ref string expression, string expressionLeft = "", string expressionRight = "") { //左右表达式之前被判定为无效则不计算,除数为0时不计算 if (expressionLeft == "Abandoned" || expressionRight == "Abandoned" || (x == 0 && method == 5) || (y == 0 && method == 4)) { expression = "Abandoned"; return -1; } int result = 0; switch (method) { case 0: { //加 result = x + y; expression = string.Format("{0}+{1}", expressionLeft == "" ? x.ToString() : expressionLeft, expressionRight == "" ? y.ToString() : expressionRight); } break; case 1: { //减 result = x - y; expression = string.Format("{0}-{1}", expressionLeft == "" ? x.ToString() : expressionLeft, expressionRight == "" ? y.ToString() : expressionRight); } break; case 2: { //被减 result = y - x; expression = string.Format("{1}-{0}", expressionLeft == "" ? x.ToString() : expressionLeft, expressionRight == "" ? y.ToString() : expressionRight); } break; case 3: { //乘以 result = x * y; expression = string.Format("({0})*({1})", expressionLeft == "" ? x.ToString() : expressionLeft, expressionRight == "" ? y.ToString() : expressionRight); } break; case 4: { //除以 if (x % y == 0) { result = x / y; expression = string.Format("({0})/({1})", expressionLeft == "" ? x.ToString() : expressionLeft, expressionRight == "" ? y.ToString() : expressionRight); } else { expression = "Abandoned"; } } break; case 5: { //除 if (y % x == 0) { result = y / x; expression = string.Format("({1})/({0})", expressionLeft == "" ? x.ToString() : expressionLeft, expressionRight == "" ? y.ToString() : expressionRight); } else { expression = "Abandoned"; } } break; } //运算不合法,则返回-1,表达式为Abandoned, if (expression == "Abandoned") { return -1; } return result; }
3. Main函数调用
static void Main(string[] args) { string s = ""; bool try24; try24 = Try24(1, 2, 3, 4, ref s); Console.WriteLine("1,2,3,4: " + s); try24 = Try24(1, 3, 5, 7, ref s); Console.WriteLine("1,3,5,7: " + s); try24 = Try24(2, 3, 6, 4, ref s); Console.WriteLine("2,3,6,4: " + s); try24 = Try24(5, 7, 9, 4, ref s); Console.WriteLine("5,7,9,4: " + s); try24 = Try24(6, 3, 4, 5, ref s); Console.WriteLine("6,3,4,5: " + s); try24 = Try24(9, 5, 2, 7, ref s); Console.WriteLine("9,5,2,7: " + s); try24 = Try24(4, 9, 8, 3, ref s); Console.WriteLine("4,9,8,3: " + s); try24 = Try24(4, 9, 8, 2, ref s); Console.WriteLine("4,9,8,2: " + s); try24 = Try24(10, 10, 10, 10, ref s); Console.WriteLine("10,10,10,10: " + s); Console.ReadLine(); }
4. 程序运行结果:
希望本文所述对大家的C#程序设计有所帮助。