📄 maze.cpp
字号:
// Maze.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
#include "Maze.h"
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_MAZE, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
if(!MazeChange(maze,mazeall[0]))
return FALSE;
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_MAZE);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_MAZE;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_WINLOGO);
return RegisterClassEx(&wcex);
}
//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
int x0,y0,xx,yy;
x0=y0=1;
xx=yy=19;
mazeshow[(x0-1)/2][(y0-1)/2].buttom=GATE;
mazeshow[(xx-1)/2][(yy-1)/2].buttom=EXIT;
char allmessage[100];
switch (message)
{
case WM_CREATE:
EnableMenuItem(GetMenu(hWnd),IDM_PLAY,MF_BYCOMMAND|MF_GRAYED);
EnableMenuItem(GetMenu(hWnd),IDM_GAME07,MF_BYCOMMAND|MF_GRAYED);
CheckMenuRadioItem(GetMenu(hWnd),IDM_GAME01,IDM_GAME07,IDM_GAME01,MF_BYCOMMAND);
game=IDM_GAME01;
break;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_GAME01:
case IDM_GAME02:
case IDM_GAME03:
case IDM_GAME04:
case IDM_GAME05:
case IDM_GAME06:
case IDM_GAME07:
selfset=0;
if(!MazeChange(maze,mazeall[wmId-IDM_GAME01]))
return 0;
game=wmId;
InvalidateRect(hWnd,NULL,1);
UpdateWindow(hWnd);
CheckMenuRadioItem(GetMenu(hWnd),IDM_GAME01,IDM_GAME07,wmId,MF_BYCOMMAND);
break;
case IDM_PRE:
if(game-1<IDM_GAME01)
break;
game--;
selfset=0;
i=j=0;
if(!MazeChange(maze,mazeall[game-IDM_GAME01]))
return 0;
InvalidateRect(hWnd,NULL,1);
UpdateWindow(hWnd);
CheckMenuRadioItem(GetMenu(hWnd),IDM_GAME01,IDM_GAME07,game,MF_BYCOMMAND);
break;
case IDM_NEXT:
if(game+1>IDM_GAME07)
break;
if(!game07&&game+1==IDM_GAME07)
break;
game++;
i=j=0;
selfset=0;
if(!MazeChange(maze,mazeall[game-IDM_GAME01]))
return 0;
InvalidateRect(hWnd,NULL,1);
UpdateWindow(hWnd);
CheckMenuRadioItem(GetMenu(hWnd),IDM_GAME01,IDM_GAME07,game,MF_BYCOMMAND);
break;
case IDM_ALL:
allwaycount=SearchAllWay(hWnd,x0,y0,xx,yy);
if(!allwaycount)
MessageBox(hWnd,"\n没有找到通路 ","失败",MB_OK|MB_ICONSTOP);
else if(allwaycount>0)
SetTimer(hWnd,waycount+2,50,NULL);
break;
case IDM_BEST:
best=SearchBestWay(hWnd,x0,y0,xx,yy);
if(best+1)
SetTimer(hWnd,1,50,NULL);
break;
case IDM_GAMESET:
selfset=1;
i=0; j=0;
MazeChange(maze,mazeall[7]);
MazeDraw(hWnd);
if(game07)
EnableMenuItem(GetMenu(hWnd),IDM_GAME07,MF_BYCOMMAND|MF_ENABLED);
EnableMenuItem(GetMenu(hWnd),IDM_PLAY,MF_BYCOMMAND|MF_ENABLED);
EnableMenuItem(GetMenu(hWnd),IDM_BEST,MF_BYCOMMAND|MF_GRAYED);
EnableMenuItem(GetMenu(hWnd),IDM_ALL,MF_BYCOMMAND|MF_GRAYED);
break;
case IDM_PLAY:
selfset=0;
game07=1;
game=IDM_GAME07;
MazeChange(mazeall[6],maze);
mazeshow[i][j].man=0;
mazeshow[(x0-1)/2][(y0-1)/2].man=1;
MazeDraw(hWnd);
i=0;j=0;
EnableMenuItem(GetMenu(hWnd),IDM_GAME07,MF_BYCOMMAND|MF_ENABLED);
EnableMenuItem(GetMenu(hWnd),IDM_PLAY,MF_BYCOMMAND|MF_GRAYED);
EnableMenuItem(GetMenu(hWnd),IDM_BEST,MF_BYCOMMAND|MF_ENABLED);
EnableMenuItem(GetMenu(hWnd),IDM_ALL,MF_BYCOMMAND|MF_ENABLED);
CheckMenuRadioItem(GetMenu(hWnd),IDM_GAME01,IDM_GAME07,IDM_GAME07,MF_BYCOMMAND);
break;
case IDM_RESET:
if(selfset==1)
MazeChange(maze,mazeall[7]);
else
MazeChange(maze,mazeall[game-IDM_GAME01]);
i=0; j=0;
MazeDraw(hWnd);
break;
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_KEYDOWN:
int vk;
vk=wParam;
if(!selfset)
{
switch(vk)
{
case VK_RIGHT:
if(!mazeshow[i][j].right)
{
mazeshow[i][j].man = 0;
mazeshow[i][j+1].man=1;
WayDraw(hWnd,i,j+1);
WayDraw(hWnd,i,j);
j++;
}
break;
case VK_DOWN:
if(!mazeshow[i][j].down)
{
mazeshow[i][j].man = 0;
mazeshow[i+1][j].man=1;
WayDraw(hWnd,i,j);
WayDraw(hWnd,i+1,j);
i++;
}
break;
case VK_LEFT:
if(!mazeshow[i][j].left)
{
mazeshow[i][j].man = 0;
mazeshow[i][j-1].man=1;
WayDraw(hWnd,i,j);
WayDraw(hWnd,i,j-1);
j--;
}
break;
case VK_UP:
if(!mazeshow[i][j].up)
{
mazeshow[i][j].man=0;
mazeshow[i-1][j].man=1;
WayDraw(hWnd,i,j);
WayDraw(hWnd,i-1,j);
i--;
}
break;
}
if(mazeshow[9][9].man == 1)
{
i=0;
j=0;
if(MessageBox(hWnd,"\n恭喜你!!你已成功走出!! ","祝贺",MB_OK|MB_ICONINFORMATION)==IDOK)
{
if(game==IDM_GAME07)
game=IDM_GAME01;
else if(game==IDM_GAME06&&!game07)
game=IDM_GAME01;
else
game++;
CheckMenuRadioItem(GetMenu(hWnd),IDM_GAME01,IDM_GAME07,game,MF_BYCOMMAND);
MazeChange(maze,mazeall[game-IDM_GAME01]);
MazeDraw(hWnd);
}
}
}
else if(selfset)
{
switch(vk)
{
case VK_UP:
if(i>0)
{
maze[2*(i-1)+1][2*j+1]=maze[2*i][2*j+1]=0;
mazeshow[i][j].up=mazeshow[i-1][j].down=0;
mazeshow[i][j].man=0;
mazeshow[i-1][j].man=1;
WayDraw(hWnd,i,j);
if(j<M-1)
WayDraw(hWnd,i-1,j+1);
WayDraw(hWnd,i-1,j);
i--;
}
break;
case VK_DOWN:
if(i<N-1)
{
maze[2*(i+1)+1][2*j+1]=maze[2*(i+1)][2*j+1]=0;
mazeshow[i][j].down=0;
mazeshow[i+1][j].up=0;
mazeshow[i][j].man=0;
mazeshow[i+1][j].man=1;
WayDraw(hWnd,i,j);
if(j<M-1)
WayDraw(hWnd,i,j+1);
WayDraw(hWnd,i+1,j);
i++;
}
break;
case VK_LEFT:
if(j>0)
{
maze[2*i+1][2*(j-1)+1]=maze[2*i+1][2*j]=0;
mazeshow[i][j].left=0;
mazeshow[i][j-1].right=0;
mazeshow[i][j].man=0;
mazeshow[i][j-1].man=1;
WayDraw(hWnd,i,j);
if(i>0)
WayDraw(hWnd,i-1,j);
WayDraw(hWnd,i,j-1);
j--;
}
break;
case VK_RIGHT:
if(j<M-1)
{
maze[2*i+1][2*(j+1)+1]=maze[2*i+1][2*(j+1)]=0;
mazeshow[i][j].right=0;
mazeshow[i][j+1].left=0;
mazeshow[i][j].man=0;
mazeshow[i][j+1].man=1;
WayDraw(hWnd,i,j);
if(i>0)
WayDraw(hWnd,i-1,j+1);
WayDraw(hWnd,i,j+1);
j++;
}
break;
}
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
RECT rt;
GetClientRect(hWnd, &rt);
DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
MazeDraw(hWnd);
EndPaint(hWnd, &ps);
break;
case WM_TIMER:
if(best>=0)
{
if(best)
mazeshow[(way[best].x-1)/2][(way[best].y-1)/2].buttom=PATH;
mazeshow[(way[best+1].x-1)/2][(way[best+1].y-1)/2].man=0;
mazeshow[(way[best].x-1)/2][(way[best].y-1)/2].man=1;
WayDraw(hWnd,(way[best+1].x-1)/2,(way[best+1].y-1)/2);
WayDraw(hWnd,(way[best].x-1)/2,(way[best].y-1)/2);
best--;
if(mazeshow[9][9].man == 1)
{
KillTimer(hWnd,1);
MessageBox(hWnd,"\n最短路径已经找到 ","成功",MB_OK|MB_ICONINFORMATION);
}
}
if(allwaycount>0&&waycount<=allwaycount)
{
if(mazeshow[9][9].man==1)
{
KillTimer(hWnd,waycount+2);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -