#define ROWSIZE 12
#define COLSIZE 12
#define MINESIZE 64
#define SHOWINIT 0x80
#define MAKEMINE 0x40
#define SHOWMINE 0x20
#define MAKESUM 0x10
#define SHOWSUM 0x0F
#define CLEARINIT 0x7f
#define CLEARMINE 0xDF
typedef unsigned char MineType[ROWSIZE + 2][COLSIZE + 2];
void Init_Ar(MineType ar)
{
for (int i = 1; i <= ROWSIZE; ++i)
{
for (int j = 1; j <= COLSIZE; ++j)
{
ar[i][j] = ar[i][j] | SHOWINIT;
}
}
int i = 0;
while (i < MINESIZE)
{
int row = rand() % ROWSIZE + 1;
int col = rand() % COLSIZE + 1;
if (!(ar[row][col] & MAKEMINE))
{
ar[row][col] = ar[row][col] | MAKEMINE;
++i;
}
}
for (int r = 1; r<=ROWSIZE; ++r)
{
for (int c = 1; c <= COLSIZE; ++c)
{
if (!ar[r][c] & MAKEMINE)
{
unsigned sum = 0;
for (int i = r - 1; i <= r + 1; ++i)
{
for (int j = c - 1; j <= c + 1; ++j)
{
if (!ar[i][j] & MAKEMINE)
{
sum += 1;
}
}
}
ar[r][c] = ar[r][c] | MAKESUM;
ar[r][c] = ar[r][c] | (0x0f & sum);
}
}
}
}
void Print_Ar1(MineType ar)
{
for (int i = 1; i <= COLSIZE; ++i)
{
printf("%2d", i);
}
printf("\n");
for (int i = 1; i <= ROWSIZE; ++i)
{
printf("%2d", i);
for (int j = 1; j <= COLSIZE; ++j)
{
if (ar[i][j] & SHOWINIT)
{
printf("%2c", '@');
}
else if (ar[i][j] & SHOWMINE)
{
printf("%2c", '#');
}
else if (ar[i][j] & MAKEMINE)
{
printf("%2d", ar[i][j] & SHOWMINE);
}
}
printf("\n");
}
printf("\n");
}
int main()
{
MineType ar = {};
Init_Ar(ar);
int row, col;
char ch;
int i = 0;
while (i < MINESIZE)
{
Print_Ar1(ar);
printf("input row col select (#,0,@) \n");
scanf_s("%d %d %c", &row, &col, &ch);
if (row <1 || row > ROWSIZE || col <1 || col > COLSIZE)
{
printf("row col inut error \n");
continue;
}
if (ch == '#')
{
ar[row][col] &= CLEARINIT;
ar[row][col] |= SHOWMINE;
if (ar[row][col] & MAKEMINE)
{
++i;
}
}
else if (ch == '0')
{
if (ar[row][col] & MAKEMINE)
{
printf("炸死");
break;
}
ar[row][col] &= CLEARINIT;
}
}
}
int main()
{
int ar[] = { 1,2,3,4,5,6,7,6,5,3,2,1 };
int n = sizeof(ar) / sizeof(ar[0]);
int tmp = ar[0];
for (int i = 1; i < n; ++i)
{
tmp = tmp ^ ar[i];
}
printf("%d \n", tmp);
return 0;
}
|