C++實(shí)現(xiàn)推箱子小項(xiàng)目
更新時間:2021年08月02日 11:45:28 作者:王也枉不了
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)推箱子小項(xiàng)目,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了C++實(shí)現(xiàn)推箱子小項(xiàng)目的具體代碼,供大家參考,具體內(nèi)容如下

代碼:
#include <bits/stdc++.h>
#include<conio.h>
using namespace std;
const int MAXN =10;
class GM
{ int x=1,y=1; //x縱坐標(biāo) ,y橫坐標(biāo)
int count =0;
public:
char map[MAXN][MAXN];
GM();
int RUN();
int check(int x1,int y1,int t,int C);//檢查
void Display(); //打印
void Show();
void Start();
};
void GM::Start()
{
while(1)
{
Show();
int h;
cin>>h;
if(h==521)
{
Display();
while(1) {
int h=RUN();
if(h==-1){system("cls");
cout<<"I LOVE YOU!";
system("pause");
exit(0);
}
else if(h==4)
{ system("cls");
GM k;
k.Start();
}
else Display();
}
}
else
{system("cls");
cout<<"輸錯了再試一次(521)";
}
}
}
void GM::Show()
{
cout<<"__________________________"<<endl;
cout<<"| WXK&&WSN(共1關(guān)) |"<<endl;
cout<<"| 游戲規(guī)則: |"<<endl;
cout<<"| &:人 O:箱子 |"<<endl;
cout<<"| H:墻 *:目的地 |"<<endl;
cout<<"| 操作說明 Q:退出 |"<<endl;
cout<<"| W:上 S:下 |"<<endl;
cout<<"| D:右 A:左 |"<<endl;
cout<<"| 開始輸入:521 |"<<endl;
cout<<"|_________________________|"<<endl;
}
void GM:: Display() //打印
{ system("cls");
cout<<"Count:"<<count<<"/ 2"<<endl;
for(int i=0;i<10;i++)
{
for(int j=0; j<10;j++)
if(map[i][j]!='0')
cout<<map[i][j];
else cout<<" ";
cout<<endl;
}
}
GM::GM() //初始化地圖
{
fill(map[0],map[0]+MAXN*MAXN,'0');
for(int i=0;i<MAXN;i++)
{
map[0][i]='H';
map[i][0]='H';
map[MAXN-1][i]='H';
map[i][MAXN-1]='H';
}
map[2][2]='O';
map[6][2]='O';
map[1][1]='P';
map[1][3]='H';
map[2][3]='H';
map[3][3]='H';
map[2][5]='H';
map[6][4]='H';
map[5][4]='H';
map[7][4]='H';
map[3][5]='H';
map[3][6]='H';
map[3][8]='H';
map[4][8]='H';
map[6][5]='*';
}
int GM::check(int x1,int y1,int t,int L)
{ int M=0,N=0;
if(t==1)
{ M=L;
N=0;
}
else if(t==-1)
{
M=0;
N=L;
}
if(map[x1][y1]=='H'||(map[x1][y1]=='O'&&(map[x1-M][y1+N]=='H'||map[x1-M][y1+N]=='O')))//有箱子但是會被主檔和直接是墻
return -1;
else if(map[x1][y1]=='O'&&map[x1-M][y1+N]!='H')//當(dāng)有箱子且不會被墻檔
{ if(map[x1-M][y1+N]=='*')
return 3;
return 1;
}
else if(map[x1][y1]!='H'&&map[x1][y1]!='*')return 2;
}
int GM::RUN()
{
char ch =getch();
int t =1,L=1;//t判斷上下還是左右,L判斷加減
if(ch =='a'||ch=='d'||ch=='A'||ch=='D')t=-1;
if(ch=='a'||ch=='s'||ch=='A'||ch=='S')L=-1;
if(ch=='Q'||ch=='q')return 4;
if(t==1&&check(x-L,y,t,L)==1)//往上或下有箱子了話
{
map[x][y]='0';
x-=L;
map[x][y]='P';
map[x-L][y]='O';
}
else if(t==1&&check(x-L,y,t,L)==2)//往上或者往下能走了話
{
map[x][y]='0';
x-=L;
map[x][y]='P';
}
else if(t==1&&check(x-L,y,t,L)==3)
{ map[x][y]='0';
x-=L;
map[x][y]='P';
if(count==2)
return -1;
}
else if(t==-1&&check(x,y+L,t,L)==1)//往左或右有箱子了話
{
map[x][y]='0';
y+=L;
map[x][y]='P';
map[x][y+L]='O';
}
else if(t==-1&&check(x,y+L,t,L)==2)//往左或者往右能走了話
{
map[x][y]='0';
y+=L;
map[x][y]='P';
}
else if(t==-1&&check(x,y+L,t,L)==3)
{ map[x][y]='0';
y+=L;
map[x][y]='P';
count++;
if(count==2)
return -1;
}
}
int main() {
system("color E4");
GM k;
k.Start();
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C/C++函數(shù)參數(shù)聲明解析int?fun()?與?int?fun(void)?的區(qū)別講解
C++中int fun()和int fun(void)的區(qū)別在于函數(shù)參數(shù)的聲明方式,前者默認(rèn)允許任意參數(shù),而后者表示沒有參數(shù),通過清晰的實(shí)例源代碼,詳細(xì)解釋了它們在函數(shù)聲明和調(diào)用中的不同之處,這篇文章介紹了C/C++函數(shù)參數(shù)聲明int?fun()與int?fun(void)的差異,需要的朋友可以參考下2024-01-01
一文帶你掌握C++中的移動語義和完美轉(zhuǎn)發(fā)
這篇文章主要為大家詳細(xì)介紹了C++中的移動語義和完美轉(zhuǎn)發(fā)的相關(guān)知識,文中的示例代碼講解詳細(xì),對我們深入掌握C++有一定的幫助,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12
C++ Boost Chrono實(shí)現(xiàn)計(jì)時碼表流程詳解
Boost是為C++語言標(biāo)準(zhǔn)庫提供擴(kuò)展的一些C++程序庫的總稱。Boost庫是一個可移植、提供源代碼的C++庫,作為標(biāo)準(zhǔn)庫的后備,是C++標(biāo)準(zhǔn)化進(jìn)程的開發(fā)引擎之一,是為C++語言標(biāo)準(zhǔn)庫提供擴(kuò)展的一些C++程序庫的總稱2022-11-11

