黑马程序员技术交流社区
标题:
iOS基础c语言推箱子
[打印本页]
作者:
guozihui876
时间:
2015-10-7 22:06
标题:
iOS基础c语言推箱子
//
// main.c
// 推箱子
//
// Created by niaoren on 15/10/4.
// Copyright (c) 2015年 niaoren. All rights reserved.
//
#include <stdio.h>
#define ROWS 10
#define COLS 11
void printMap(char arr[ROWS][COLS]){
for (int i=0; i<10; i++) {
printf("\n");
for (int j=0; j<10; j++) {
printf("%c ",arr[i][j]);
}
}
printf("\n");
}
//void temp(char map[][],int ){
// char temp;
// temp=map[personY][personX];
// map[personY][personX] = map[newPersonY][newPersonx];
// map[newPersonY][newPersonx] = temp;
//
//}
int main(int argc, const char * argv[]) {
// insert code here...
// printf("Hello, World!\n");
// char map[ROWS][11]={
// '#','#','#','#','#','#','#','#','#','#',
// '#',' ','O','#','#','#','#',' ',' ','#',
// '#',' ','X','#','#','#','#',' ',' ','#',
// '#',' ',' ',' ',' ',' ',' ',' ',' ','#',
// '#','#','#','#','#','#',' ',' ',' ','#',
// '#',' ',' ','#','#','#','#',' ',' ','#',
// '#',' ',' ',' ',' ',' ',' ',' ',' ','#',
// '#',' ',' ',' ','#','#','#','#','#','#',
// '#',' ',' ',' ',' ',' ',' ',' ',' ','#',
// '#','#','#','#','#','#','#','#','#','#'
// };
char map[ROWS][COLS]= {
"##########",
"# O#### #",
"# X#### #",
"# #",
"###### #",
"# #### #",
"# #",
"# ######",
"# ",
"##########",
};
printMap(map);
printf("请输入wsad 分别表示上下左右\n");
char key;
int personX=2;
int personY=1;
int boxX=2;
int boxY=2;
int newPersonX,newPersonY,newBoxY,newBoxX;
char temp;
while (1) {
scanf("%c",&key);
// getchar();
switch (key) {
case 'W':
case 'w':
if (map[personY-1][personX]== ' ') {
newPersonY = personY-1;
newPersonX = personX;
temp=map[personY][personX];
map[personY][personX] = map[newPersonY][newPersonX];
map[newPersonY][newPersonX] = temp;
personY--;
printMap(map);
}
else if(map[personY-1][personX]=='X'&&map[boxY-1][boxX]==' '){
newBoxY = boxY-1;
newBoxX = boxX;
temp = map[newBoxY][newBoxX];
map[newBoxY][newBoxX] = map[boxY][boxX];
map[boxY][boxX] = temp;
boxY--;
newPersonY = personY-1;
newPersonX = personX;
temp=map[personY][personX];
map[personY][personX] = map[newPersonY][newPersonX];
map[newPersonY][newPersonX] = temp;
personY--;
printMap(map);
}
break;
case 'S':
case 's':
if (map[personY+1][personX]== ' ') {
newPersonY = personY+1;
newPersonX = personX;
temp=map[personY][personX];
map[personY][personX] = map[newPersonY][newPersonX];
map[newPersonY][newPersonX] = temp;
personY++;
printMap(map);
}
else if(map[personY+1][personX]=='X'&&map[boxY+1][boxX]==' '){
newBoxY = boxY+1;
newBoxX = boxX;
temp = map[newBoxY][newBoxX];
map[newBoxY][newBoxX] = map[boxY][boxX];
map[boxY][boxX] = temp;
boxY++;
newPersonY = personY+1;
newPersonX = personX;
temp=map[personY][personX];
map[personY][personX] = map[newPersonY][newPersonX];
map[newPersonY][newPersonX] = temp;
personY++;
printMap(map);
}
break;
case 'A':
case 'a':
if (map[personY][personX-1]== ' ') {
newPersonY = personY;
newPersonX = personX-1;
temp=map[personY][personX];
map[personY][personX] = map[newPersonY][newPersonX];
map[newPersonY][newPersonX] = temp;
personX--;
printMap(map);
}
else if(map[personY][personX-1]=='X'&&map[boxY][boxX-1]==' '){
newBoxX = boxX-1;
newBoxY = boxY;
temp = map[newBoxY][newBoxX];
map[newBoxY][newBoxX] = map[boxY][boxX];
map[boxY][boxX] = temp;
boxX--;
newPersonY = personY;
newPersonX = personX-1;
temp=map[personY][personX];
map[personY][personX] = map[newPersonY][newPersonX];
map[newPersonY][newPersonX] = temp;
personX--;
printMap(map);
}
break;
case 'D':
case 'd':
if (map[personY][personX+1]== ' ') {
newPersonY = personY;
newPersonX = personX+1;
temp=map[personY][personX];
map[personY][personX] = map[newPersonY][newPersonX];
map[newPersonY][newPersonX] = temp;
personX++;
printMap(map);
}
else if(map[personY][personX+1]=='X'&&map[boxY][boxX+1]==' '){
newBoxY = boxY;
newBoxX = boxX+1;
temp = map[newBoxY][newBoxX];
map[newBoxY][newBoxX] = map[boxY][boxX];
map[boxY][boxX] = temp;
boxX++;
newPersonX = personX+1;
newPersonY = personY;
temp=map[personY][personX];
map[personY][personX] = map[newPersonY][newPersonX];
map[newPersonY][newPersonX] = temp;
personX++;
printMap(map);
}
break;
}
if(boxX==COLS-2){
printf("你赢了");
return 0;
}
}
return 0;
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2