题没做出来,华容道游戏却做出来了
查看原帖
题没做出来,华容道游戏却做出来了
1032391
封禁用户楼主2024/1/14 11:44

代码如下

#include<bits/stdc++.h>
#include<windows.h>
using namespace std;
void SetColor(int x,int y){//颜色调节器
	WORD wColor=((y&0x0F)<<4)+(x&0x0F);
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),wColor);
}
void Simple();//简单模式 
void Normal();//普通模式 
void Hard();  //困难模式 
int main()//主函数
{
	int sum;
	system("color 07");
	cout<<"Hello challenger."<<endl;
	cout<<"Please select mode:"<<endl;
	cout<<"\nEnter 1 for easy mode, enter 2 for normal mode, and enter 3 for hard mode."<<endl;
	cout<<"The rules of the game are as follows:\n";
	cout<<"\nSimple mode: 3*3 square matrix, \nnormal mode: 4*4 square matrix,\nhard mode: 3*3 square matrix, but there is a limit on the number of times."<<endl<<endl;
	cin>>sum;
	SetColor(0,15);
	cout<<"Games start!"<<endl<<endl;
	if(sum==1) Simple();
	else if(sum==2) Normal();
	else if(sum==3) Hard();
	else {
	SetColor(0,15);
	cout<<"Invalid input, please restart the game!";
	SetColor(15,0);	
	}
	return 0;
}

void Simple(){//简单模式
    int x=1,y=1;
    char a[10][10]={{'A','C','B'},{'E','*','F'},{'G','D','H'}};//理解透的可以自行更改 
    char s; 	
    SetColor(0,15);//初始场景 
    cout<<"please change"<<endl<<endl; 
    for(int i=0;i<3;i++){
	for(int j=0;j<3;j++) cout<<a[i][j]<<' ';
	cout<<endl;	
	} 
	SetColor(0,15);
    cout<<endl<<"become"<<endl<<endl;
	char n='A';
	for(int i=0;i<2;i++){
	for(int j=0;j<3;j++){
	cout<<n<<' ';
	n++;	
	} 
	cout<<endl;	
	}
	for(int i=0;i<2;i++){
	cout<<n<<' ';
	n++; 	
	} 
	cout<<'*'<<' '<<endl;
	SetColor(15,0);
	cout<<"The rules are as follows:"<<endl<<endl;
	cout<<"w: move up.  "<<"s: Move Downward.  "<<"a: move to the left.  "<<"d: move to the right."<<endl;
	cout<<"You can also enter b; to restart the game"<<endl<<endl;
	for(;;){//死循环实现不断输入 
	cin>>s;	
	cout<<endl;
	if(s=='b'){//移动 
	SetColor(4,15);
	cout<<"\n\nA new round of games!\n";
	SetColor(15,0);;
	Simple();
	return; 
	} 
	else if(s=='w'&&x!=0) swap(a[x][y],a[x--][y]);
	else if(s=='s'&&x!=2) swap(a[x][y],a[x++][y]);
	else if(s=='a'&&y!=0) swap(a[x][y],a[x][y--]);
	else if(s=='d'&&y!=2) swap(a[x][y],a[x][y++]);
	else{
	SetColor(0,15);
	for(int i=0;i<3;i++){
	for(int j=0;j<3;j++) cout<<a[i][j]<<' ';	
	cout<<endl;
	}
	SetColor(15,0);
	cout<<endl<<"Out of bounds, unable to move, please re-enter!"<<endl<<endl<<endl;
	continue;	
	} 
	for(int i=0;i<3;i++){
	for(int j=0;j<3;j++){
	if(a[i][j]=='*'){
	SetColor(4,0);
	cout<<a[i][j]<<' ';
	SetColor(15,0);
	}
	else cout<<a[i][j]<<' ';	
	} 	
	cout<<endl;
	}
	char n='A';
	int m=0;
	for(int i=0;i<2;i++){
	for(int j=0;j<3;j++){
	if(a[i][j]==n) m++;
	n++;	
	} 	
	}
	for(int i=0;i<3;i++){
	if(a[2][i]==n) m++;
	n++; 	
	} 
	if(m==8){//胜利条件 
	system("color 60");
	SetColor(0,15);
	cout<<endl<<"The game is over and you win!"<<endl;
	return;	
	} 
	else cout<<endl<<"continue!";
	} 
}

void Normal(){//普通模式
    int x=1,y=1;
    char a[10][10]={{'A','B','C','D'},{'E','*','F','G'},{'H','K','J','I'},{'L','N','M','O'}};
    char s; 
	SetColor(0,15);	
    cout<<"please change"<<endl<<endl; 
    for(int i=0;i<4;i++){
	for(int j=0;j<4;j++) cout<<a[i][j]<<' ';
	cout<<endl;	
	} 
	SetColor(15,0);
	SetColor(0,15);
    cout<<endl<<"become"<<endl<<endl;
	char n='A';
	for(int i=0;i<3;i++){
	for(int j=0;j<4;j++){
	cout<<n<<' ';
	n++;	
	} 
	cout<<endl;	
	}
	for(int i=0;i<3;i++){
	cout<<n<<' ';
	n++; 	
	} 
	cout<<'*'<<' '<<endl;
	SetColor(15,0);
	cout<<"The rules are as follows:"<<endl<<endl;
	cout<<"w: move up.  "<<"s: Move Downward.  "<<"a: move to the left.  "<<"d: move to the right."<<endl;
	cout<<"You can also enter b; to restart the game"<<endl<<endl;
	for(;;){
	cin>>s;	
	cout<<endl;
	if(s=='b'){
	SetColor(4,15);
	cout<<"\n\nA new round of games!\n";
	SetColor(15,0);;
	Normal();
	return; 
	}
	else if(s=='w'&&x!=0) swap(a[x][y],a[x--][y]);
	else if(s=='s'&&x!=3) swap(a[x][y],a[x++][y]);
	else if(s=='a'&&y!=0) swap(a[x][y],a[x][y--]);
	else if(s=='d'&&y!=3) swap(a[x][y],a[x][y++]);
	else{
	SetColor(0,15);
	for(int i=0;i<4;i++){
	for(int j=0;j<4;j++) cout<<a[i][j]<<' ';	
	cout<<endl;
	}
	SetColor(15,0);
	cout<<endl<<"Out of bounds, unable to move, please re-enter!"<<endl<<endl<<endl;
	continue;
	} 
	for(int i=0;i<4;i++){
	for(int j=0;j<4;j++){
	if(a[i][j]=='*'){
	SetColor(4,0);
	cout<<a[i][j]<<' ';
	SetColor(15,0);
	}	
	else cout<<a[i][j]<<' ';	
	} 	
	cout<<endl;
	}
	char n='A';
	int m=0;
	for(int i=0;i<3;i++){
	for(int j=0;j<4;j++){
	if(a[i][j]==n) m++;
	n++;	
	} 	
	}
	for(int i=0;i<4;i++){
	if(a[3][i]==n) m++;
	n++; 	
	} 
	if(m==15){
	system("color 60");
	SetColor(0,15);
	cout<<endl<<"The game is over and you win!"<<endl;
	SetColor(15,0);
	return;	
	} 
	else cout<<endl<<"continue!"<<endl<<endl<<endl;
	} 		
}
 
void Hard(){//困难模式
    int x=1,y=1;
    char a[10][10]={{'A','C','B'},{'E','*','F'},{'G','D','H'}};
    char s; 	
    SetColor(0,15);
    cout<<"please change"<<endl<<endl; 
    for(int i=0;i<3;i++){
	for(int j=0;j<3;j++) cout<<a[i][j]<<' ';
	cout<<endl;	
	} 
	SetColor(0,15);
    cout<<endl<<"become"<<endl<<endl;
	char n='A';
	for(int i=0;i<2;i++){
	for(int j=0;j<3;j++){
	cout<<n<<' ';
	n++;	
	} 
	cout<<endl;	
	}
	for(int i=0;i<2;i++){
	cout<<n<<' ';
	n++; 	
	} 
	cout<<'*'<<' '<<endl;
	SetColor(15,0);
	cout<<"The rules are as follows:"<<endl<<endl;
	cout<<"w: move up.  "<<"s: Move Downward.  "<<"a: move to the left.  "<<"d: move to the right."<<endl;
	cout<<"You can also enter b; to restart the game"<<endl<<endl;
	SetColor(0,15);
	cout<<"Now you can move 45 times. When the number is used up, the game is over.\n\n";
	SetColor(15,0);
	int time=45;//次数限制(可根据个人实力更改) 
	for(;;){
	if(time<=0){//次数用完,游戏失败 
	system("color 74");
	SetColor(15,0); 
	cout<<"Running out of steps!\n";
	cout<<"Game over!\n\n";
	SetColor(0,15);
	for(int i=0;i<3;i++){
	for(int j=0;j<3;j++) cout<<a[i][j]<<' ';	
	cout<<endl;
	}
	return;
	}	
	cin>>s;	
	cout<<endl;
	if(s=='b'){
	SetColor(4,15);
	cout<<"\n\nA new round of games!\n";
	SetColor(15,0);;
	Hard();
	return; 
	} 
	else if(s=='w'&&x!=0) swap(a[x][y],a[x--][y]);
	else if(s=='s'&&x!=2) swap(a[x][y],a[x++][y]);
	else if(s=='a'&&y!=0) swap(a[x][y],a[x][y--]);
	else if(s=='d'&&y!=2) swap(a[x][y],a[x][y++]);
	else{
	SetColor(0,15);
	for(int i=0;i<3;i++){
	for(int j=0;j<3;j++) cout<<a[i][j]<<' ';	
	cout<<endl;
	}
	SetColor(15,0);
	cout<<endl<<"Out of bounds, unable to move, please re-enter!"<<endl<<endl<<endl;
	continue;	
	} 
	for(int i=0;i<3;i++){
	for(int j=0;j<3;j++){
	if(a[i][j]=='*'){
	SetColor(4,0);
	cout<<a[i][j]<<' ';
	SetColor(15,0);
	}
	else cout<<a[i][j]<<' ';	
	} 	
	cout<<endl;
	}
	char n='A';
	int m=0;
	for(int i=0;i<2;i++){
	for(int j=0;j<3;j++){
	if(a[i][j]==n) m++;
	n++;	
	} 	
	}
	for(int i=0;i<3;i++){
	if(a[2][i]==n) m++;
	n++; 	
	} 
	if(m==8){
	system("color 60");
	SetColor(0,15);
	cout<<endl<<"The game is over and you win!"<<endl;
	SetColor(15,0);
	return;	
	} 
	else cout<<endl<<"continue!";
	SetColor(4,15);
	cout<<" You have "<<time-1<<" steps left."<<endl<<endl<<endl;
	SetColor(15,0);
	time--;//次数减少 
	} 	
}
2024/1/14 11:44
加载中...