鱼C论坛

 找回密码
 立即注册
查看: 2431|回复: 4

Windows编程写了个扫雷,但是退出的时候会出现Stack Overflow求解决

[复制链接]
发表于 2014-7-26 14:50:34 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #define UNICODE
  2. #include <Windows.h>
  3. #include <strsafe.h>
  4. #include <windowsx.h>
  5. #include <time.h>

  6. #define HEIGHT 15
  7. #define WIDTH 15
  8. #define MAX_STRING 256

  9. #define LAND L'○'
  10. #define MINE L'●'
  11. #define SAFE L' '

  12. TCHAR szMatrix[MAX_STRING][MAX_STRING];                //扫雷游戏区域
  13. size_t nSide;                        //区域边长
  14. BOOL bMine[MAX_STRING][MAX_STRING];        //记录雷的位置
  15. UINT uMine[MAX_STRING][MAX_STRING];        //记录周围雷的个数
  16. int nMine;        //记录雷的个数

  17. LRESULT CALLBACK WindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam);

  18. int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR szCmdLine,INT nCmdShow)
  19. {
  20.         static TCHAR szClassName[] = TEXT("MyClass");
  21.         WNDCLASS wndclass;
  22.         HWND hwnd;
  23.         MSG msg;

  24.         wndclass.cbClsExtra = 0;
  25.         wndclass.cbWndExtra = 0;
  26.         wndclass.hbrBackground = (HBRUSH)GetStockObject(DC_BRUSH);
  27.         wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
  28.         wndclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
  29.         wndclass.hInstance = hInstance;
  30.         wndclass.lpfnWndProc = WindowProc;
  31.         wndclass.lpszClassName = szClassName;
  32.         wndclass.lpszMenuName = NULL;
  33.         wndclass.style = CS_HREDRAW | CS_VREDRAW;
  34.        
  35.         if (!RegisterClass(&wndclass))
  36.         {
  37.                 MessageBox(NULL,TEXT("本程序必须在WINNT下运行!"),TEXT("警告"),MB_OK);
  38.                 return 0;
  39.         }

  40.         nSide = 20;
  41.         nMine = 40;
  42.         for (int i=0;i<nSide;i++)
  43.         {
  44.                 for (int j=0;j<nSide;j++)
  45.                 {
  46.                         szMatrix[i][j] = LAND;
  47.                 }
  48.         }

  49.         srand((unsigned)time(NULL));
  50.         //下面随机出现nMine颗雷
  51.         for (int i=0;i<nMine;i++)
  52.         {
  53.                 int x = rand()%nSide;
  54.                 int y = rand()%nSide;
  55.                 if (!bMine[x][y])
  56.                 {
  57.                         bMine[x][y] = TRUE;
  58.                 }
  59.                 else
  60.                 {
  61.                         i--;
  62.                 }
  63.         }
  64.         //下面计算每一块周围有多少颗雷
  65.         for (int i=0;i<nSide;i++)
  66.         {
  67.                 for (int j=0;j<nSide;j++)
  68.                 {
  69.                         if (bMine[i][j])        continue;                //当前块是雷
  70.                         int nCounts = 0;
  71.                         for (int m=i-1;m<=i+1;m++)
  72.                         {
  73.                                 if (m<0 || m>=nSide)        continue;
  74.                                 for (int n=j-1;n<=j+1;n++)
  75.                                 {
  76.                                         if (n<0 || n>=nSide)        continue;
  77.                                         if (bMine[m][n])        nCounts++;
  78.                                 }
  79.                         }
  80.                         //存储雷数nCounts
  81.                         uMine[i][j] = nCounts;
  82.                 }
  83.         }

  84.         hwnd = CreateWindow(szClassName,
  85.                                                 TEXT("BY:落叶"),
  86.                                                 WS_OVERLAPPEDWINDOW,
  87.                                                 CW_USEDEFAULT,
  88.                                                 CW_USEDEFAULT,
  89.                                                 CW_USEDEFAULT,
  90.                                                 CW_USEDEFAULT,
  91.                                                 NULL,
  92.                                                 NULL,
  93.                                                 hInstance,
  94.                                                 NULL);

  95.         ShowWindow(hwnd,nCmdShow);
  96.         UpdateWindow(hwnd);

  97.         while (GetMessage(&msg,NULL,0,0))
  98.         {
  99.                 TranslateMessage(&msg);
  100.                 DispatchMessage(&msg);
  101.         }

  102.         return msg.wParam;
  103. }

  104. void SwipeLand(HDC hdc,int x,int y,BOOL bRecord[MAX_STRING][MAX_STRING]);        //计算并现出没雷的连续区域

  105. LRESULT CALLBACK WindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
  106. {
  107.         HDC hdc;
  108.         PAINTSTRUCT ps;
  109.         RECT rect;

  110.         int i,j;        //循环变量
  111.         int xPos,yPos;        //记录鼠标位置
  112.         int x,y;        //记录地雷下标

  113.         switch (uMsg)
  114.         {
  115.                 case WM_PAINT:
  116.                         hdc = BeginPaint(hwnd,&ps);
  117.                         //绘制扫雷区域
  118.                         for (i=0;i<nSide;i++)
  119.                         {
  120.                                 for (j=0;j<nSide;j++)
  121.                                 {
  122.                                         TextOut(hdc,i*WIDTH,j*HEIGHT,&szMatrix[i][j],1);
  123.                                 }
  124.                         }
  125.                         EndPaint(hwnd,&ps);
  126.                         break;
  127.                 case WM_LBUTTONUP:
  128.                         xPos = GET_X_LPARAM(lParam);
  129.                         yPos = GET_Y_LPARAM(lParam);
  130.                         x = xPos/WIDTH;
  131.                         y = yPos/HEIGHT;
  132.                         if (y < nSide && x < nSide)
  133.                         {
  134.                                 hdc = GetDC(hwnd);
  135.                                 //判断是否有雷
  136.                                 if (bMine[x][y])
  137.                                 {
  138.                                         //先显示雷的位置然后退出
  139.                                         for (i=0;i<nSide;i++)
  140.                                                 for (j=0;j<nSide;j++)
  141.                                                         if (bMine[i][j])
  142.                                                                 TextOut(hdc,i*WIDTH,j*HEIGHT,TEXT("●"),1);
  143.                                         MessageBox(hwnd,TEXT("游戏结束!"),TEXT("提示"),MB_OK);
  144.                                         ReleaseDC(hwnd,hdc);
  145.                                         DestroyWindow(hwnd);
  146.                                         break;
  147.                                 }
  148.                                 BOOL bRecord[MAX_STRING][MAX_STRING] = {FALSE};        //记录是否已遍历
  149.                                 SwipeLand(hdc,x,y,bRecord);
  150.                                 ReleaseDC(hwnd,hdc);
  151.                         }
  152.                         break;
  153.                 case WM_CLOSE:
  154.                         if (IDYES == MessageBox(hwnd,TEXT("确认退出扫雷?"),TEXT("提示"),MB_YESNO | MB_ICONINFORMATION))
  155.                                 DestroyWindow(hwnd);
  156.                         break;
  157.                 case WM_DESTROY:
  158.                         PostQuitMessage(0);
  159.                         break;
  160.                 default:
  161.                         return DefWindowProc(hwnd,uMsg,wParam,lParam);
  162.         }

  163.         return 0;
  164. }

  165. void SwipeLand(HDC hdc,int x,int y,BOOL bRecord[MAX_STRING][MAX_STRING])        //计算并现出没雷的连续区域
  166. {
  167.         bRecord[x][y] = TRUE;
  168.         if (x<0 || x>=nSide || y<0 || y>=nSide)        return;
  169.         static TCHAR szMine[5];        //为显示雷做准备
  170.         //现出当前位置
  171.         StringCchPrintf(szMine,5,TEXT(" "));
  172.         TextOut(hdc,x*WIDTH,y*HEIGHT,szMine,1);
  173.         if (uMine[x][y]>0)
  174.         {
  175.                 StringCchPrintf(szMine,5,TEXT("%d"),uMine[x][y]);
  176.                 TextOut(hdc,x*WIDTH,y*HEIGHT,szMine,1);
  177.                 return;
  178.         }
  179.         //现出上下左右4个方向
  180.         if (!bRecord[x][y-1])        SwipeLand(hdc,x,y-1,bRecord);
  181.         if (!bRecord[x][y+1])        SwipeLand(hdc,x,y+1,bRecord);
  182.         if (!bRecord[x-1][y])        SwipeLand(hdc,x-1,y,bRecord);
  183.         if (!bRecord[x+1][y])        SwipeLand(hdc,x+1,y,bRecord);
  184. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2014-7-26 18:01:33 | 显示全部楼层
好高端,大气
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-7-28 20:09:13 | 显示全部楼层
好流弊的样子
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-8-7 00:44:39 | 显示全部楼层
我也在写。。不过是淡季后图片的重绘问题。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-8-13 17:53:18 | 显示全部楼层
把问题顶起来
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 18:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表