注:作者终于实现了随机产生的关卡都是有解的,如果您遇到了无解情况请在该博客下评论或私信作者。
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <fstream>
using namespace std;
void color(int a){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
void block(int x,int y){
HANDLE hCon;
hCon = GetStdHandle(STD_OUTPUT_HANDLE);
COORD setps;
setps.X = x;
setps.Y = y;
SetConsoleCursorPosition(hCon,setps);
}
ifstream fin;
ofstream fout;
int n, x, y, bu = 0, zx, zy;
int a[15][15];
int a1[15][15];
int a2[15][15];
int shicha = 0;
vector<int> v;
bool h = false;
void output(){
color(15);
block(0, 0);
cout << "当前步数:" << bu << endl;
for(int i = 1; i <= n; i++){
color(15 + 6 * 16);
for(int j = 1; j <= n; j++){
if(a1[i][j] != a[i][j]){
block(j * 3 - 1, i + 1);
if(a[i][j] == 0){
cout << " ";
}else{
printf("%02d", a[i][j]);
}
if(j != n){
cout << " ";
}
a1[i][j] = a[i][j];
}
}
}
block(0, n + 2);
}
bool ying(){
bool hh1 = false;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
if(i == n && j == n){
return true;
}else{
if(a[i][j] != i * n - n + j){
hh1 = true;
break;
}
}
}
if(hh1){
break;
}
}
return false;
}
void yi(int x1){
if(x1 == 0){
if(x == n){
return;
}else{
swap(a[x][y], a[x + 1][y]);
x++;
bu++;
}
}else if(x1 == 1){
if(x == 1){
return;
}else{
swap(a[x][y], a[x - 1][y]);
x--;
bu++;
}
}else if(x1 == 2){
if(y == n){
return;
}else{
swap(a[x][y], a[x][y + 1]);
y++;
bu++;
}
}else{
if(y == 1){
return;
}else{
swap(a[x][y], a[x][y - 1]);
y--;
bu++;
}
}
}
int rand2(int x, int y){
srand(time(NULL));
srand(rand() % 65535 + shicha);
srand(rand() % 65535 + time(NULL));
srand(rand() % 65535 - shicha);
return (rand() % (y - x + 1)) + x;
}
void suii1(){
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
if(i == n && j == n){
a[i][j] = 0;
}else{
a[i][j] = i * n - n + j;
}
}
}
srand(time(NULL));
long long sui1 = rand();
while(sui1 <= 1000000000000000007){
sui1 = sui1 * sui1;
}
for(int i = 1; i <= n / 2; i++){
yi(1);
yi(3);
}
while(sui1 > 0){
int aa1 = sui1 % 4;
sui1 = sui1 / 4;
if(aa1 == 0 && x == n){
aa1 = 1;
}else if(aa1 == 1 && x == 1){
aa1 = 0;
}else if(aa1 == 2 && y == n){
aa1 = 3;
}else if(aa1 == 3 && y == 1){
aa1 = 2;
}
yi(aa1);
}
}
void suii2(){
int az1[110];
int az1l = n * n - 1;
for(int i = 0; i < n * n; i++){
az1[i] = i;
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
shicha += rand2(11, 101);
int azsui1 = rand2(0, az1l);
shicha += rand2(11, 101);
if(shicha >= 60 * 61 * 62 * 63){
shicha -= 60 * 61 * 62 * 63;
}
a[i][j] = az1[azsui1];
bool azh1 = false;
for(int k = 0; k <= az1l; k++){
if(azh1){
az1[k - 1] = az1[k];
}else if(k == azsui1){
azh1 = true;
}
}
az1l--;
}
}
int az2[n + 1][n + 1];
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
az2[i][j] = a[i][j];
if(az2[i][j] == 0){
x = i;
y = j;
}
}
}
for(int i = 1; i <= n; i++){
yi(0);
yi(2);
}
int az3 = 0;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
//output();
//cout << az3 << endl;
//system("pause");
if(i == n && j == n){
continue;
}
if(a[i][j] == i * n + j - n){
continue;
}else{
while(a[i][j] != i * n + j - n){
int az4 = a[i][j];
a[i][j] = a[(az4 - 1) / n + 1][(az4 - 1) % n + 1];
a[(az4 - 1) / n + 1][(az4 - 1) % n + 1] = az4;
//cout << a[(az4 - 1) / n + 1][(az4 - 1) % n + 1] << " " << a[i][j] << endl;
//system("pause");
az3++;
}
}
}
}
if(az3 % 2 == 1){
swap(az2[n - 1][n], az2[n][n - 1]);
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
a[i][j] = az2[i][j];
if(az2[i][j] == 0){
x = i;
y = j;
}
}
}
}
void play2(){
system("cls");
while(true){
output();
fout.close();
fout.clear();
fout.open("数字华容道-存档1.png");
fout << n << " " << bu << " " << x << " " << y << endl;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
fout << a[i][j] << " ";
}
fout << endl;
}
for(int i = 0; i < v.size(); i++){
fout << v[i];
}
fout << "<";
fout << endl;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
fout << a2[i][j] << " ";
}
fout << endl;
}
fout << zx << " " << zy;
if(ying()){
break;
}
char m2 = getch();
if(m2 == '0'){
h = true;
return;
}else if(m2 == 'w'){
v.push_back(0);
yi(0);
}else if(m2 == 's'){
v.push_back(1);
yi(1);
}else if(m2 == 'a'){
v.push_back(2);
yi(2);
}else if(m2 == 'd'){
v.push_back(3);
yi(3);
}
}
fout.close();
fout.clear();
fout.open("数字华容道-存档1.png");
fout << "0 0 0 0" << endl;
fin.close();
fin.clear();
fin.open("数字华容道-存档2.jpg");
int zz1 = 0;
fin >> zz1;
char zz9;
int zz10 = 0;
int zz11 = 0;
if(zz1 == 0){
fout.close();
fout.clear();
fin.close();
fin.clear();
fin.open("数字华容道-存档3.bmp");
fout.open("数字华容道-存档2.jpg");
fin >> zz1;
zz11 = zz1;
fout << zz1 + 1 << endl;
for(int i = 0; i < zz11; i++){
fin >> zz10;
fout << zz10 << " ";
fin >> zz1;
fout << zz1 << " ";
fin >> zz1;
fout << zz1 << endl;
for(int i = 1; i <= zz10; i++){
for(int j = 1; j <= zz10; j++){
fin >> zz1;
fout << zz1 << " ";
}
fout << endl;
}
fin >> zz9;
while(zz9 != '<'){
if(zz9 >= '0' && zz9 <= '3'){
fout << zz9;
}
fin >> zz9;
}
fout << zz9 << endl;
}
fout << n << " " << zx << " " << zy << endl;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
fout << a2[i][j] << " ";
}
fout << endl;
}
for(int i = 0; i < v.size(); i++){
fout << v[i];
}
fout << "< " << endl;
fout.close();
fout.clear();
fout.open("数字华容道-存档3.bmp");
fout << "0";
}else{
fout.close();
fout.clear();
fout.open("数字华容道-存档3.bmp");
zz11 = zz1;
fout << zz1 + 1 << endl;
for(int i = 0; i < zz11; i++){
fin >> zz10;
fout << zz10 << " ";
fin >> zz1;
fout << zz1 << " ";
fin >> zz1;
fout << zz1 << endl;
for(int i = 1; i <= zz10; i++){
for(int j = 1; j <= zz10; j++){
fin >> zz1;
fout << zz1 << " ";
}
fout << endl;
}
fin >> zz9;
while(zz9 != '<'){
if(zz9 >= '0' && zz9 <= '3'){
fout << zz9;
}
fin >> zz9;
}
fout << zz9 << endl;
}
fout << endl;
fout << n << " " << zx << " " << zy << endl;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
fout << a2[i][j] << " ";
}
fout << endl;
}
for(int i = 0; i < v.size(); i++){
fout << v[i];
}
fout << "< " << endl;
fout.close();
fout.clear();
fout.open("数字华容道-存档2.jpg");
fout << "0";
}
color(15);
cout << endl << " 恭喜获胜!" << endl;
Sleep(3000);
return;
}
void play(){
p2:system("cls");
cout << "游戏开始" << endl;
cout << "请输入生成形式" << endl;
cout << " 1 在“数字华容道.txt中读入" << endl;
cout << " 2 随机模式1" << endl;
cout << " 3 随机模式2" << endl;
char m1 = getch();
if(m1 == '1'){
ifstream fin("数字华容道-存档1.png");
int m3;
fin >> m3;
if(m3 != n){
cout << "阶数不对" << endl;
goto p2;
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
fin >> a[i][j];
if(a[i][j] == 0){
x = i;
y = j;
}
}
}
}else if(m1 == '2'){
suii1();
}else if(m1 == '3'){
suii2();
}else{
goto p2;
}
bu = 0;
system("cls");
memset(a1, -1, sizeof(a1));
v.clear();
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
a2[i][j] = a[i][j];
}
}
zx = x;
zy = y;
play2();
}
int main( ){
system("mode con cols=50 lines=20");
p5:color(15);
system("cls");
cout << "欢迎来到 XTW 蒟蒻的数字华容道!"<< endl;
Sleep(750);
cout << "请选择:" << endl;
Sleep(500);
cout << " 1.开始游戏" << endl;
Sleep(200);
cout << " 2.通关记录" << endl;
Sleep(200);
cout << " 3.继续游戏" << endl;
Sleep(200);
cout << " 0.退出" << endl;
Sleep(200);
p1:char m5 = getch();
if(m5 == '1'){
cout << "1";
Sleep(750);
system("cls");
cout << "请输入阶数(2~10)(按回车)" << endl;
cin >> n;
if(n < 2 || n > 10){
cout << "输入有误" << endl;
Sleep(500);
system("cls");
goto p1;
}
p3:x = n;
y = n;
Sleep(1000);
play();
if(h){
return 0;
}
color(15);
system("cls");
cout << "是否再来一局?" << endl;
Sleep(300);
cout << " Y.是 N.否" << endl;
Sleep(150);
char m4 = getch();
if(m4 == 'n' || m4 == 'N' || m4 == '0'){
goto p5;
}else{
goto p3;
}
}else if(m5 == '2'){
cout << "2";
Sleep(750);
system("cls");
fin.close();
fin.clear();
fin.open("数字华容道-存档2.jpg");
int zz2 = 0;
fin >> zz2;
if(zz2 == 0){
fin.close();
fin.clear();
fin.open("数字华容道-存档3.bmp");
fin >> zz2;
}
if(zz2 == 0){
cout << "你……确定???" << endl;
Sleep(1050);
goto p5;
}
cout << "一共有" << zz2 << "个记录,";
Sleep(250);
p7:cout << endl << "请选择(按回车):";
int zz3;
cin >> zz3;
if(zz3 < 1 || zz3 > zz2){
goto p7;
}
int zz4 = 0;
while(zz4 < zz3){
if(zz4 % 40 == 0){
system("cls");
cout << "加载中";
}else{
cout << "<";
}
fin >> n >> x >> y;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
fin >> a[i][j];
}
}
v.clear();
char zz5;
fin >> zz5;
while(zz5 != '<'){
v.push_back(zz5 - '0');
fin >> zz5;
}
zz4++;
}
system("cls");
cout << "加载完成!" << endl;
system("pause");
memset(a1, -1, sizeof(a1));
bu = 0;
system("cls");
output();
for(int i = 0; i < v.size(); i++){
color(15);
for(int j = 0; j < i % 5; j++){
cout << " " << endl;
}
system("pause");
for(int j = i % 5 + 1; j < 5; j++){
cout << " " << endl;
}
yi(v[i]);
output();
}
color(15);
for(int j = 0; j < 5; j++){
cout << " " << endl;
}
Sleep(1050);
system("pause");
goto p5;
}else if(m5 == '3'){
cout << "3";
Sleep(750);
fin.close();
fin.clear();
fin.open("数字华容道-存档1.png");
fin >> n >> bu >> x >> y;
if(n == 0){
system("cls");
cout << "你……确定???";
Sleep(1050) ;
goto p5;
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
fin >> a[i][j];
}
}
v.clear();
char m6;
fin >> m6;
while(m6 != '<'){
if(m6 >= '0' && m6 <= '3'){
v.push_back(m6 - '0');
}
fin >> m6;
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
fin >> a2[i][j];
}
}
fin >> zx >> zy;
memset(a1, -1, sizeof(a1));
play2();
bool m8 = false;
p6:if(m8){
x = n;
y = n;
play();
m8 = false;
}
if(h){
return 0;
}
color(15);
system("cls");
cout << "是否再来一局?" << endl;
Sleep(300);
cout << " Y.是 N.否" << endl;
Sleep(150);
char m7 = getch();
if(m7 == 'n' || m7 == 'N' || m7 == '0'){
goto p5;
}else{
m8 = true;
goto p6;
}
}else if(m5 == '0'){
return 0;
}else{
goto p5;
}
return 0;
}附录:生成形式 1 的格式:
4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 0 15