这是跟着教程做的一个扫雷,输出的数字没有任何问题,但对应的游戏区却是全黑的
代码如下,求大佬指点
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <graphics.h>
#define R 10
#define C 10
#define M 20
#define SIZE 50
IMAGE img[12];
int count = 0;
int map[R + 2][C + 2];
void f1()
{
srand((unsigned)time(NULL));
for (int i = 0; i <= R + 1; i++)
{
for (int j = 0; j <= C + 1; j++)
{
map[i][j] = 0;
}
}
int n = 0;
while (n < M)
{
int x = rand() % R + 1;//保证在有效输出区域布置雷
int y = rand() % C + 1;
if (map[x][y] == 0)
{
map[x][y] = -1;
n++;
}
}
for (int i = 1; i <= R; i++)
{
for (int j = 1; j <= C; j++)
{
if (map[i][j] != -1)
{
for (int m = i - 1; m <= i + 1; m++)
{
for (int n = j - 1; n <= j + 1; n++)
{
if (map[m][n] == -1)
{
map[i][j]++;
}
}
}
}
}
}
for (int i = 1; i <= R; i++)
{
for (int j = 1; j <= C; j++)
{
map[i][j] += 20;
}
}
}
void f2()
{
for (int i = 1; i <= R; i++)
{
for (int j = 1; j <= C; j++)
{
printf("%2d ", map[i][j]);
if (map[i][j] == -1)
{
putimage((i - 1) * SIZE, (j - 1) * SIZE, &img[10]);
}
else if (map[i][j] >= 0 && map[i][j] <= 8)
{
putimage((i - 1) * SIZE, (j - 1) * SIZE, &img[map[i][j]]);
}
else if (map[i][j] >= 19 && map[i][j] <= 28)
{
putimage((i - 1) * SIZE, (j - 1) * SIZE, &img[9]);
}
else if (map[i][j] >= 30)
{
putimage((i - 1) * SIZE, (j - 1) * SIZE, &img[11]);
}
}
printf("\n");
}
}
void Open(int r, int c)
{
map[r][c] -= 20;
count++;
for (int p = r - 1; p <= r + 1; p++)
{
for (int q = c - 1; q <= c + 1; q++)
{
if (p >= 1 && p <= R && q >= 1 && q <= C)
{
if (map[p][q] >= 19 && map[p][q] <= 28)
{
if (map[p][q] != 20)
{
map[p][q] -= 20;
count++;
}
else
{
Open(p, q);
}
}
}
}
}
}
int Play()
{
MOUSEMSG msg = { 0 };
int r, c;
while (1)
{
msg = GetMouseMsg();
switch (msg.uMsg)
{
case WM_LBUTTONDOWN:
r = msg.x / SIZE + 1;
c = msg.y / SIZE + 1;
if (map[r][c] >= 19 && map[r][c] <= 28)
{
if (map[r][c] == 20)
{
Open(r, c);
}
else
{
map[r][c] -= 20;
count++;
}
}
break;
case WM_RBUTTONDOWN:
r = msg.x / SIZE + 1;
c = msg.y / SIZE + 1;
if (map[r][c] >= 19 && map[r][c] <= 28)
{
map[r][c] += 40;
}
else if (map[r][c] > 30)
{
map[r][c] -= 50;
}
return map[r][c];
break;
}
}
}
int main()
{
HWND hwnd = initgraph(R * SIZE, C * SIZE, SHOWCONSOLE);
loadimage(&img[0], L"0.jpg", SIZE, SIZE);
loadimage(&img[1], L"1.jpg", SIZE, SIZE);
loadimage(&img[2], L"2.jpg", SIZE, SIZE);
loadimage(&img[3], L"3.jpg", SIZE, SIZE);
loadimage(&img[4], L"4.jpg", SIZE, SIZE);
loadimage(&img[5], L"5.jpg", SIZE, SIZE);
loadimage(&img[6], L"6.jpg", SIZE, SIZE);
loadimage(&img[7], L"7.jpg", SIZE, SIZE);
loadimage(&img[8], L"8.jpg", SIZE, SIZE);
loadimage(&img[9], L"9.jpg", SIZE, SIZE);
loadimage(&img[10], L"10.jpg", SIZE, SIZE);
loadimage(&img[11], L"11.jpg", SIZE, SIZE);
f1();
while (1)
{
f2();
if (Play() == -1)
{
f2();
MessageBox(hwnd, L"you are wrong!", L"", MB_OK);
break;
}
if (R * C - M == count)
{
f2();
MessageBox(hwnd, L"you win!", L"", MB_OK);
break;
}
}
closegraph();
return 0;
}