jhxs1516
(Öben Gombrich)
1
#include <bits/stdc++.h>
#include <windows.h>
#include <conio.h>
using namespace std;
char wasd;
int n,a[1005][1005],b[1005][1005];
struct node{
int x,y;
}o;
void chushihua(){
o.x=o.y=n-1;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
a[i][j]=i*n+(j+1);
b[i][j]=i*n+(j+1);
}
}
a[n-1][n-1]=0;
}
void shuchu(){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++)
if(a[i][j]==0) printf("* ");
else printf("%-4d",a[i][j]);
cout<<"\n\n";
}
}
void daluan1(){
int wasd2=rand()%5;
while(wasd2==0) wasd2=rand()%(5);
if(wasd2==1&&o.x>0){
swap(a[o.x][o.y],a[o.x--][o.y]);
}else if(wasd2==2&&o.x<n-1){
swap(a[o.x][o.y],a[o.x++][o.y]);
}else if(wasd2==3&&o.y>0){
swap(a[o.x][o.y],a[o.x][o.y--]);
}else if(wasd2==4&&o.y<n-1){
swap(a[o.x][o.y],a[o.x][o.y++]);
}
}
void daluan(int i){
for(int j=0;j<i;j++){
daluan1();
}
}
void duru(){
wasd=getch();
if((wasd=='w'||wasd=='W')&&o.x>0){
swap(a[o.x][o.y],a[o.x--][o.y]);
}else if((wasd=='s'||wasd=='S')&&o.x<n-1){
swap(a[o.x][o.y],a[o.x++][o.y]);
}else if((wasd=='a'||wasd=='A')&&o.y>0){
swap(a[o.x][o.y],a[o.x][o.y--]);
}else if((wasd=='d'||wasd=='D')&&o.y<n-1){
swap(a[o.x][o.y],a[o.x][o.y++]);
}
}
void gotoxy(int x, int y) {
COORD pos = {x,y};
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, pos);
return ;
}
void tishi(){
cout<<"\"*\"表示空格\n";
cout<<"按wasd控制\"*\"的走向\n";
cout<<"祝你玩的愉快\n";
}
int main(){
cin>>n;
srand(time(0));
chushihua();
daluan(n*n*n*n);
while(true){
gotoxy(0,0);
tishi();
shuchu();
duru();
}
return 0;
}