MAN!的随机修养void hard::autocreate() // 获得随机数,得到x,y坐标,写入地图{ int pos4 = 0; int autopost[5] = {}; // 随机到了那副拼图 int autoposi[8] = {}; // 记录哪副没有被随机,方便check int onerand = 0; for (;;) { pos4 = rand() % 36 + 1; for (int i = 1; i <= 4; i++) { for (;;) { onerand = rand() % 7 + 1; if (recheck(autopost, onerand)) break; } autopost[i] = onerand; // 1~7 autoposi[autopost[i]]++; } // 得到一个不重复的数组 autochoice(autopost, pos4); // 对以上四个解得到结果 for (int i = 1; i <= 7; i++) { if (autoposi[i] != 1) { int erand = rand() % (number[i - 1].size()); putsolution(erand, i - 1); // 得到随机数 } } auto_map(re_solution()); if (endcheck(re_map())) { mark(re_map()); break; } else { for (int i = 1; i <= 7; i++) { autoposi[i] = 0; } delework(); } }} // 先得到随机的要合成“4”的拼图和位置,然后随机剩余的,最后根据随机的记录生成work。void hard::autochoice(int* autopost, int pos4){ // 以pos4作为能覆盖区域的最大值,最小值选择 for (int i = 1; i <= 4; i++) // 判断choice是否覆盖 { int findmax = checkchoice(number[autopost[i] - 1].data(), number[autopost[i] - 1].size() - 1, pos4); // 拿到最大位置 int findmin = 0; // 得到最小位置 int erand = 0; // 最后的随机 switch (autopost[i]) { case 7: case 6: erand = pos4; putsolution(erand, autopost[i] - 1); // 7,6在number中的上下限为1 break; case 5: case 4: // 重构得到最小位置 findmin = findmax - 6; if (findmin < 0) findmin = 0; // 移动到指定位置(左上角 erand = rand() % (findmax - findmin) + findmin; putsolution(erand, autopost[i] - 1); break; case 3: case 2: findmin = findmax - 4; // 移动到指定位置(左上角 if (findmin < 0) findmin = 0; erand = rand() % (findmax - findmin) + findmin; putsolution(erand, autopost[i] - 1); break; case 1: // 重构得到最小位置 findmin = findmax - 4; if (findmin < 0) findmin = 0; // 移动到指定位置(左上角 erand = rand() % (findmax - findmin) + findmin; putsolution(erand, autopost[i] - 1); break; } }}int hard::checkchoice(int* a, int end, int c) // 得到位置“4”的地点{ // 二分查找 int str = 0; int empt = 0; for (; str <= end;) { int min = (str + end) / 2; if (a[min] <= c) { str = min + 1; empt = min; } else end = min - 1; } return empt;}bool hard::recheck(int* post,int n){ for (int i = 1; i <= 4; i++) { if (post[i]==n) { return false; } } return true;}bool hard::endcheck(char* work){ int count = 0; for (int i = 0; i < 36; i++) { if (work[i] > '4') return false; else if (work[i] == '4') count++; } if (count != 1) return false; else return true;}void hard::mark(char* a){ for (int i = 0; i < 36; i++) { switch (a[i]) { case '0': case '2': a[i] = 'O'; break; case '1': case '3': a[i] = 'A'; break; case '4': a[i] = '%'; break; } }}void hard::custom(char* work){ for (int i = 0; i < 36; i++) { cin >> work[i]; } cout << "输入完毕" << endl; cout << "现在你可以进行游戏了" << endl;}void hard::sto_auto(){ ifstream ifs("E:/visual stidue/programer/Plant/autocreate.txt", ios::out); ofstream ofs("temp.txt", ios::out); // 创建一个临时文件 if (!ofs.is_open()||!ifs.is_open()) { cout << "无法打开文件" << endl; } int max = 0; ifs >> max; max++; ofs << max<<"\n"; char empt; int num = 0; for (int i = 0; i <max-1; i++) { for (int j = 0; j < 36; j++) { ifs >> empt; ofs << empt; } for (int j = 0; j < 7; j++) { ofs << " "; ifs >> num; ofs << num; } ofs << "\n"; } char* work = re_map(); for (int i = 0; i < 36; i++) { ofs << work[i]; } int *solutions = re_solution(); for (int i = 0; i < 7; i++) { ofs << " "; ofs << solutions[i]<<" "; } ofs << "\n"; // 要写入的新数据 // 关闭文件 ofs.close(); ifs.close(); remove("E:/visual stidue/programer/Plant/autocreate.txt"); rename("temp.txt", "E:/visual stidue/programer/Plant/autocreate.txt");} 前一部分为随机算法,后一部分为更新文件内容 MAN!的演示4OOOOOAAAAOOA%AAOOAAAAOOOOOOOOAAAAOOO 5 5 6 6 25 23 12OOOOOOOOOOAAOOAOAAOOAAOAAAOAOAO%OOOO 7 10 12 24 21 31 31OOOOOOOOOOOOAOOOOOAOAAOAOAAAOA%AAAOA 7 12 13 23 20 30 30OOOOOOOOOOOOAAOOOOAAOOOOA%OAAOAAAAAA 3 14 12 17 15 18 25 文件现状开始运行接下来系统将生成随机地图打印地图- - - - - - - - - - - - - - - - - - -| - | - | - | - | - | - || 0 O | 0 O | 0 O | 0 O | 0 O | 0 O |- - - - - - - - - - - - - - - - - -| - | - | - | - | - | - || 0 O | 0 O | 0 O | 0 O | 0 O | 0 O |- - - - - - - - - - - - - - - - - - -| - | - | - | - | - | - || 0 A | 0 O | 0 A | 0 A | 0 O | 0 O |- - - - - - - - - - - - - - - - - -| - | - | - | - | - | - || 0 O | 0 A | 0 % | 0 A | 0 A | 0 O |- - - - - - - - - - - - - - - - - - -| - | - | - | - | - | - || 0 A | 0 O | 0 A | 0 O | 0 O | 0 O |- - - - - - - - - - - - - - - - - -| - | - | - | - | - | - || 0 A | 0 O | 0 O | 0 A | 0 A | 0 O |- - - - - - - - - - - - - - - - - -开始做答吧打印拼图---------------------------------------------------------------* 1 * 2 * 3 * 4 * 5 * 6 * 7* **** * *** * *** * ** * ** * ** **** * *** * *** * ** * ** * * * ** **** * *** * *** * * * *---------------------------------------------------------------那么,在这第1次放置,你要选择哪个拼图?5OOOOOAAAAOOA%AAOOAAAAOOOOOOOOAAAAOOO 5 5 6 6 25 23 12OOOOOOOOOOAAOOAOAAOOAAOAAAOAOAO%OOOO 7 10 12 24 21 31 31OOOOOOOOOOOOAOOOOOAOAAOAOAAAOA%AAAOA 7 12 13 23 20 30 30OOOOOOOOOOOOAAOOOOAAOOOOA%OAAOAAAAAA 3 14 12 17 15 18 25OOOOOOOOOOOOAOAAOOOA%AAOAOAOOOAOOAAO 5 13 14 18 14 12 22 现在的文件