写了个解迷小程序,c prime plus上的练习题太乏味了。
#include <stdio.h>int a[7] = {1, 2, 3, 0, 4, 5, 6};
void print_it(void);
int main(void)
{
char str[1000];
int ch;
int i;
printf(
"Turn >> >> >> << << <<\n"
" to << << << >> >> >> 1-6: move r: restart q: quit\n\n");
print_it();
while (gets(str) != NULL)
{
ch = *str;
if (ch == 'q')
break;
else if (ch == 'r')
{
a[0] = 1;
a[1] = 2;
a[2] = 3;
a[3] = 0;
a[4] = 4;
a[5] = 5;
a[6] = 6;
printf("\n");
}
else if (ch >= '1' && ch <= '6')
{
ch -= '0';
for (i = 0; i < 7; i++)
if (a[i] == ch)
break;
if (a[i] < 4 && i < 6 && a[i+1] == 0)
{a[i+1] = a[i]; a[i] = 0;}
else if (a[i] < 4 && i < 5 && a[i+2] == 0)
{a[i+2] = a[i]; a[i] = 0;}
else if (a[i] > 3 && i > 0 && a[i-1] == 0)
{a[i-1] = a[i]; a[i] = 0;}
else if (a[i] > 3 && i > 1 && a[i-2] == 0)
{a[i-2] = a[i]; a[i] = 0;}
else
printf("\a");
}
else
printf("\a");
print_it();
if (a[0] + a[1] + a[2] == 15 && a[3] == 0)
{
printf("\b\b\bYOU WIN!\n");
break;
}
}
printf("\nBye.\n");
return 0;
}
void print_it(void)
{
int i;
printf(" ");
for (i = 0; i < 7; i++)
if (a[i] == 0)
printf(" ");
else if (a[i] < 4)
printf("%d>> ", a[i]);
else
printf("<<%d ", a[i]);
printf(" ");
}