作业介绍

数字华容道

#include<iostream>
#include<conio.h>
#include<windows.h> 
using namespace std;
int n_puzzle_3[3][3] = {1,2,3,4,5,6,7,8,0};
int n_puzzle[3][3] = {1,3,8,7,0,2,5,6,4};
void outArr() 
{
	for(int i=0;i<3;++i)
	{
		for(int j=0;j<3;++j){
			if(n_puzzle[i][j] != 0){
				cout << n_puzzle[i][j] << " ";
			}
			else{
				cout <<"  ";
			}
		}
		cout<<endl;			
	}	
}
void change(int *one,int *two){
	int temp;
	temp = *one;
	*one = *two;
	*two = temp;
} 
int main(){	
	outArr();
	int x=1,y=1,count=0;
	while(true){				
		cout<<"请输入移动方向(wasd):"<<endl;
		char choose;	
		choose = getch();
		++count;
		if(choose == 'w')
		{
			if(x>=0 && x<2)
			{
				change(&n_puzzle[x][y],&n_puzzle[x+1][y]);
				++x;
			}
		}
		if(choose == 'a')
		{
			if(y>=0 && y<2)
			{
				change(&n_puzzle[x][y],&n_puzzle[x][y+1]);
				++y;
			}
		}
		if(choose == 's')
		{
			if(x>0 && x<=2)
			{
				change(&n_puzzle[x][y],&n_puzzle[x-1][y]);
				--x;
			}
		}
		if(choose == 'd')
		{
			if(y>0 && y<=2)
			{
				change(&n_puzzle[x][y],&n_puzzle[x][y-1]);
				--y;
			}
		}
		system("cls");
		outArr();
		int flag = 1;
		for(int i=0;i<3;++i)
		{
			for(int j=0;j<3;++j){
				if(n_puzzle[i][j] != n_puzzle_3[i][j])
				{
					flag=0;					
				}	
			}
		}
		if(flag == 1)
		{	
			string steps = "完成数字华容道,使用步数为:";			//messagebox显示使用步数 
			char sum[100]; 
			itoa(count,sum,10);							//将记录玩家步数的变量sum,转成字符串,赋值给iToA数组 
			steps += sum; 
			MessageBox(0,steps.c_str(),"提示",MB_OK);//将获得的字符串显示在messagebox上 
			//MessageBox(0,"恭喜你完成数字华容道","提示",MB_OK);
			break; 
		}		
	}			  
	return 0;
}


状态
已结束
题目
3
开始时间
2023-12-1 0:00
截止时间
2023-12-31 23:59
可延期
24 小时