C#实现的24点游戏实例详解
程序员文章站
2023-12-03 17:00:46
本文实例分析了c#实现的24点游戏。分享给大家供大家参考。具体如下:
1. 24点游戏规则及算法
规则:给出4个自然数,找出能够求出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#程序设计有所帮助。
上一篇: Asp.net MVC定义短网址的方法