#include
#include
#include
int main(int argc,const char* argv[])
{
char map[8][8] = {
{0,0,1,1,1,1,0,0},
{0,0,1,3,3,1,0,0},
{0,1,1,0,3,1,1,0},
{0,1,0,0,4,3,1,0},
{1,1,0,4,0,0,1,1},
{1,0,0,1,4,4,0,1},
{1,0,0,2,0,0,0,1},
{1,1,1,1,1,1,1,1},
};
int mr = 6 , mc = 3;
size_t step = 0;
for(;;)
{
system("clear");
int success = 0;
for(int r=0; r<8; r++)
{
for(int c=0; c<8; c++)
{
switch(map[r][c])
{
case 0: printf(" "); break;
case 1: printf(" #"); break;
case 2: printf(" @"); break;
case 3: printf(" O"); break;
case 4: printf(" ="); break;
case 5: printf(" @"); break;
case 7: printf(" ="); success++; break;
}
}
printf("\n");
}
if(4 == success)
{
printf("恭喜你过关,共用了%u步!\n",step);
break;
}
switch(getch())
{
case 183:
if(0==map[mr-1][mc] || 3==map[mr-1][mc])
{
map[mr--][mc] -= 2;
map[mr][mc] += 2;
step++;
}
else if( (4==map[mr-1][mc] || 7==map[mr-1][mc]) && (0==map[mr-2][mc] || 3==map[mr-2][mc]))
{
map[mr--][mc] -= 2;
map[mr][mc] += 2;
map[mr][mc] -= 4;
map[mr-1][mc] += 4;
step++;
}
break;
case 184:
if(0==map[mr+1][mc] || 3==map[mr+1][mc])
{
map[mr++][mc] -= 2;
map[mr][mc] += 2;
step++;
}
else if( (4==map[mr+1][mc] || 7==map[mr+1][mc]) && (0==map[mr+2][mc] || 3==map[mr+2][mc]))
{
map[mr++][mc] -= 2;
map[mr][mc] += 2;
map[mr][mc] -= 4;
map[mr+1][mc] += 4;
step++;
}
break;
case 185:
if(0==map[mr][mc+1] || 3==map[mr][mc+1])
{
map[mr][mc++] -= 2;
map[mr][mc] += 2;
step++;
}
else if( (4==map[mr][mc+1] || 7==map[mr][mc+1]) && (0==map[mr][mc+2] || 3==map[mr][mc+2]))
{
map[mr][mc++] -= 2;
map[mr][mc] += 2;
map[mr][mc] -= 4;
map[mr][mc+1] += 4;
step++;
}
break;
case 186:
if(0==map[mr][mc-1] || 3==map[mr][mc-1])
{
map[mr][mc--] -= 2;
map[mr][mc] += 2;
step++;
}
else if( (4==map[mr][mc-1] || 7==map[mr][mc-1]) && (0==map[mr][mc-2] || 3==map[mr][mc-2]))
{
map[mr][mc--] -= 2;
map[mr][mc] += 2;
map[mr][mc] -= 4;
map[mr][mc-1] += 4;
step++;
}
break;
}
}
}
改良版:
#include
#include
#include
int main(int argc,const char* argv[])
{
char map[8][8] = {
{0,0,1,1,1,1,0,0},
{0,0,1,3,3,1,0,0},
{0,1,1,0,3,1,1,0},
{0,1,0,0,4,3,1,0},
{1,1,0,4,0,0,1,1},
{1,0,0,1,4,4,0,1},
{1,0,0,2,0,0,0,1},
{1,1,1,1,1,1,1,1},
};
int mr = 6 , mc = 3 , step = 0;
for(;;)
{
system("clear");
int success = 0;
for(int r=0; r<8; r++)
{
for(int c=0; c<8; c++)
{
switch(map[r][c])
{
case 0: printf(" "); break;
case 1: printf(" #"); break;
case 3: printf(" O"); break;
case 2:
case 5: printf(" @"); break;
case 7: success++;
case 4: printf(" ="); break;
}
}
printf("\n");
}
if(4 == success)
{
printf("恭喜你过关,共用了%u步!\n",step);
break;
}
int r = 0 , c = 0;
switch(getch())
{
case 183: r = -1; break;
case 184: r = 1; break;
case 185: c = 1; break;
case 186: c = -1; break;
}
if(0==map[mr+r][mc+c] || 3==map[mr+r][mc+c])
{
map[mr][mc] -= 2;
mr += r;
mc += c;
map[mr][mc] += 2;
step++;
}
else if((4==map[mr+r][mc+c] || 7==map[mr+r][mc+c]) &&
(0==map[mr+r*2][mc+c*2] || 3==map[mr+r*2][mc+c*2]))
{
map[mr][mc] -= 2;
mr += r;
mc += c;
map[mr][mc] -= 2;
map[mr+r][mc+c] += 4;
step++;
}
}
}