c语言课程设计经典例题推箱子
更新时间:2024-01-23 03:57:01 阅读量: 教育文库 文档下载
- 推箱子c语言课程设计报告推荐度:
- 相关推荐
贪吃蛇源代码完美运行,结尾附图 #include
int printMap(int map[][16]);
void moveBox(int map[][16],int x,int y,int select,int map2[][16]); int insert(int map[][16],int map2[][16],int sum); void UI(); void img1(); void img2(); void img3(); void img4(); void img5(); void img6();
void color(int b) //颜色函数 {
HANDLE hConsole =
GetStdHandle((STD_OUTPUT_HANDLE)) ; SetConsoleTextAttribute(hConsole,b) ; }
void img1() {
int map2[14][16]; int i,j,sum=0;
int map[14][16]={
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0}, {0,0,0,0,0,0,1,3,1,0,0,0,0,0,0,0},
{0,0,0,1,1,1,1,4,1,0,0,0,0,0,0,0}, {0,0,0,1,3,2,4,6,1,1,1,0,0,0,0,0}, {0,0,0,1,1,1,4,2,4,3,1,0,0,0,0,0}, {0,0,0,0,0,1,2,1,1,1,1,0,0,0,0,0}, {0,0,0,0,0,1,3,1,0,0,0,0,0,0,0,0}, {0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, };
for(i=0;i<14;i++)
for(j=0;j<16;j++){
map2[i][j]=map[i][j];
if(map[i][j] == 3 || map[i][j] == 5) sum++; }
insert(map,map2,sum); img2(); }
void img2() {
int map2[14][16]; int i,j,sum=0;
int map[14][16]={
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0}, {0,0,0,0,1,2,6,3,3,3,1,0,0,0,0,0}, {0,0,0,0,1,2,2,2,2,2,1,1,0,0,0,0}, {0,0,0,1,1,1,4,2,2,2,2,1,0,0,0,0}, {0,0,0,1,2,2,2,2,2,2,2,1,0,0,0,0}, {0,0,0,1,2,4,2,1,4,1,2,1,0,0,0,0}, {0,0,0,1,2,2,2,1,2,2,2,1,0,0,0,0},
{0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, };
for(i=0;i<14;i++)
for(j=0;j<16;j++){
map2[i][j]=map[i][j];
if(map[i][j] == 3 || map[i][j] == 5) sum++; }
insert(map,map2,sum); img3(); }
void img3() {
int map2[14][16]; int i,j,sum=0;
int map[14][16]={
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0}, {0,0,0,0,1,2,2,6,1,0,0,0,0,0,0,0}, {0,0,0,0,1,2,4,4,1,0,1,1,1,0,0,0}, {0,0,0,0,1,2,4,2,1,0,1,3,1,0,0,0}, {0,0,0,0,1,1,1,2,1,1,1,3,1,0,0,0}, {0,0,0,0,0,1,1,2,2,2,2,3,1,0,0,0}, {0,0,0,0,0,1,2,2,2,1,2,2,1,0,0,0}, {0,0,0,0,0,1,2,2,2,1,1,1,1,0,0,0}, {0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, };
for(i=0;i<14;i++)
for(j=0;j<16;j++){
map2[i][j]=map[i][j];
if(map[i][j] == 3 || map[i][j] == 5) sum++; }
insert(map,map2,sum); img4(); }
void img4() {
int map2[14][16]; int i,j,sum=0;
int map[14][16]={
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0}, {0,0,0,0,0,0,1,2,2,1,0,0,0,0,0,0}, {0,0,0,0,0,1,1,2,2,1,0,0,0,0,0,0}, {0,0,0,0,0,1,6,4,2,1,0,0,0,0,0,0}, {0,0,0,0,0,1,1,4,2,1,1,0,0,0,0,0}, {0,0,0,0,0,1,1,2,4,2,1,0,0,0,0,0}, {0,0,0,0,0,1,3,4,2,2,1,0,0,0,0,0}, {0,0,0,0,0,1,3,3,5,3,1,0,0,0,0,0}, {0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, };
for(i=0;i<14;i++)
for(j=0;j<16;j++){
map2[i][j]=map[i][j];
if(map[i][j] == 3 || map[i][j] == 5) sum++;
}
insert(map,map2,sum); img5(); }
void img5() {
int map2[14][16]; int i,j,sum=0;
int map[14][16]={
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0}, {0,0,1,1,1,1,2,2,2,2,2,1,0,0,0,0}, {0,0,1,2,2,2,3,1,1,1,2,1,0,0,0,0}, {0,0,1,2,1,2,1,2,2,2,2,1,1,0,0,0}, {0,0,1,2,1,2,4,2,4,1,3,2,1,0,0,0}, {0,0,1,2,1,2,2,5,2,2,1,2,1,0,0,0}, {0,0,1,2,3,1,4,2,4,2,1,2,1,0,0,0}, {0,0,1,1,2,2,2,2,1,2,1,2,1,1,1,0}, {0,0,0,1,2,1,1,1,3,2,2,2,2,6,1,0}, {0,0,0,1,2,2,2,2,2,1,1,2,2,2,1,0}, {0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, };
for(i=0;i<14;i++)
for(j=0;j<16;j++){
map2[i][j]=map[i][j];
if(map[i][j] == 3 || map[i][j] == 5) sum++; }
insert(map,map2,sum); img5(); }
void img6() {
int map2[14][16]; int i,j,sum=0;
int map[14][16]={
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0}, {0,0,0,0,0,1,3,2,3,2,3,1,0,0,0,0}, {0,0,0,0,0,1,2,4,4,4,2,1,0,0,0,0}, {0,0,0,0,0,1,3,4,6,4,3,1,0,0,0,0}, {0,0,0,0,0,1,2,4,4,4,2,1,0,0,0,0}, {0,0,0,0,0,1,3,2,3,2,3,1,0,0,0,0}, {0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, };
for(i=0;i<14;i++)
for(j=0;j<16;j++){
map2[i][j]=map[i][j];
if(map[i][j] == 3 || map[i][j] == 5) sum++; }
insert(map,map2,sum); }
int insert(int map[][16],int map2[][16],int sum) {
int i,j;
int x,y; //人的位置
char op; //所按下的方向键
int num = 0; //还剩下几个箱子没有推向目的地
while(1){
for(i=0;i<14;i++){
for(j=0;j<16;j++)
if(map[i][j] == 6) goto found; found:
} x = i; y = j;
system(\
num = printMap(map); if(num == sum){
printf(\你通关了....\\n\ return 0; } else{
op = getch();
if((op == 'Q') || (op =='q')){ printf(\退出....\\n\ return 2; }
op = getch(); }
switch(op) {
case 72: //上箭头
moveBox(map,x,y,1,map2); break;
case 75: //左箭头
moveBox(map,x,y,2,map2); break;
case 77: //右箭头
moveBox(map,x,y,3,map2);
break;
case 80: //下箭头
moveBox(map,x,y,4,map2); break; default:
break; } } }
int printMap(int map[][16]) {
int i,j;
int count = 0;
for(i=0;i<14;i++){
for(j=0;j<16;j++){
switch(map[i][j]){
case 0: printf(\ \ break;
case 1: color(13); printf(\■\ break;
case 2: 地
printf(\ \ break;
case 3: color(11); printf(\×\ break;
case 4: color(8);
//空地 //墙 //内部空//目的地//箱子 printf(\◎\ break;
case 5: //箱子推到目的地后显示
count++; color(9); printf(\☆\ break;
case 6: //小人 color(10); printf(\♀\ break;
} }
printf(\ }
printf(\
printf(\按方向键操作游戏,按Q退出!\\n\ return count; }
void moveBox(int map[][16],int x,int y,int select,int map2[][16]) {
int x1,y1; //下个位置的坐标 int x2,y2; //下下个位置的坐标 switch(select) {
case 1: //向上移动 x1 = x - 1; y1 = y; x2 = x - 2; y2 = y; break;
case 2: //向左移动
x1 = x; y1 = y - 1; x2 = x; y2 = y - 2; break;
case 3: //向右移动 x1 = x; y1 = y + 1; x2 = x; y2 = y + 2; break;
case 4: //向下移动 x1 = x + 1; y1 = y; x2 = x + 2; y2 = y; break; default:
break; }
//operate map
switch(map[x1][y1]){
case 1: //墙,不动 break;
case 2: if(map2[x][y] == 3){//空格,分情况变换
map[x1][y1] = 6; map[x][y] = 3; } else{
map[x1][y1] = 6; map[x][y] = 2; } break;
case 3:
if(map2[x][y] == 3){//目的地,分情况变换 map[x1][y1] = 6; map[x][y] = 3; } else{
map[x1][y1] = 6; case 4: 6)
map2[x][y] == 6)
case 5:
map[x][y] = 2; }
break;
if(map[x2][y2] == 3){ map[x2][y2] = 5; map[x1][y1] = 6;
if(map2[x][y] == 2 || map2[x][y] == map[x][y] = 2; else if(map2[x][y] == 3) map[x][y] = 3; else
map[x][y] = 2; }
else if(map[x2][y2] == 2 || map2[x][y] == 6){ map[x2][y2] = 4; map[x1][y1] = 6; if(map2[x][y] == 2 || map[x][y] = 2; else if(map2[x][y] == 3) map[x][y] = 3; else
map[x][y] = 2; }
break; if((map2[x][y] == 2 || map2[x][y] == 6) && map2[x2][y2] == 3) {
map[x2][y2] = 5; map[x1][y1] = 6; map[x][y] = 2; }
else if(map2[x][y] == 3 && map[x2][y2] == 3){
map[x2][y2] = 5; map[x1][y1] = 6; map[x][y] = 3; }
else if(map2[x][y] == 5 && map[x2][y2] == 3){
map[x2][y2] = 5; map[x1][y1] = 6; map[x][y] = 3; map2[x][y] = 3; }
else if((map2[x][y] == 2 || map2[x][y] == 6) && map2[x2][y2] == 2){
map[x2][y2] = 4; map[x1][y1] = 6; map[x][y] = 2; map2[x1][y1] = 3; }
//箱子推到目的地后 break; default:
break; } }
void UI()
{
char ch,num; color(11);
printf(\开始--------------|\\n\ printf(\选关--------------|\\n\ printf(\退出--------------|\\n\ ch=getch(); if(ch == '1')
img1(); if(ch == '2'){
printf(\请输入关数(1--6):\ num=getch(); switch(num){
case '1' : img1();break; case '2' : img2();break; case '3' : img3();break; case '4' : img4();break; case '5' : img5();break; case '6' : img6();break; default : printf(\error!\\n\ } }
if(ch == '3'){
system(\ exit(0); } }
void main() {
UI(); }
正在阅读:
c语言课程设计经典例题推箱子01-23
公共交通让座情况调查与研究05-15
专题研究报告-《基础设施和公用事业特许经营管理办法》评析及与《市政公用事业特许经营管理办法》条文比较 - 图文12-13
生物化学习题及参考答案10-06
2015-2020年中国工程交易中心行业运营态势及未来发展趋势报告08-30
工程量清单及计价表09-30
砌体结构习题集05-12
大学生毕业实习报告3000字_105-11
平顶山住宅楼施工组织设计 - 图文04-26
促进我国建筑节能工作的建议04-19
- exercise2
- 铅锌矿详查地质设计 - 图文
- 厨余垃圾、餐厨垃圾堆肥系统设计方案
- 陈明珠开题报告
- 化工原理精选例题
- 政府形象宣传册营销案例
- 小学一至三年级语文阅读专项练习题
- 2014.民诉 期末考试 复习题
- 巅峰智业 - 做好顶层设计对建设城市的重要意义
- (三起)冀教版三年级英语上册Unit4 Lesson24练习题及答案
- 2017年实心轮胎现状及发展趋势分析(目录)
- 基于GIS的农用地定级技术研究定稿
- 2017-2022年中国医疗保健市场调查与市场前景预测报告(目录) - 图文
- 作业
- OFDM技术仿真(MATLAB代码) - 图文
- Android工程师笔试题及答案
- 生命密码联合密码
- 空间地上权若干法律问题探究
- 江苏学业水平测试《机械基础》模拟试题
- 选课走班实施方案
- 推箱子
- 例题
- 语言
- 课程
- 经典
- 设计