跟着杨立祥老师的课程,为了完成扫雷的作业,打算先用DFS/BFS实现路径搜索的简单Demo。

生成迷宫:

/*
扫雷程序生成方砖
*/
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <windows.h>
#include <windowsx.h>
#include "resource.h" LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreInstance,
PSTR szCmdLine, int iCmdshow)
{
static TCHAR szAppName[] = TEXT("Maze");
HWND hwnd;
MSG msg;
WNDCLASS wndclass; wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName; if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("registered error"), szAppName, MB_ICONERROR);
}
hwnd = CreateWindow(szAppName,
TEXT("Mazes"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL); ShowWindow(hwnd, iCmdshow);
UpdateWindow(hwnd); while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
} LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HBITMAP hBitmap;
static int cxClient, cyClient, cxSource, cySource, xClick, yClick;
static int leftPos, rightPos, topPos, bottomPos;
BITMAP bitmap;
HDC hdc, hdcMem;
PAINTSTRUCT ps;
int x, y;
HINSTANCE hInstance; switch (message)
{
case WM_CREATE:
PlaySound(TEXT("start.wav"), NULL, SND_FILENAME | SND_ASYNC);
srand((unsigned)time(NULL));
hInstance = ((LPCREATESTRUCT)lParam)->hInstance;
hBitmap = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_BITMAP1));
GetObject(hBitmap, sizeof(BITMAP), &bitmap);
cxSource = bitmap.bmWidth;
cySource = bitmap.bmHeight / 3; return 0; case WM_SIZE:
cxClient = GET_X_LPARAM(lParam);
cyClient = GET_Y_LPARAM(lParam);
return 0; case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
hdcMem = CreateCompatibleDC(hdc);
SelectObject(hdcMem, hBitmap); //方砖的范围(左上角)
topPos = cySource * 2;
leftPos = cxSource * 2; for (y = topPos; y <= cyClient - cySource * 3; y += cySource)
{
for (x = leftPos; x <= cxClient - cxSource * 3; x += cxSource)
{ if (rand() % 4 == 1)
{
BitBlt(hdc, x, y, cxSource, cySource, hdcMem, 0, 0, SRCCOPY);
}
else
{
BitBlt(hdc, x, y, cxSource, cySource, hdcMem, 0, cySource, SRCCOPY);
}
}
}
rightPos = x - cxSource;
bottomPos = y - cySource;
//左上:右下为出口
BitBlt(hdc, cxSource * 2, cySource * 2, cxSource, cySource, hdcMem, 0, cySource, SRCCOPY);
BitBlt(hdc, rightPos, bottomPos, cxSource, cySource, hdcMem, 0, cySource, SRCCOPY); DeleteDC(hdcMem);
EndPaint(hwnd, &ps);
return 0;
case WM_LBUTTONDOWN:
xClick = GET_X_LPARAM(lParam);
yClick = GET_Y_LPARAM(lParam);
//无效区
if (xClick < leftPos || xClick > rightPos + cxSource || yClick < topPos || yClick > bottomPos + cySource)
{
break;
} for (y = topPos; y <= bottomPos; y += cySource)
{
if (yClick >= y && yClick <= y + cySource)
{
yClick = y;
break;
}
} for (x = leftPos; x <= rightPos; x += cxSource)
{
if (xClick >= x && xClick <= x + cxSource)
{
xClick = x;
break;
}
}
hdc = GetDC(hwnd);
hdcMem = CreateCompatibleDC(hdc);
SelectObject(hdcMem, hBitmap);
BitBlt(hdc, xClick, yClick, cxSource, cySource, hdcMem, 0, cySource * 2, SRCCOPY);
DeleteDC(hdcMem);
ReleaseDC(hwnd, hdc);
return 0;
case WM_DESTROY:
DeleteObject(hBitmap);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}

效果图:

最新文章

  1. nginx+webpy配置
  2. iOS中修改头部tabBarButton 默认按钮的颜色和默认字体颜色
  3. CSS层叠样式表的层叠是什么意思(转自知乎)
  4. oracle通过plsql导入dmp数据文件
  5. Framework 类库的事件编程
  6. 基于visual Studio2013解决C语言竞赛题之1076放鞭炮
  7. TOGAF架构内容框架之内容元模型(上)
  8. 聊聊JAVA中 String类为什么不可变
  9. linux上搭建ftp
  10. wxWidgets 在 Linux 下开发环境配置
  11. [转]jquery异步ajax与服务器通信过程中如何通过then方法链式传递多层数据
  12. 为什么企业需要IT资产管理
  13. [HihoCoder1393]网络流三&#183;二分图多重匹配
  14. CXF框架入门(重点)
  15. 开发创建XMPP“发布订阅”扩展(xmpp pubsub extend)
  16. nodejs 爬虫模板 map&amp;array 数据模型
  17. ruby字符串连接
  18. 从零开始的Python学习Episode 8——深浅拷贝
  19. Jquery 欲绑定事件
  20. YBT 5.4 状态压缩动态规划

热门文章

  1. 一文详解滑动平均法、滑动平均模型法(Moving average,MA)
  2. 使用Rancher Server部署本地多节点K8S集群
  3. Django 单表查询
  4. .net core 开车记:Data Protection Key 过期问题与登录页面访问慢
  5. 11 个最佳的 Python 编译器和解释器
  6. 深入浅出 JVM 系列(一)什么是 JVM?它处于什么位置?
  7. 让vue-router渲染为指定的标签
  8. React Hooks 实现和由来以及解决的问题
  9. java: integer number is too large
  10. postgresql gin索引使用