扫雷小游戏,NUM判断游戏的胜负时有时少加,有时又多加了,出了什么问题..
#include<stdio.h>#include<stdlib.h>
#include<time.h>
#include<graphics.h>
#include<windows.h>
#define hang 10
#define lie 10
#define lei 10
#define size 50
int NUM = 0;
int arr[hang+2][lie+2];
IMAGE graph[12];
void fun4(int m, int n);
void fun1()
{
int i, j,m,n,sum=0;
for (i = 0; i < hang+2; i++)
{
for (j = 0; j < lie+2 ; j++)
{
arr[i][j] = 0;
}
}
srand((unsigned int)time(NULL));
while (lei > sum)
{
m = 1 + rand() % (hang);
n = 1 + rand() % (lie);
if (arr[m][n] == 0)
{
arr[m][n] = -1;
sum++;
}
}
for (i = 1; i <= hang; i++)
{
for (j = 1; j <=lie; j++)
{
if (arr[i][j] == 0)
{
for (m = i - 1; m <= i + 1; m++)
{
for (n = j - 1; n <=j + 1;n++)
{
if (arr[m][n] == -1)
arr[i][j]++;
}
}
}
}
}
for (i = 1; i <= hang; i++)
{
for (j = 0; j <= lie; j++)
{
arr[i][j] += 20;
}
}
}
int fun2()
{
int m, n;
MOUSEMSG msg = {0};
while (1)
{
msg = GetMouseMsg();
switch (msg.uMsg)
{
case WM_LBUTTONDOWN:
m = msg.x / size + 1;
n = msg.y / size + 1;
if (arr[m][n] >= 19 && arr[m][n] <= 28)
{
if (arr[m][n] == 20)
{
fun4(m, n);
}
else
{
arr[m][n] -= 20;
NUM++;
}
return arr[m][n];
}
break;
case WM_RBUTTONDOWN:
m = msg.x / size + 1;
n = msg.y / size + 1;
if (arr[m][n] >= 19 && arr[m][n] <= 28)
{
arr[m][n] += 30;
}
else if (arr[m][n] > 30)
arr[m][n] -= 30;
return arr[m][n];
break;
}
}
}
void fun3()
{
int i, j;
for (i = 1; i <= hang; i++)
{
for (j = 1; j <= lie; j++)
{
if (arr[i][j] == -1)
putimage((i - 1)*size, (j - 1)*size, &graph[9]);
else if (arr[i][j] >= 0 && arr[i][j] <= 8)
putimage((i - 1)*size, (j - 1)*size, &graph[arr[i][j]]);
else if (arr[i][j] >= 19 && arr[i][j] < 28)
putimage((i - 1)*size, (j - 1)*size, &graph[11]);
else if (arr[i][j]>30)
putimage((i - 1)*size, (j - 1)*size, &graph[10]);
}
}
}
void fun4(int m,int n)
{
arr[m][n] -= 20;
NUM++;
int i, j;
for (i = m-1; i <= m+1; i++)
{
for (j = n-1; j <= n+1; j++)
{
if (m >= 1 && m <= hang&&n >= 1 && n <= lie)
{
if (arr[i][j] >= 19 && arr[i][j] <= 28)
{
if (arr[i][j] != 20)
{
arr[i][j] -= 20;
NUM++;
}
else
{
fun4(i, j);
}
}
}
}
}
}
int main()
{
HWND hand= initgraph(size*hang, size*lie);
loadimage(&graph[0], L"0.jpg", size, size);
loadimage(&graph[1], L"1.jpg", size, size);
loadimage(&graph[2], L"2.jpg", size, size);
loadimage(&graph[3], L"3.jpg", size, size);
loadimage(&graph[4], L"4.jpg", size, size);
loadimage(&graph[5], L"5.jpg", size, size);
loadimage(&graph[6], L"6.jpg", size, size);
loadimage(&graph[7], L"7.jpg", size, size);
loadimage(&graph[8], L"8.jpg", size, size);
loadimage(&graph[9], L"bomb0.jpg", size, size);
loadimage(&graph[10], L"flag.jpg", size, size);
loadimage(&graph[11], L"s.jpg", size, size);
fun1();
int i, j;
for (i = 1; i <= hang; i++)
{
for (j = 1; j <= lie; j++)
{
printf("%2d", arr[i][j]);
}
printf("\n");
}
while (1)
{
fun3();
if (fun2() == -1)
{
fun3();
MessageBox(hand, L"low", L"", MB_OK);
break;
}
if (hang*lie - NUM == lei)
{
fun3();
MessageBox(hand, L"win", L"", MB_OK);
break;
}
}
system("pause");
return 0;
}