你想我猜小游戏
作者:互联网
#include <iostream> #include <algorithm> #include <string> #include<sstream> #include<ctime> #include<vector> using namespace std; class gam2 { public: gam2(string str) :result1(str) {}; ~gam2(); static string resultgam2;//用于存储电脑的数字 string getresult(); string compareresult(); private: string result1; }; string gam2::resultgam2 = "0123"; string gam2::getresult() { return result1; } string gam2::compareresult() { int j = 0; int k = 0;//原本是放在第一个for循环里面的 string str1 = result1; string str2 = gam2::resultgam2; for (int i = 0; i < 4; i++) { for (int g = 0; g < 4; g++) { if (str1[i] == str2[g]) { if (i == g) j++; else k++; } } } string str3 = "0A0B"; str3[0] = str3[0] + j;//原本j放for循环里面,不可见 str3[2] = str3[2] + k; return str3; } gam2::~gam2() { } int mainmenu(); void game1(); void game2(); void paihangbang(); string getresultgam2(); int isgam2available(string* j); int isgam1available(string* j, int* A, int* B); void gam1errorout();//用于错误退出 void gam1victory(); void gam1get(int& C, int& A, int& B, string& i);//用于读入并更新 void switch0(int& C, int& A, int& B, string& i, string& j, string& k); void switch02(int& C, int& A, int& B, string& i, string& j, string& k);//解决0,2,2的问题,注:每个数字代表的是4,4,2一组的数字中知道有多少个。 void switch023(int& C, int& A, int& B, string& i, string& j, string& k); void switch03(int& C, int& A, int& B, string& i, string& j, string& k);//解决0,3,1的问题 void switch032(int& C, int& A, int& B, string& i, string& j, string& k); void switch033(int& C, int& A, int& B, string& i, string& j, string& k); void switch1(int& C, int& A, int& B, string& i, string& j, string& k); void switch11(int& C, int& A, int& B, string& i, string& j, string& k);//解决1,1,2的问题 void switch112(int& C, int& A, int& B, string& i, string& j, string& k); void switch112_1(int& C, int& A, int& B, string& i, string& j, string& k);//解决知道后三个判断第一个的问题 void switch113(int& C, int& A, int& B, string& i, string& j, string& k); void switch12(int& C, int& A, int& B, string& i, string& j, string& k);//解决1,2,1的问题 void switch123_1(int& C, int& A, int& B, string& i, string& j, string& k);//已知k23是,从j456选择一个,里面调用了switch112_1 void switch123_2(int& C, int& A, int& B, string& i, string& j, string& k);//已知k3是,j7不是,从j456选择两个,里面调用了switch112_1 void switch13(int& C, int& A, int& B, string& i, string& j, string& k);//解决1,3,0的问题 void switch2(int& C, int& A, int& B, string& i, string& j, string& k); void switch22(int& C, int& A, int& B, string& i, string& j, string& k);//解决2,2,0的问题 void switch22_1(int& C, int& A, int& B, string& i, string& j, string& k); void switch3(int& C, int& A, int& B, string& i, string& j, string& k); void switchA0(int& C, int& A, int& B, string& i, string& j, string& k); void switchA00(int& C, int& A, int& B, string& i, string& j, string& k); void switchA001(int& C, int& A, int& B, string& i, string& j, string& k); void switchA01(int& C, int& A, int& B, string& i, string& j, string& k); void switchA1(int& C, int& A, int& B, string& i, string& j, string& k); void switchA10(int& C, int& A, int& B, string& i, string& j, string& k); void switchA12(int& C, int& A, int& B, string& i, string& j, string& k); void switchA2(int& C, int& A, int& B, string& i, string& j, string& k); void switchA21(int& C, int& A, int& B, string& i, string& j, string& k); int main() { while (mainmenu()); } //主菜单 int mainmenu() { system("cls"); cout << "1,你想我猜" << endl; cout << "2,我想你猜" << endl; cout << "3,排行榜" << endl; cout << "4,退出" << endl; int i; cin >> i; switch (i) { case 1: game1(); break; case 2: game2(); break; case 3: paihangbang(); break; case 4: return 0; default: cout << "输入错误!请重新输入" << endl; system("pause"); break; } return 1; } //游戏1本体 void game1() { system("cls"); cout << "请想一个不重复的四位数,如 0123 并对电脑提出的数字进行反馈,如输入 2A1B 表示有两个数字位置正确,一个数字位置不对但是在四位数中" << endl; system("pause"); cout << "0123" << endl; string i;//用于接收 string j = "0123456789"; string k = "0123"; int A = 0, B = 0;//用于保存对应的数字 cin >> i; /* while ((A == 4 && B == 0)==0)//结束条件 { while (isgam1available(&i, &A, &B));//当返回值是0的时候,将会跳出,进行下一步,当返回值是1时,会继续执行这个子函数。 } */ while (isgam1available(&i, &A, &B));//当返回值是0的时候,将会跳出,进行下一步,当返回值是1时,会继续执行这个子函数。 int C = A + B; switch (C) { case 0: switch0(C, A, B, i, j, k); break; case 1: switch1(C, A, B, i, j, k); break; case 2: switch2(C, A, B, i, j, k); break; case 3: switch3(C, A, B, i, j, k); break; case 4: break; } //上面的整个过程完结之后,若是C!=4,则是“撒谎”的情况,若是C=4,则进行下一步,对k中的数进行组合,并依照A的值进行操作 if (C == 4) { switch (A) { case 0: cout << k[1] << k[0] << k[2] << k[3] << endl;//2134 gam1get(C, A, B, i); switchA0(C, A, B, i, j, k); break; case 1: cout << k[1] << k[0] << k[2] << k[3] << endl; gam1get(C, A, B, i); switchA1(C, A, B, i, j, k); break; case 2: cout << k[1] << k[0] << k[2] << k[3] << endl; gam1get(C, A, B, i); switchA2(C, A, B, i, j, k); break; case 4: gam1victory(); break; default: gam1errorout(); break; } } return; } void gam1errorout() { cout << "年轻人不讲武德,来骗,我一个老实巴交的电脑,真是太坏了" << endl; system("pause"); return; } void gam1victory() { cout << "那必须是我啦!" << endl; system("pause"); return; } void gam1get(int& C, int& A, int& B, string& i) { cin >> i; while (isgam1available(&i, &A, &B)); C = A + B; return; } int isgam1available(string* j, int* A, int* B) { if ((*j)[1] == 'A' && (*j)[3] == 'B') { *A = (*j)[0] - '0';//数据转换 *B = (*j)[2] - '0'; if ((*A + *B) > 4) { cout << "输入不合法,请输入如输入 2A1B 表示有两个数字位置正确,一个数字位置不对但是在四位数中" << endl; cin >> *j; return 1; } else return 0; } else { cout << "输入不合法,请输入如输入 2A1B 表示有两个数字位置正确,一个数字位置不对但是在四位数中" << endl; cin >> *j; return 1; } } void switch0(int& C, int& A, int& B, string& i, string& j, string& k) { k[0] = j[4]; k[1] = j[5]; k[2] = j[6]; k[3] = j[7]; cout << k << endl; gam1get(C, A, B, i); switch (C) { case 2: switch02(C, A, B, i, j, k); break; case 3: switch03(C, A, B, i, j, k); break; case 4: break; default: gam1errorout(); break; } return; } void switch02(int& C, int& A, int& B, string& i, string& j, string& k) { k[0] = j[4]; k[1] = j[5]; k[2] = j[8]; k[3] = j[9]; cout << k << endl; gam1get(C, A, B, i); switch (C) { case 2: switch (C) { k[0] = j[6]; k[1] = j[7]; k[2] = j[8]; k[3] = j[9]; cout << k << endl; gam1get(C, A, B, i); case 4: break; default: gam1errorout(); break; } break; case 3: switch023(C, A, B, i, j, k); break; case 4: break; default: gam1errorout(); break; } return; } void switch023(int& C, int& A, int& B, string& i, string& j, string& k) { k[0] = j[4]; k[1] = j[5]; k[2] = j[8]; k[3] = j[9]; cout << k << endl; gam1get(C, A, B, i); switch (C) { case 2: k[0] = j[5]; k[1] = j[7]; k[2] = j[8]; k[3] = j[9]; cout << k << endl; gam1get(C, A, B, i); switch (C) { case 4: break; default: gam1errorout(); break; } case 3: k[0] = j[4]; k[1] = j[7]; k[2] = j[8]; k[3] = j[9]; cout << k << endl; gam1get(C, A, B, i); switch (C) { case 2: k[0] = j[5]; k[1] = j[6]; k[2] = j[8]; k[3] = j[9]; cout << k << endl; gam1get(C, A, B, i); switch (C) { case 4: break; default: gam1errorout(); break; } break; case 4: break; default: gam1errorout(); break; } break; case 4: break; default: break; } return; } void switch03(int& C, int& A, int& B, string& i, string& j, string& k) { k[0] = j[4]; k[1] = j[5]; k[2] = j[6]; k[3] = j[8]; cout << k << endl; gam1get(C, A, B, i); switch (C) { case 2: switch032(C, A, B, i, j, k); break; case 3: switch033(C, A, B, i, j, k); break; case 4: break; default: gam1errorout(); break; } return; } void switch032(int& C, int& A, int& B, string& i, string& j, string& k) { k[0] = j[4]; k[1] = j[5]; k[2] = j[7]; k[3] = j[8]; cout << k << endl; gam1get(C, A, B, i); switch (C) { case 2://j67是 k[0] = j[4]; k[1] = j[6]; k[2] = j[7]; k[3] = j[9]; cout << k << endl; gam1get(C, A, B, i); switch (C) { case 3: k[0] = j[5]; k[1] = j[6]; k[2] = j[7]; k[3] = j[9]; cout << k << endl; gam1get(C, A, B, i); switch (C) { case 4: break; default: gam1errorout(); break; } break; case 4: break; default: gam1errorout(); break; } break; case 3: k[0] = j[4]; k[1] = j[6]; k[2] = j[7]; k[3] = j[8]; cout << k << endl; gam1get(C, A, B, i); switch (C) { case 3: k[0] = j[5]; k[1] = j[6]; k[2] = j[7]; k[3] = j[8]; cout << k << endl; gam1get(C, A, B, i); switch (C) { case 4: break; default: gam1errorout(); break; } break; case 4: break; default: gam1errorout(); break; } break; case 4: break; default: gam1errorout(); break; } return; } void switch033(int& C, int& A, int& B, string& i, string& j, string& k) { k[0] = j[4]; k[1] = j[5]; k[2] = j[6]; k[3] = j[9]; cout << k << endl; gam1get(C, A, B, i); switch (C) { case 2: k[0] = j[4]; k[1] = j[5]; k[2] = j[7]; k[3] = j[8]; cout << k << endl; gam1get(C, A, B, i); switch (C) { case 3: k[0] = j[4]; k[1] = j[6]; k[2] = j[7]; k[3] = j[8]; cout << k << endl; gam1get(C, A, B, i); switch (C) { case 3: k[0] = j[5]; k[1] = j[6]; k[2] = j[7]; k[3] = j[8]; cout << k << endl; gam1get(C, A, B, i); switch (C) { case 4: break; default: gam1errorout(); break; } break; case 4: break; default: gam1errorout(); break; } break; case 4: break; default: gam1errorout(); break; } break; case 4: break; default: gam1errorout(); break; } return; } void switch1(int& C, int& A, int& B, string& i, string& j, string& k) { k[0] = j[4]; k[1] = j[5]; k[2] = j[6]; k[3] = j[7]; cout << k << endl; gam1get(C, A, B, i); switch (C) { case 1: switch11(C, A, B, i, j, k); break; case 2: switch12(C, A, B, i, j, k); break; case 3: switch13(C, A, B, i, j, k); break; default: gam1errorout(); break; } return; } void switch11(int& C, int& A, int& B, string& i, string& j, string& k) { //这里的策略是先对4567中的那个找出来,然后对0123进行遍历 k[0] = j[4]; k[1] = j[5]; k[2] = j[8]; k[3] = j[9]; cout << k << endl; gam1get(C, A, B, i); switch (C) { case 2: switch112(C, A, B, i, j, k); break; case 3: switch113(C, A, B, i, j, k); break; default: gam1errorout(); break; } return; } void switch112(int& C, int& A, int& B, string& i, string& j, string& k) { k[0] = j[4]; k[1] = j[6]; cout << k << endl; gam1get(C, A, B, i); switch (C) { case 2: k[1] = j[7]; cout << k << endl; gam1get(C, A, B, i); switch (C) { case 3: switch112_1(C, A, B, i, j, k);//这个函数将会只对k的第一位进行修改 break; default: gam1errorout(); break; } break; case 3: switch112_1(C, A, B, i, j, k); break; default: gam1errorout(); break; } return; } void switch112_1(int& C, int& A, int& B, string& i, string& j, string& k)//只对k[0]进行修改,最多四次即可判断 { k[0] = j[0]; cout << k << endl; gam1get(C, A, B, i); switch (C) { case 3: k[0] = j[1]; cout << k << endl; gam1get(C, A, B, i); switch (C) { case 3: k[0] = j[2]; cout << k << endl; gam1get(C, A, B, i); switch (C) { case 3: k[0] = j[3]; cout << k << endl; gam1get(C, A, B, i); switch (C) { case 4: break; default: gam1errorout(); break; } break; case 4: break; default: gam1errorout(); break; } break; case 4: break; default: gam1errorout(); break; } break; case 4: break; default: gam1errorout(); break; } return; } void switch113(int& C, int& A, int& B, string& i, string& j, string& k) { k[0] = j[4]; k[1] = j[6]; cout << k << endl; gam1get(C, A, B, i); switch (C) { case 2: k[1] = j[5]; switch112_1(C, A, B, i, j, k); break; case 3: k[1] = j[4]; switch112_1(C, A, B, i, j, k); break; default: gam1errorout(); break; } return; } void switch12(int& C, int& A, int& B, string& i, string& j, string& k) { k[0] = j[4]; k[1] = j[5]; k[2] = j[6]; k[3] = j[8]; cout << k << endl; gam1get(C, A, B, i); switch (C) { case 1://这种情况7,9肯定有,从j456位选一个 k[2] = j[7]; k[3] = j[9]; switch123_1(C, A, B, i, j, k); break; case 2: k[0] = j[4]; k[1] = j[5]; k[2] = j[6]; k[3] = j[9]; cout << k << endl; gam1get(C, A, B, i); switch (C) { case 1://7,8肯定有从j456选一个 k[2] = j[7]; k[3] = j[8]; switch123_1(C, A, B, i, j, k); break; case 3://9肯定有,j7不是从j456选二 k[3] = j[9]; switch123_2(C, A, B, i, j, k); break; default: gam1errorout(); break; } break; case 3://有8,j7不是456选二 k[3] = j[8]; switch123_2(C, A, B, i, j, k); break; case 4: break; default: gam1errorout(); break; } return; } void switch123_1(int& C, int& A, int& B, string& i, string& j, string& k)//已知k23是,从j456选择一个 { k[0] = j[4]; k[1] = j[5]; cout << k << endl; gam1get(C, A, B, i); switch (C) { case 2://说明j6是 k[1] = j[6]; switch112_1(C, A, B, i, j, k); break; case 3: k[1] = j[6]; cout << k << endl; gam1get(C, A, B, i); switch (C) { case 2://说明j5是 k[1] = j[5]; switch112_1(C, A, B, i, j, k); break; case 3://说明j4是 k[1] = j[4]; switch112_1(C, A, B, i, j, k); break; default: gam1errorout(); break; } break; default: gam1errorout(); break; } return; } void switch123_2(int& C, int& A, int& B, string& i, string& j, string& k)//已知k3是,j7不是,从j456选择两个 { k[0] = j[7]; k[1] = j[4]; k[2] = j[5]; cout << k << endl; gam1get(C, A, B, i); switch (C) { case 2://j6是 k[2] = j[6]; cout << k << endl; gam1get(C, A, B, i); switch (C) { case 2://j5是 k[1] = j[5]; switch112_1(C, A, B, i, j, k); break; case 3://j4是 switch112_1(C, A, B, i, j, k); break; default: gam1errorout(); break; } break; case 3://j45是 switch112_1(C, A, B, i, j, k); break; default: gam1errorout(); break; } return; } void switch13(int& C, int& A, int& B, string& i, string& j, string& k) { k[0] = j[8]; k[1] = j[4]; k[2] = j[5]; k[3] = j[6]; cout << k << endl; gam1get(C, A, B, i); switch (C) { case 2://j7是 k[3] = j[7];//此时k=8457 cout << k << endl; gam1get(C, A, B, i); switch (C) { case 2://j6是 k[2] = j[6];//此时k=8467 cout << k << endl; gam1get(C, A, B, i); switch (C) { case 2://j567是 k[1] = j[5];//k=8567 switch112_1(C, A, B, i, j, k); break; case 3://j467是 switch112_1(C, A, B, i, j, k); break; default: gam1errorout(); break; } break; case 3://j457是 switch112_1(C, A, B, i, j, k); break; default: gam1errorout(); break; } break; case 3://j456都是 switch112_1(C, A, B, i, j, k); break; default: gam1errorout(); break; } return; } void switch2(int& C, int& A, int& B, string& i, string& j, string& k) { k[0] = j[4]; k[1] = j[5]; k[2] = j[6]; k[3] = j[7]; cout << k << endl; gam1get(C, A, B, i); switch (C) { case 0://与0,2,2同理 j = "4567012389"; switch02(C, A, B, i, j, k); break; case 1://与1,2,1同理 j = "4567012389"; switch12(C, A, B, i, j, k); break; case 2: switch22(C, A, B, i, j, k); break; default: gam1errorout(); break; } return; } void switch22(int& C, int& A, int& B, string& i, string& j, string& k)//具体思路是先找两个再找两个,两次的程序差别不大,减少工作量 { k[0] = j[4]; k[1] = j[5]; k[2] = j[8]; k[3] = j[9]; cout << k << endl; gam1get(C, A, B, i); switch (C) { case 0://j67是 switch22_1(C, A, B, i, j, k); break; case 1: k[1] = j[6];//k=4689 cout << k << endl; gam1get(C, A, B, i); switch (C) { case 0://j57是 switch22_1(C, A, B, i, j, k); break; case 1: k[1] = j[7];//k=4789 cout << k << endl; gam1get(C, A, B, i); switch (C) { case 0://j56是 switch22_1(C, A, B, i, j, k); break; case 2://j47是 switch22_1(C, A, B, i, j, k); break; default: gam1errorout(); break; } break; case 2://j46是 switch22_1(C, A, B, i, j, k); break; default: gam1errorout(); break; } break; case 2://j45是 switch22_1(C, A, B, i, j, k); break; default: gam1errorout(); break; } return; } void switch22_1(int& C, int& A, int& B, string& i, string& j, string& k) { j = "4567012389"; k[2] = j[4]; k[3] = j[5]; cout << k << endl; gam1get(C, A, B, i); switch (C) { case 2://j67是 k[2] = j[6]; k[3] = j[7]; switch (C) { case 4: break; default: gam1errorout(); break; } break; case 3: k[3] = j[6];//k=4689 cout << k << endl; gam1get(C, A, B, i); switch (C) { case 2://j57是 k[2] = j[5]; k[3] = j[7]; switch (C) { case 4: break; default: gam1errorout(); break; } break; case 3: k[3] = j[7];//k=4789 cout << k << endl; gam1get(C, A, B, i); switch (C) { case 2://j56是 k[2] = j[5]; k[3] = j[6]; switch (C) { case 4: break; default: gam1errorout(); break; } break; case 4://j47是 break; default: gam1errorout(); break; } break; case 4://j46是 break; default: gam1errorout(); break; } break; case 4://j45是 break; default: gam1errorout(); break; } return; } void switch3(int& C, int& A, int& B, string& i, string& j, string& k) { k[0] = j[4]; k[1] = j[5]; k[2] = j[6]; k[3] = j[7]; cout << k << endl; gam1get(C, A, B, i); switch (C) { case 0://同0,3,1 j = "4567012389"; switch03(C, A, B, i, j, k); break; case 1://同1,3,0 j = "4567012389"; switch13(C, A, B, i, j, k); break; default: gam1errorout(); break; } return; } void switchA0(int& C, int& A, int& B, string& i, string& j, string& k) { if (C != 4 || A == 3) { gam1errorout(); return; } else { switch (A) { case 0: cout << k[2] << k[1] << k[0] << k[3] << endl;//3214 gam1get(C, A, B, i); switchA00(C, A, B, i, j, k); break; case 1: cout << k[2] << k[1] << k[0] << k[3] << endl;//3214 gam1get(C, A, B, i); switchA01(C, A, B, i, j, k); break; case 2: cout << k[1] << k[0] << k[3] << k[2] << endl;//2143 gam1get(C, A, B, i); switch (A) { case 4: gam1victory(); break; default: gam1errorout(); break; } break; default: gam1errorout(); break; } } return; } void switchA00(int& C, int& A, int& B, string& i, string& j, string& k) { if (C != 4 || A == 3) { gam1errorout(); return; } else { switch (A) { case 0: cout << k[3] << k[2] << k[1] << k[0] << endl;//4321 gam1get(C, A, B, i); switch (A) { case 4: gam1victory(); break; default: gam1errorout(); break; } break; case 1: cout << k[2] << k[3] << k[1] << k[0] << endl;//3421 gam1get(C, A, B, i); switchA001(C, A, B, i, j, k); break; case 2: cout << k[2] << k[3] << k[0] << k[1] << endl;//3412 gam1get(C, A, B, i); switch (A) { case 4: gam1victory(); break; default: gam1errorout(); break; } break; default: gam1errorout(); break; } } return; } void switchA001(int& C, int& A, int& B, string& i, string& j, string& k) { if (C != 4 || A == 3) { gam1errorout(); return; } else { switch (A) { case 0: cout << k[3] << k[2] << k[0] << k[1] << endl;//4312 gam1get(C, A, B, i); switch (A) { case 4: gam1victory(); break; default: gam1errorout(); break; } break; case 4: gam1victory(); break; default: gam1errorout(); break; } } return; } void switchA01(int& C, int& A, int& B, string& i, string& j, string& k) { if (C != 4 || A == 3) { gam1errorout(); return; } else { switch (A) { case 0: cout << k[1] << k[2] << k[3] << k[0] << endl;//2341 gam1get(C, A, B, i); switch (A) { case 0: cout << k[3] << k[0] << k[1] << k[2] << endl;//4123 gam1get(C, A, B, i); switch (A) { case 4: gam1victory(); break; default: gam1errorout(); break; } break; case 4: gam1victory(); break; default: gam1errorout(); break; } break; case 1: cout << k[1] << k[3] << k[0] << k[2] << endl;//2413 gam1get(C, A, B, i); switch (A) { case 0: cout << k[2] << k[0] << k[3] << k[1] << endl;//3142 gam1get(C, A, B, i); switch (A) { case 4: gam1victory(); break; default: gam1errorout(); break; } break; case 4: gam1victory(); break; default: gam1errorout(); break; } break; default: gam1errorout(); break; } } return; } void switchA1(int& C, int& A, int& B, string& i, string& j, string& k) { if (C != 4 || A == 3) { gam1errorout(); return; } else { switch (A) { case 0: cout << k[2] << k[1] << k[0] << k[3] << endl;//3214 gam1get(C, A, B, i); switchA10(C, A, B, i, j, k); break; case 2: cout << k[2] << k[1] << k[0] << k[3] << endl;//3214 gam1get(C, A, B, i); switchA12(C, A, B, i, j, k); break; default: gam1errorout(); break; } } return; } void switchA10(int& C, int& A, int& B, string& i, string& j, string& k) { if (C != 4 || A == 3) { gam1errorout(); return; } else { switch (A) { case 0: cout << k[0] << k[2] << k[3] << k[1] << endl;//1342 gam1get(C, A, B, i); switch (A) { case 1: cout << k[0] << k[3] << k[1] << k[2] << endl;//1423 gam1get(C, A, B, i); switch (A) { case 4: gam1victory(); break; default: gam1errorout(); break; } break; case 4: gam1victory(); break; default: gam1errorout(); break; } break; case 2: cout << k[2] << k[1] << k[3] << k[0] << endl;//3241 gam1get(C, A, B, i); switch (A) { case 1: cout << k[3] << k[1] << k[0] << k[2] << endl;//4213 gam1get(C, A, B, i); switch (A) { case 4: gam1victory(); break; default: gam1errorout(); break; } break; case 4: gam1victory(); break; default: gam1errorout(); break; } break; default: gam1errorout(); break; } } return; } void switchA12(int& C, int& A, int& B, string& i, string& j, string& k) { switch (A) { case 0: cout << k[1] << k[3] << k[2] << k[0] << endl;//2431 gam1get(C, A, B, i); switch (A) { case 1: cout << k[3] << k[0] << k[2] << k[1] << endl;//4132 gam1get(C, A, B, i); switch (A) { case 4: gam1victory(); break; default: gam1errorout(); break; } break; case 4: gam1victory(); break; default: gam1errorout(); break; } break; case 1: cout << k[2] << k[0] << k[1] << k[3] << endl;//3124 gam1get(C, A, B, i); switch (A) { case 4: gam1victory(); break; default: gam1errorout(); break; } break; case 2: cout << k[1] << k[2] << k[0] << k[3] << endl;//2314 gam1get(C, A, B, i); switch (A) { case 4: gam1victory(); break; default: gam1errorout(); break; } break; default: gam1errorout(); break; } return; } void switchA2(int& C, int& A, int& B, string& i, string& j, string& k) { switch (A) { case 0: cout << k[0] << k[1] << k[3] << k[2] << endl;//1243 gam1get(C, A, B, i); switch (A) { case 4: gam1victory(); break; default: gam1errorout(); break; } break; case 1: cout << k[2] << k[1] << k[0] << k[3] << endl;//3214 gam1get(C, A, B, i); switchA21(C, A, B, i, j, k); break; case 4: gam1victory(); break; default: gam1errorout(); break; } return; } void switchA21(int& C, int& A, int& B, string& i, string& j, string& k) { switch (A) { case 0: cout << k[0] << k[3] << k[2] << k[1] << endl;//1432 gam1get(C, A, B, i); switch (A) { case 4: gam1victory(); break; default: gam1errorout(); break; } break; case 1: cout << k[0] << k[2] << k[1] << k[3] << endl;//1324 gam1get(C, A, B, i); switch (A) { case 0: cout << k[3] << k[1] << k[2] << k[0] << endl;//4231 gam1get(C, A, B, i); switch (A) { case 4: gam1victory(); break; default: gam1errorout(); break; } break; case 4: gam1victory(); break; default: gam1errorout(); break; } break; case 4: gam1victory(); break; default: gam1errorout(); break; } return; } //游戏2本体 void game2() { system("cls"); cout << "游戏开始,电脑已经想出了一个数字,请输入一个不重复的四位数,如 0123 ,若输出 2A1B 表示有两个数字位置正确,一个数字位置不对但是在四位数中" << endl; int i = 0; string j; gam2::resultgam2 = getresultgam2(); for (i = 0; i < 15; i++)//用于控制回合数 { cin >> j; while (isgam2available(&j));//当返回值是0的时候,将会跳出,进行下一步,当返回值是1时,会继续执行这个子函数。 gam2 game = j;//不是很理解这个传值的机制或者说,初始化 if ((gam2::resultgam2).compare(game.getresult()) == 0)//比较两个string类型字符串是否相等 { cout << "恭喜你答对了!" << endl; break; } else { cout << game.compareresult() << endl;//输出反馈 } } if (i >= 15) cout << "很遗憾,未在15回合内猜出正确答案" << endl; system("pause"); return; } //初始化一个gam2的静态变量 string getresultgam2() { char temp[] = "0123456789"; random_shuffle(temp, temp + 10); temp[4] = 0; return temp; } //判断游戏2输入字符是否合法 int isgam2available(string* j) { int count[10] = { 0 };//count[n]表示数字n对应的数组位置的数,如果这个数字出现了不止一次,那么这个位置的数就会自增不止一次,它的数字就会大于1 for (int i = 0; i < 4; i++) { if ((*j)[i] >= '0' && (*j)[i] <= '9')//原本是j[i] { int n = (*j)[i] - '0';//提取数字 count[n]++; } else { cout << "存在非法字符,请重新输入:" << endl; cin >> *j; return 1; } } for (int i = 0; i < 10; i++)//这里的i必须要声明数据类型 { if (count[i] > 1) { cout << "存在重复数字,请输入含不重复数字的四位数,如0123:" << endl; cin >> *j; return 1; } } return 0; } void paihangbang() { system("cls"); cout << "其实这只是一个摆设,想不到吧" << endl; system("pause"); return; }
标签:case,cout,int,void,gam2,小游戏,string 来源: https://www.cnblogs.com/xjbhxcz/p/16267800.html