C++实现推箱子小项目

时间:2022-09-04 11:50:39

本文实例为大家分享了C++实现推箱子小项目的具体代码,供大家参考,具体内容如下

C++实现推箱子小项目

代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <bits/stdc++.h>
#include<conio.h>
using namespace std;
const int MAXN =10;
class GM
{ int x=1,y=1; //x纵坐标 ,y横坐标
  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关)   |"<<endl;
cout<<"|    游戏规则:           |"<<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')//当有箱子且不会被墙档
{ 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();
 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/wxk0123456789/article/details/119259479