推箱子~求大神精简;
//
// main.c
// 《推箱子》游戏
//
// Created by sharui on 15-6-30.
// Copyright (c) 2015年 sharui. All rights reserved.
//
#include <stdio.h>
#define Row 10
#define Col 10
void mapPrint(char map[Row][Col]){
for (int i = 0; i < Row; i++) {
for (int j = 0; j < Col; j++) {
printf("%c",map[i][j]);
}printf("\n");
}
}
void exchange(char map[Row][Col],int oldX,int oldY,int newX, int newY){
char temp;
temp=map[oldY][oldX];
map[oldY][oldX]=map[newY][newX];
map[newY][newX]=temp;
}
int main(int argc, const char * argv[]) {
char map[Row][Col]={
{'#','#','#','#','#','#','#','#','#','#'},
{'#',' ',' ','#','#','#','#','#','#','#'},
{'#',' ','0','#','#','#','#','#','#','#'},
{'#',' ','X','#','#','#','#','#','#','#'},
{'#','#',' ','#','#','#','#',' ',' ',' '},
{'#','#',' ','#','#','#','#',' ',' ','#'},
{'#',' ',' ','#','#','#','#',' ',' ','#'},
{'#',' ',' ',' ',' ',' ',' ',' ',' ','#'},
{'#',' ',' ',' ',' ','#','#',' ',' ','#'},
{'#','#','#','#','#','#','#','#','#','#'}
};
mapPrint(map);
int playerNowX=2,playerNowY=2,playerNextX,playerNextY;
int boxNowX=2,boxNowY=3,boxNextX,boxNextY;
char direction;
playerNextX=playerNowX;
playerNextY=playerNowY;
boxNextX=boxNowX;
boxNextY=boxNowY;
char street = ' ';
char box = 'X';
printf("请输入方向, w为上,s为下,a为左,d为右,q为退出");
while (1) {
playerNextX=playerNowX;
playerNextY=playerNowY;
scanf("%c",&direction);
getchar();
switch (direction) {
case 'w':
case 'W': playerNextY--;
break;
case 's':
case 'S': playerNextY++;
break;
case 'a':
case 'A': playerNextX--;
break;
case 'd':
case 'D': playerNextX++;
break;
case 'q':
case 'Q':
printf("程序退出中。\n");
printf("程序退出中。。\n");
printf("程序退出中。。。\n");
printf("程序已退出。\n");
return 0;
break; }
if (map[playerNextY][playerNextX]==street) {
exchange(map,playerNowX,playerNowY,playerNextX,playerNextY);
playerNowX=playerNextX;
playerNowY=playerNextY;
}else if(map[playerNextY][playerNextX]==box){
boxNextX=boxNowX+(boxNextX-playerNowX);
boxNextY=boxNextY+(boxNextY-playerNowY);
if (map[boxNextY][boxNextX]==street) {
exchange(map,boxNowX,boxNowY,boxNextX,boxNextY);
boxNowX=boxNextX;
boxNowY=boxNextY;
exchange(map,playerNowX,playerNowY,playerNextX,playerNextY);
playerNowX=playerNextX;
playerNowY=playerNextY;
}
}
mapPrint(map);
if (boxNowX==Row-1) {
printf("走出来了");
return 0;
}
}
return 0;
}
|
|