本文实例为大家分享了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
|
#include<stdio.h> #include<stdlib.h> #include<getch.h> int main( int argc, const char *argv[]) { int cut=0; int a[8][8]={ {0,0,3,3,3,3,0,0}, {0,0,3,5,5,3,0,0}, {0,0,3,0,5,3,0,0}, {0,3,0,0,4,5,3,0}, {3,3,0,4,0,0,3,3}, {3,0,0,3,4,4,0,3}, {3,0,0,2,0,0,0,3}, {3,3,3,3,3,3,3,3}}; int x=6,y=3; for (;;) { int cunt=0; system ( "clear" ); for ( int i=0;i<8;i++) { for ( int j=0;j<8;j++) { if (a[i][j]==9) { cunt++; } switch (a[i][j]) { case 0: printf ( " " ); break ; case 2: printf ( "@ " ); break ; //人 case 3: printf ( "# " ); break ; //墙 case 4: printf ( "$ " ); break ; //箱子 case 5: printf ( "o " ); break ; //目标位置 case 7: printf ( "@ " ); break ; case 9: printf ( "$ " ); break ; } } printf ( "\n" ); } if (4==cunt) { printf ( "成功\n步数:%d" ,cut); return 0; } //根据数据的大小来判断当前的坐标上的情况 switch (getch()) { case 183: if (0!=x&&3==a[x-1][y]) { a[x][y]=2; } else if (5==a[x-1][y]||0==a[x-1][y]) { a[x][y]-=2; a[x-1][y]+=2; x--; cut++; } else if ((5==a[x-2][y]||0==a[x-2][y])&&(4==a[x-1][y]||9==a[x-1][y])) { a[x-2][y]+=4; a[x-1][y]-=4; a[x-1][y]+=2; a[x][y]-=2; x--;cut++; } break ; case 184: if (0!=x&&3==a[x+1][y]) { a[x][y]=2; } else if ((5==a[x+1][y])||(0==a[x+1][y])) { a[x][y]-=2; a[x+1][y]+=2; x++;cut++; } else if ((5==a[x+2][y]||0==a[x+2][y])&&(4==a[x+1][y]||9==a[x+1][y])) { a[x+2][y]+=4; a[x+1][y]-=4; a[x+1][y]+=2; a[x][y]-=2; x++;cut++; } break ; case 185: if (0!=x&&3==a[x][y+1]) { a[x][y]=2; } else if (5==a[x][y+1]||0==a[x][y+1]) { a[x][y]-=2; a[x][y+1]+=2; y++;cut++; } else if ((0==a[x][y+2]||5==a[x][y+2])&&(4==a[x][y+1]||9==a[x][y+1])) { a[x][y+2]+=4; a[x][y+1]-=4; a[x][y+1]+=2; a[x][y]-=2; y++;cut++; } break ; case 186: if (0!=x&&3==a[x][y-1]) { a[x][y]=2; } else if (5==a[x][y-1]||0==a[x][y-1]) { a[x][y]-=2; a[x][y-1]+=2; y--;cut++; } else if ((0==a[x][y-2]||5==a[x-1][y])&&(4==a[x][y-1]||9==a[x][y-1])) { a[x][y-2]+=4; a[x][y-1]-=4; a[x][y-1]+=2; a[x][y]-=2; y--;cut++; } break ; } } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/weixin_49198344/article/details/108173105