C++ 24点游戏 C++实现简单24点游戏
JaCo Wu 人气:0随机生成4个代表扑克牌牌面的数字字母,程序自动列出所有可能算出24的表达式,用擅长的语言(C/C++/Java或其他均可)实现程序解决问题。
程序风格良好(使用自定义注释模板)
列出表达式无重复。
以下为源代码
#include<iostream> #include<string> #include <stdlib.h> #include<time.h> using namespace std; char card[] = { 'A','2','3','4','5','6','7','8','9','10','J','Q','K' }; char buf[4]; double nums[4]; char ope[4] = { '+','-','*','/' }; void cre()//生成 { int i = 0; int j; cout << "生成的四张牌面为:"; srand((unsigned)time(0)); for (i = 0; i<4; i++) { j =rand() % 13; buf[i] = card[j]; } cout << buf[0] << ";" << buf[1] << ";" << buf[2] << ";" << buf[3] << "。" << endl; for (i = 0; i<4; i++) { if (buf[i] == 'A') nums[i] = 1; else if(buf[i] == '2') nums[i] = 2; else if (buf[i] == '3') nums[i] = 3; else if (buf[i] == '4') nums[i] = 4; else if (buf[i] == '5') nums[i] = 5; else if (buf[i] == '6') nums[i] = 6; else if (buf[i] == '7') nums[i] = 7; else if (buf[i] == '8') nums[i] = 8; else if (buf[i] == '9') nums[i] = 9; else if (buf[i] == '10') nums[i] = 10; else if (buf[i] == 'J') nums[i] = 11; else if (buf[i] == 'Q') nums[i] = 12; else if (buf[i] == 'K') nums[i] = 13; } } double calcute(double a, double b, char index) { if (index == '+') return a + b; //若为+,则返回相应结果 else if (index == '-') return a - b; else if (index == '*') return a*b; else if (index == '/') if (b != 0) return a / b; //只有当分母不为0时,返回结果 } void exh()//穷举计算 { double temp[3], tem[2]; //第一个符号放置后,经过计算后相当于剩下三个数,这个数组用于存储这三个数 double sum; //求得的和 int judge = 0; //判断是否找到一个合理的解 for (int i = 0; i < 4; i++) //第一次放置的符号 { for (int j = 0; j < 4; j++) //第二次放置的符号 { for (int k = 0; k < 4; k++) //第三次放置的符号 { for (int m = 0; m < 3; m++) //首先计算的两个相邻数字,共有3种情况,相当于括号的作用 { if (nums[m + 1] == 0 && ope[i] == '/') break; temp[m] = calcute(nums[m], nums[m + 1], ope[i]); temp[(m + 1) % 3] = nums[(m + 2) % 4]; temp[(m + 2) % 3] = nums[(m + 3) % 4]; //先确定首先计算的两个数字,计算完成相当于剩下三个数,按顺序储存在temp数组中 for (int n = 0; n < 2; n++) //三个数字选出先计算的两个相邻数字,两种情况,相当于第二个括号 { if (temp[n + 1] == 0 && ope[j] == '/') break; tem[n] = calcute(temp[n], temp[n + 1], ope[j]); tem[(n + 1) % 2] = temp[(n + 2) % 3]; //先确定首先计算的两个数字,计算完成相当于剩下两个数,按顺序储存在temp数组中 if (tem[1] == 0 && ope[k] == '/') break; sum = calcute(tem[0], tem[1], ope[k]); //计算和 if (sum == 24) //若和为24 { judge = 1; //判断符为1,表示已求得解 if (m == 0 && n == 0) cout << "((" << nums[0] << ope[i] << nums[1] << ")" << ope[j] << nums[2] << ")" << ope[k] << nums[3] << "=" << sum << endl; else if (m == 0 && n == 1) cout << "(" << nums[0] << ope[i] << nums[1] << ")" << ope[k] << "(" << nums[2] << ope[j] << nums[3] << ")=" << sum << endl; else if (m == 1 && n == 0) cout << "(" << nums[0] << ope[j] << "(" << nums[1] << ope[i] << nums[2] << ")" << ope[k] << nums[3] << "=" << sum << endl; else if (m == 1 && n == 1) cout << nums[0] << ope[k] << "((" << nums[1] << ope[i] << nums[2] << ")" << ope[j] << nums[3] << ")=" << sum << endl; else if (m == 2 && n == 0) cout << "(" << nums[0] << ope[j] << nums[1] << ")" << ope[k] << "(" << nums[2] << ope[i] << nums[3] << ")=" << sum << endl; else if (m == 2 && n == 0) cout << nums[0] << ope[k] << "(" << nums[1] << ope[j] << "(" << nums[2] << ope[i] << nums[3] << "))=" << sum << endl; //m=0,1,2 n=0,1表示六种括号放置可能,并按照这六种可能输出相应的格式的计算式 } } } } } } if (judge == 0) cout << "这四张扑克牌无法找到一个合理的解" << endl; //如果没有找到结果,符号位为0 } int main() { int i; int select = 1; cout<< " ################################################" << endl << " # #" << endl << " # 欢迎进入24点游戏 #" << endl << " # #" << endl << " ################################################" << endl; while (select) { cout<< " ################################################" << endl << " # #" << endl << " # 是否开始游戏 #" << endl << " # #" << endl << " # 0.是 1.否 #" << endl << " # #" << endl << " ################################################" << endl; cout << "请输入你的选择(0或1):"; cin >> i; switch (i) { case 0: cre(); exh(); break; case 1: select = 0; break; default: cout << "请在0和1之间选择!" << endl; } } return 0; }
效果图1
效果图2
加载全部内容