**
详细见注释
**
#include <stdlib.h>
#include <getch.h>
int main(int argc,const char* argv[])
{
char map[8][8] ={
{0,0,3,3,3,3,0,0},
{0,0,3,5,5,3,0,0},
{0,3,3,0,5,3,3,0},
{0,3,0,0,4,5,3,0},
{3,3,0,4,0,0,3,3},
{3,0,0,0,4,4,0,3},
{3,0,0,2,0,0,0,3},
{3,3,3,3,3,3,3,3}
};
char m_x = 6,m_y = 3;
for(;;)
{
int cnt = 0;
system("clear");
for(int i=0; i<8; i++)
{
for(int j=0; j<8; j++)
{
switch(map[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("$ "); cnt++; break;
}
}
printf("\n");
}
if(4 == cnt)
{
printf("游戏胜利!\n");
return 0;
}
int p_x = 0, p_y = 0;
switch(getch())
{
case 183: p_x--; break;
case 184: p_x++; break;
case 185: p_y++; break;
case 186: p_y--; break;
}
if(0 == map[m_x+p_x][m_y+p_y] || 5 == map[m_x+p_x][m_y+p_y])
{
map[m_x+p_x][m_y+p_y] += 2;
map[m_x][m_y] -= 2;
m_x += p_x;
m_y += p_y;
}
else if(4 == map[m_x+p_x][m_y+p_y] || 9 == map[m_x+p_x][m_y+p_y])
{
if(0 == map[m_x+2*p_x][m_y+2*p_y] ||
5 == map[m_x+2*p_x][m_y+2*p_y])
{
map[m_x+2*p_x][m_y+2*p_y] += 4;
map[m_x+p_x][m_y+p_y] -= 2;
map[m_x][m_y] -= 2;
m_x += p_x;
m_y += p_y;
}
}
}
}
|