跳到正文

个人记录

数字华容道

用户 767762约 7 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年4月25日
篇幅
约 7 分钟

版本

  1. v1当前
C++
//改编于https://www.luogu.com.cn/blog/Los/creative-code-1
#include<iostream>
#include<windows.h>
#include<conio.h>
#include<ctime>
#define STA SetConsoleTextAttribute
#define GSH GetStdHandle(STD_OUTPUT_HANDLE)

using namespace std;
const int waittime=50;//输出每个字符的间隔时间
int num[10005]={0},n,p[30][30],x,y,sum;

void hidden(bool a){//0隐藏光标,1显示
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO cci;
    GetConsoleCursorInfo(hOut,&cci);
    cci.bVisible=a;
    SetConsoleCursorInfo(hOut,&cci);
    return;
}
void color(int x) {
    STA(GSH,x);
}
void show(const char* p){//缓慢输出语句
    for(;;){
        if (*p!=0)printf("%c",*p++);
        else break;
        Sleep(waittime);
    }
}
bool iswin(){
    int f=1;
    for(int i=1;i<=n-1;i++){
        for(int j=1;j<=n;j++){
            if(p[i][j]!=f)return false;
            f++;
        }
    }
    return true;
}
int main(){
	system("cls");
    SetConsoleTitle("数字华容道v1.0");
	srand(time(0));
	color(10);
	show("数字华容道\n");
	Sleep(200);
	show("请输入行数列数(n<25,n<0退出)\n");
	color(15);
    scanf("%d",&n);
    if(n>25){
    	show("\n输入不合法,请重试");
		Sleep(500);
		main();
	}
    else if(n<0){
    	int a=MessageBox(NULL,"确认退出?","Tips",MB_YESNO);
    	if(a==IDYES)exit(0);
    	else main();
	}
    x=rand()%n+1;
    y=rand()%n+1;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(i==x && j==y)continue;
            int k=rand()%(n*n-1)+1;
            while(num[k])k=rand()%(n*n-1)+1;
            p[i][j]=k;
            num[k]=1;
        }
    }
    for(int i=1;i<=n*n;i++)num[i]=0;
    while(!iswin()){
        char a;
        hidden(0);
        system("cls");
        printf("------------------------------------\n");
        printf("| 你已经移动了%-5dsteps.        |\n",sum);
        printf("|  移动: W A S D     重来: R       |\n");
        printf("-----------------------------------\n");
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                if(i==x && j==y)printf("%-5c",'*');
                else printf("%-5d",p[i][j]);
            }
            cout<<endl;
        }
        a=getch();
        if(a=='w' || a=='W'){
            if(x!=n){
                p[x][y]=p[x+1][y];
                x++;
            }
        }
        if(a=='s' || a=='S'){
            if(x!=1){
                p[x][y]=p[x-1][y];
                x--;
            }
        }
        if(a=='a' || a=='A'){
            if(y!=n){
                p[x][y]=p[x][y+1];
                y++;
            }
        }
        if(a=='d' || a=='D'){
            if(y!=1){
                p[x][y]=p[x][y-1];
                y--;
            }
        }
        if(a=='r' || a=='R'){
            x=rand()%n+1;
            y=rand()%n+1;
            for(int i=1;i<=n;i++){
                for(int j=1;j<=n;j++){
                    if(i==x && j==y)continue;
                    int k=rand()%(n*n-1)+1;
                    while(num[k])k=rand()%(n*n-1)+1;
                    p[i][j]=k;
                    num[k]=1;
                }
            }
            for(int i=1;i<=n*n;i++)num[i]=0;
            sum=0;
        }
        sum++;
    }
    system("cls");
    color(12);
    printf("*-----------------*\n");
    color(13);
   	printf("  Total:%-5d\n",sum);
    printf("  你成功了!    \n");
    color(12);
    printf("*-----------------*\n");
    Sleep(1000);
    main();
}
原文