组图小游戏(vc)

/*
*Copyright(c)2010, 福建星网锐捷通讯股份有限公司
*All rights reserved.
*
*Name:            HuaRongDao.cpp
*Description:     实现"华容道"
*
*Author:          xuwenfa
*Date:            2010-3-27
*/

#include "stdafx.h"
#include "resource.h"
#include <time.h>
#include <Mmsystem.h>
#include "HuaRongDao.h"
#pragma comment( lib, "winmm.lib" ) /*加入winmm.lib库*/

HINSTANCE hInst;           /*本引用程序实例*/
TCHAR szTitle[MAX_LOADSTRING];        /*窗口标题*/
TCHAR szWindowClass[MAX_LOADSTRING];      /*窗口类名*/
RECT g_wndRect = {100, 50, 900, 650};              /*窗口大小*/
RECT picRect = {20, 20, 500, 500};                 /*图片区*/
RECT smallPicRect = {550, 20, 750, 220};           /*缩图区*/
RECT ctlRect = {550, 20, 750, 510};                /*控制区*/
HBITMAP g_hBitmap;                                 /*图片*/
BITMAP g_bmpInfo;                                  /*图片信息*/
HBITMAP g_btnNew[2];                               /*New按钮*/
BITMAP g_btnNewInfo[2];                            /*New按钮信息*/
HBITMAP g_btnAuto[2];                              /*Auto按钮*/
BITMAP g_btnAutoInfo[2];                           /*Auto按钮信息*/
int g_Map[MAX][MAX];                               /*二维数组*/  
int g_MapInfo[MAX][MAX];                           /*数组信息(是否按下)*/   
HDC doubleDC;                                      /*双缓冲*/       
bool g_Success = true;                             /*成功标志*/
int g_Segment = EASY;                              /*段数*/ 
int g_step = 0;                                    /*步数*/
int g_EasyMinStep = 999;                           /*Easy最小步数*/
int g_MiddleMinStep = 999;                         /*Middle最小步数*/
int g_HardMinStep = 999;                           /*Hard最小步数*/
int g_Check = 2;                                   /*菜单项的check标志*/
int g_btnInfo[2] = {0, 0};                         /*按钮标志*/
bool g_Automode = false;                           /*自动排列模式*/

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
 MSG msg;                               /*消息*/
 HACCEL hAccelTable;                    /*快速表*/
    InitVariable();                        /*初始化全局变量*/

 LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);        /*载入字符串*/
 LoadString(hInstance, IDC_HUARONGDAO, szWindowClass, MAX_LOADSTRING);
 MyRegisterClass(hInstance);            /*注册窗口类*/

 if (!InitInstance (hInstance, nCmdShow))  /*实例化窗口,并初始化窗口*/
 {
  return FALSE;
 }
 hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_HUARONGDAO);

 while (GetMessage(&msg, NULL, 0, 0))
 {
  if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
  {
   TranslateMessage(&msg);
   DispatchMessage(&msg);
  }
 }

 return msg.wParam;
}
/*
*Name:       MyRegisterClass
*Input:      HINSTANCE hInstance
*Output:    
*Return:     ATOM
*/
ATOM MyRegisterClass(HINSTANCE hInstance)
{
 WNDCLASSEX wcex;

 wcex.cbSize = sizeof(WNDCLASSEX);

 wcex.style   = CS_HREDRAW | CS_VREDRAW;
 wcex.lpfnWndProc = (WNDPROC)MyWndProc;
 wcex.cbClsExtra  = 0;
 wcex.cbWndExtra  = 0;
 wcex.hInstance  = hInstance;
 wcex.hIcon   = LoadIcon(hInstance, (LPCTSTR)IDI_HUARONGDAO);
 wcex.hCursor  = LoadCursor(NULL, IDC_ARROW);
 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
 wcex.lpszMenuName = (LPCSTR)IDC_HUARONGDAO;
 wcex.lpszClassName = szWindowClass;
 wcex.hIconSm  = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

 return RegisterClassEx(&wcex);
}
/*
*Name:     InitInstace
*Input:    HINSTANCE hInstance,
           int nCmdShow
*Output: 
*Return:   BOOL
*Description: 创建窗口,并显示更新
*/
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance;

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX,
      g_wndRect.left, g_wndRect.top, g_wndRect.right - g_wndRect.left,
   g_wndRect.bottom - g_wndRect.top, NULL, NULL, hInstance, NULL);      /*创建窗口*/

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);                  /*显示窗口*/
   UpdateWindow(hWnd);                          /*更新窗口*/

   return TRUE;
}
/*
*Name:          MyWndProc
*Input:         HWND hWnd,
                UINT message,
    WPARAM wParam,
    LPARAM lParam
*Output:
*Return:        LRESULT
*Description:   消息出来程序
*/
LRESULT CALLBACK MyWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
 int wmId, wmEvent;
 PAINTSTRUCT ps;
 HDC hdc;
 HMENU hSubMenu;
 int x = 0;
 int y = 0;
 switch (message)
  {
  case WM_COMMAND:
   wmId    = LOWORD(wParam);
   wmEvent = HIWORD(wParam);
   switch (wmId)                   /*响应menu的按钮*/
   {
    case IDM_ABOUT:             /*响应关于对话框*/
       DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
       break;
    case IDM_QUIT:              /*退出*/
     DestroyWindow(hWnd);
     break;
    case IDM_HELP:              /*响应帮助对话框*/       
     DialogBox(hInst, (LPCTSTR)IDD_HELP, hWnd, (DLGPROC)Help);
     break;
    case IDM_EASY:              /*响应Easy*/
     g_Segment = EASY;       /*设置段数*/
     g_Check = 2;           
     InitMap();              /*初始化地图*/
     InitWindow(hWnd);       /*更新窗口*/
     g_Success = true;
     break;
    case IDM_MIDDLE:            /*响应Middle*/
     g_Segment = MIDDLE;
     g_Check = 3;
     InitMap();
     InitWindow(hWnd);
     g_Success = true;
     break;
    case IDM_HARD:              /*响应Hard*/
     g_Segment = HARD;
     g_Check = 4;
     InitMap();
     InitWindow(hWnd);
     g_Success = true;
     break; 
    case IDM_NEW:               /*响应New*/
     RandMap();
     InitWindow(hWnd);
        g_Success = false;
     break;
    default:
       return DefWindowProc(hWnd, message, wParam, lParam);
   }
   break;
  case WM_PAINT:                  /*窗口重绘*/
   hdc = BeginPaint(hWnd, &ps);
   RECT rt;
   GetClientRect(hWnd, &rt);  
   InitWindow(hWnd);
   EndPaint(hWnd, &ps);
   break;
  case WM_INITMENUPOPUP:         /*响应子菜单弹出*/
   hSubMenu = (HMENU)wParam;  /*获取子菜单指针*/
   switch (lParam)           
   {
   case 0:
    CheckMenuItem(hSubMenu, 2, MF_BYPOSITION | MF_UNCHECKED);
    CheckMenuItem(hSubMenu, 3, MF_BYPOSITION | MF_UNCHECKED);
    CheckMenuItem(hSubMenu, 4, MF_BYPOSITION | MF_UNCHECKED);
    CheckMenuItem(hSubMenu, g_Check, MF_BYPOSITION | MF_CHECKED); /*设置check标志*/
    break;
   default:
    break;
   }
   break;
  case WM_LBUTTONDOWN:                   /*鼠标左键按下*/
   x = LOWORD(lParam);                /*获取鼠标x*/
   y = HIWORD(lParam);                /*获取鼠标y*/
   if (x > ctlRect.left +10 && x < ctlRect.left + BTN_WIDTH + 10 &&
    y > smallPicRect.bottom + 20 && y < smallPicRect.bottom + 20 +BTN_HEIGHT && !g_Automode) /*判断是否点击new按钮*/
   { 
    g_btnInfo[0] = 1;              /*new按钮按下标志*/
    RandMap();                     /*随机化地图*/
    g_Success = false;
    InitWindow(hWnd);
  
   }
   if (x > ctlRect.left + BTN_WIDTH +30 && x < ctlRect.left + 2*BTN_WIDTH +30 &&
    y > smallPicRect.bottom + 20 && y < smallPicRect.bottom + 20 +BTN_HEIGHT && !g_Success) /*判断是否点击auto按钮*/
   { 
    g_btnInfo[1] = 1;              /*auto按钮按下标志*/
    SetTimer(hWnd, 0, 200, NULL);  /*设置定时器*/
    InitWindow(hWnd);
    g_Automode = true;             /*标示自动模式*/
   }
   if (!g_Success && !g_Automode)    
   {
    OnLButtonDown(hWnd, LOWORD(lParam), HIWORD(lParam));
    Exchange(hWnd, LOWORD(lParam), HIWORD(lParam));    /*交换*/
   }
   break;
  case WM_TIMER:                      /*定时器响应*/
   switch(wParam)
   {
   case 0:
    if (!g_Success)
    {
     AutoMode(hWnd);
     PlaySound("Exchange.wav", NULL, SND_FILENAME | SND_ASYNC);
     InitWindow(hWnd);
    }
    break;
   default:
    break;
   }
   break;
  case WM_LBUTTONUP:              /*鼠标左键松开*/
   OnLButtonUp(hWnd);         
   break;
  case WM_DESTROY:                /*窗口销毁*/    
   DeleteObject(g_hBitmap);
   DeleteObject(g_btnAuto[0]);
   DeleteObject(g_btnAuto[1]);
   DeleteObject(g_btnNew[0]);
   DeleteObject(g_btnNew[1]);
   PostQuitMessage(0);       /*发出接收WM_QUIT消息*/
   break;
  default:
   return DefWindowProc(hWnd, message, wParam, lParam);  /*返回默认的窗口处理函数*/
   }
   return 0;
}
/*
*Name:       About
*Input:      HWND hDlg,
             UINT message,
    WPARAM wParam,
    LPARAM lParam
*Output:   
*Return:     LRESULT
*Description: 相应关于对话框
*/
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
 switch (message)
 {
  case WM_INITDIALOG:             /*对话框初始化*/
    return TRUE;

  case WM_COMMAND:                /*按键处理*/
   if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
   {
    EndDialog(hDlg, LOWORD(wParam));  /*退出对话框*/
    return TRUE;
   }
   break;
 }
    return FALSE;
}
/*
*Name:       Help
*Input:      HWND hDlg,
             UINT message,
    WPARAM wParam,
    LPARAM lParam
*Output:   
*Return:     LRESULT
*Description: 相应帮助对话框
*/
LRESULT CALLBACK Help(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
 switch (message)
 {
 case WM_INITDIALOG:             /*对话框初始化*/
  return TRUE;
  
 case WM_COMMAND:                /*按键处理*/
  if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  {
   EndDialog(hDlg, LOWORD(wParam));  /*退出对话框*/
   return TRUE;
  }
  break;
 }
    return FALSE;
}

/*
*Name:      InitWindow
*Input:     HWND hWnd
*OUtput:   
*Return:    void
*Description: 绘制场景,放置图片,采用双缓冲模式
*/
void InitWindow(HWND hWnd)
{
 HDC hdc;          /*目前dc*/
 RECT rect;        /*客户区域*/
 HDC memdc;        /*图片dc*/
 int picWidth = (picRect.right - picRect.left)/g_Segment;   /*图片宽度*/
 int picHeight = (picRect.bottom - picRect.top)/g_Segment;  /*图片长度*/
 int n = 0;
 int k = 0;
 int m = g_Segment*g_Segment;
 HBITMAP oldhBitmap;  /*存放老hbitmap*/
 HBITMAP dBmp;        /*双缓冲hbitmap*/
 HBITMAP olddBmp;     /*存放老缓冲hbitmap*/
 char str[100] = "";      
 hdc = GetDC(hWnd);   /*获取屏幕dc*/
 doubleDC = CreateCompatibleDC(hdc);  /*让缓冲dc与屏幕兼容*/
 GetClientRect(hWnd, &rect);          /*获取客户区域*/
 dBmp = CreateCompatibleBitmap(hdc, rect.right - rect.left, rect.bottom - rect.top); /*创建与屏幕dc相兼容的hbitmap*/
 olddBmp = (HBITMAP)SelectObject(doubleDC, dBmp);  /*选进dBmp*/
 Rectangle(
  doubleDC,
  rect.left,
  rect.top,
  rect.right,
  rect.bottom);                        /*整个客户区填充白色*/

 Rectangle(
  doubleDC,
  picRect.left - PIC_SPACE,
  picRect.top - PIC_SPACE,
  picRect.right + g_Segment*PIC_SPACE,
  picRect.bottom + g_Segment*PIC_SPACE);/*绘制图片区外框*/

 Rectangle(
  doubleDC,
  ctlRect.left - PIC_SPACE,
  ctlRect.top - PIC_SPACE,
  ctlRect.right + PIC_SPACE,
  ctlRect.bottom + PIC_SPACE);       /*绘制控制区外框*/

 Rectangle(
  doubleDC,
  smallPicRect.left - PIC_SPACE,
  smallPicRect.top - PIC_SPACE,
  smallPicRect.right + PIC_SPACE,
  smallPicRect.bottom + PIC_SPACE);   /*绘制缩略图外框*/  
 Rectangle(
  doubleDC,
  smallPicRect.left - PIC_SPACE,
  smallPicRect.bottom + 100,
  smallPicRect.right + PIC_SPACE,
  ctlRect.bottom + PIC_SPACE);        /*绘制排行榜区外框*/
 /*显示图片*/
 memdc = CreateCompatibleDC(doubleDC);
 oldhBitmap = (HBITMAP)SelectObject(memdc, g_hBitmap);
 SetStretchBltMode(doubleDC, HALFTONE);   /*选择stretchBlt的mode,以防止失真*/                 

 while(n < m)                             /*根据g_Map映射图片各个区域*/
 {
  k = g_Map[n/g_Segment][n%g_Segment]-1;
  StretchBlt(
   doubleDC,
   picRect.left + (n % g_Segment) * (picWidth + PIC_SPACE),
   picRect.top + (n / g_Segment) * (picHeight + PIC_SPACE),
   picWidth,
   picHeight,
   memdc,
   (k % g_Segment) * g_bmpInfo.bmWidth/g_Segment,
   (k / g_Segment) * g_bmpInfo.bmHeight/g_Segment,
   g_bmpInfo.bmWidth/g_Segment,
   g_bmpInfo.bmHeight/g_Segment,
   SRCCOPY); 
  n++;
 }

 StretchBlt(
     doubleDC,
  smallPicRect.left,
  smallPicRect.top,
  smallPicRect.right - smallPicRect.left,
  smallPicRect.bottom - smallPicRect.top,
  memdc,
  0,
  0,
  g_bmpInfo.bmWidth,
  g_bmpInfo.bmHeight,
  SRCCOPY);                            /*显示缩略图*/
    SelectObject(memdc, oldhBitmap);
 oldhBitmap = (HBITMAP)SelectObject(memdc, g_btnNew[g_btnInfo[0]]);
 StretchBlt(
  doubleDC,
  ctlRect.left + 10,
  smallPicRect.bottom + 20,
  BTN_WIDTH,
  BTN_HEIGHT,
  memdc,
  0,
  0,
  g_btnNewInfo[g_btnInfo[0]].bmWidth,
  g_btnNewInfo[g_btnInfo[0]].bmHeight,
  SRCCOPY);                           /*显示New按钮*/
 SelectObject(memdc, oldhBitmap);
 oldhBitmap = (HBITMAP)SelectObject(memdc, g_btnAuto[g_btnInfo[1]]);
 StretchBlt(
  doubleDC,
  ctlRect.left + BTN_WIDTH + 30,
  smallPicRect.bottom + 20,
  BTN_WIDTH,
  BTN_HEIGHT,
  memdc,
  0,
  0,
  g_btnAutoInfo[g_btnInfo[1]].bmWidth,
  g_btnAutoInfo[g_btnInfo[1]].bmHeight,
  SRCCOPY);                         /*显示Auto按钮*/
 wsprintf(str, "步数: %d   ", g_step);
 TextOut(doubleDC, ctlRect.left + 50, smallPicRect.bottom + 70, str, strlen(str));
 wsprintf(str, "最好成绩(最少步数)");
 TextOut(doubleDC, ctlRect.left + 30, smallPicRect.bottom + 110, str, strlen(str));
 wsprintf(str, "Easy  :     %d    ", g_EasyMinStep);
 TextOut(doubleDC, ctlRect.left + 10, smallPicRect.bottom + 150, str, strlen(str));
 wsprintf(str, "Middle:     %d    ", g_MiddleMinStep);
 TextOut(doubleDC, ctlRect.left + 10, smallPicRect.bottom + 200, str, strlen(str));
 wsprintf(str, "Hard  :     %d    ", g_HardMinStep);
 TextOut(doubleDC, ctlRect.left + 10, smallPicRect.bottom + 250, str, strlen(str));

 BitBlt(hdc, 0, 0, rect.right - rect.left, rect.bottom - rect.top, doubleDC, 0, 0, SRCCOPY);  /*将缓存dc拷到屏幕上*/

 SelectObject(memdc, oldhBitmap);
 SelectObject(doubleDC, olddBmp);
 /*删除对象*/
 DeleteObject(doubleDC);
 DeleteObject(memdc);
 DeleteObject(dBmp);
 DeleteObject(olddBmp);
 DeleteObject(oldhBitmap);
 DeleteObject(hdc);
}
/*
*Name:     InitVariable()
*Input:
*OUtput:
*Return:   void
*Description:  初始化变量,加载图片
*/
void InitVariable()
{
 WIN32_FIND_DATA file;
 HANDLE handle;

 memset(g_Map, 0, sizeof(g_Map));
 memset(g_MapInfo, 0,sizeof(g_MapInfo));
 InitMap();

 handle = FindFirstFile("*.bmp", &file);
 if (handle == INVALID_HANDLE_VALUE)                                      /*没有找到文件,就退出*/
 {
  PlaySound("Error.wav", NULL, SND_FILENAME | SND_ASYNC);
  MessageBox(NULL,  "该目录下没有.bmp文件,请放入图片!", "提示", MB_OK);
  exit(1);
 }
 else
 {
  g_hBitmap = (HBITMAP)LoadImage(hInst, file.cFileName, IMAGE_BITMAP,
   0, 0, LR_LOADFROMFILE | LR_DEFAULTCOLOR | LR_DEFAULTSIZE);       /*载入图片*/
  GetObject(g_hBitmap, sizeof(BITMAP), (LPVOID)&g_bmpInfo);            /*获取图片信息*/
 }
 g_btnNew[0] = (HBITMAP)LoadImage(hInst, "rec/new1.bmp", IMAGE_BITMAP,
   0, 0, LR_LOADFROMFILE | LR_DEFAULTCOLOR | LR_DEFAULTSIZE);       /*载入图片*/
    GetObject(g_btnNew[0], sizeof(BITMAP), (LPVOID)&g_btnNewInfo[0]);            /*获取图片信息*/
 g_btnNew[1] = (HBITMAP)LoadImage(hInst, "rec/new2.bmp", IMAGE_BITMAP,
  0, 0, LR_LOADFROMFILE | LR_DEFAULTCOLOR | LR_DEFAULTSIZE);       /*载入图片*/
    GetObject(g_btnNew[1], sizeof(BITMAP), (LPVOID)&g_btnNewInfo[1]);            /*获取图片信息*/

 g_btnAuto[0] = (HBITMAP)LoadImage(hInst, "rec/auto1.bmp", IMAGE_BITMAP,
  0, 0, LR_LOADFROMFILE | LR_DEFAULTCOLOR | LR_DEFAULTSIZE);       /*载入图片*/
    GetObject(g_btnAuto[0], sizeof(BITMAP), (LPVOID)&g_btnAutoInfo[0]);            /*获取图片信息*/
 g_btnAuto[1] = (HBITMAP)LoadImage(hInst, "rec/auto2.bmp", IMAGE_BITMAP,
  0, 0, LR_LOADFROMFILE | LR_DEFAULTCOLOR | LR_DEFAULTSIZE);       /*载入图片*/
    GetObject(g_btnAuto[1], sizeof(BITMAP), (LPVOID)&g_btnAutoInfo[1]);            /*获取图片信息*/
 CloseHandle(handle);
}
/*
*Name:     RandMap
*Input:
*Output:  
*Return:   void
*Description: 随机化地图
*/
void RandMap()
{
 int i = 0;
 int n = 0;
    int j = 0;
 int k = 1;
 int m = g_Segment*g_Segment;
 memset(g_Map, 0, sizeof(g_Map));
 srand((unsigned)time(NULL));
 while (m > 0)
 {
  n =rand()%m;
  i = 0;
  while (n >= 0)
  {
   if (g_Map[i/g_Segment][i%g_Segment] == 0)
   {
    n--;
   }
   i++;
  }
  g_Map[(i-1)/g_Segment][(i-1)%g_Segment] = k++;
  m--;
 }
}
/*
*Name:     OnLButtonDown
*Input:    HWND hWnd,
           int x,
     int y
*Output:  
*Return:   void
*Description: 左键按下,响应程序
*/
void OnLButtonDown(HWND hWnd, int x, int y)
{
 int picWidth = (picRect.right - picRect.left)/g_Segment;
 int picHeight = (picRect.bottom - picRect.top)/g_Segment;
 HBRUSH hBrush;
 HBRUSH oldhBrush;
 HDC hdc;
 int i = 0;
 int j = 0;
 hdc = GetDC(hWnd);
 i = (x - picRect.left)/(picWidth + PIC_SPACE);
 j = (y - picRect.top)/(picHeight + PIC_SPACE);
 if (i < g_Segment && j < g_Segment)  /*鼠标在图片区*/
 {
  g_MapInfo[j][i] = DOWN;          /*设置图片按下标志*/
  PlaySound("Click.wav", NULL, SND_FILENAME | SND_ASYNC);  /*调用单击声音*/
  hBrush = (HBRUSH)GetStockObject(NULL_BRUSH);  
  oldhBrush = (HBRUSH)SelectObject(hdc, hBrush);
  Rectangle(hdc,
   picRect.left + i * (picWidth + PIC_SPACE)-1 ,
   picRect.top + j * (picHeight + PIC_SPACE) - 1,
   picRect.left + i * (picWidth + PIC_SPACE) + 1 + picWidth,
   picRect.top + j * (picHeight + PIC_SPACE) + 1 + picHeight); /*画空心外框*/

  SelectObject(hdc, oldhBrush);
 }
 DeleteObject(hBrush);
 DeleteObject(oldhBrush);
 DeleteObject(hdc);
}
/*
*Name:      OnLButtonUp
*Input:     HWND hWnd
*Output:   
*Return:    void
*Description: 响应鼠标松开事件
*/
void OnLButtonUp(HWND hWnd)
{
 if (g_btnInfo[0] == 1 || g_btnInfo[1] == 1)
 {
  g_btnInfo[0] = 0;
  g_btnInfo[1] = 0;
  InitWindow(hWnd);
 } 
}
/*
*Name:       Exchange
*Input:      HWND hWnd,
             int x,
    int y
*Output:     
*Return:     void
*Description:交换相邻的图片块
*/
void Exchange(HWND hWnd, int x, int y)
{
 int picWidth = (picRect.right - picRect.left)/g_Segment;
 int picHeight = (picRect.bottom - picRect.top)/g_Segment;
 int i = 0;
 int j = 0;
 int m = 0;
 int n = 0;
 bool isNext = false;
 static int times = 0;     /*计算按下图片的个数*/
 int k = 0;
 int temp = 0;

 int derector[4][2] = {
  -1, 0,
  0, -1,
  1, 0,
  0, 1};
 j = (x - picRect.left)/(picWidth + PIC_SPACE);
 i = (y - picRect.top)/(picHeight + PIC_SPACE);

 if (i < g_Segment && j < g_Segment)
 {
  times++;

  if (times >= 2)
  {
   for (k = 0; k < 4; k++)         /*判断图片块是否相邻*/
   {
    m = i + derector[k][0];
    n = j + derector[k][1];
    if (m >= 0 && m < g_Segment && n >= 0 && n < g_Segment && g_MapInfo[m][n] == DOWN)
    {
     isNext = true;
     break;
    }
   }
   if (isNext)                  /*如果相邻*/
   {
    temp = g_Map[i][j];
    g_Map[i][j] = g_Map[m][n];
    g_Map[m][n] = temp;
    g_step++;
    PlaySound("Exchange.wav", NULL, SND_FILENAME | SND_ASYNC);
   }
   else
   {
    PlaySound("UnExchange.wav", NULL, SND_FILENAME | SND_ASYNC);
   }
   InitWindow(hWnd);
   if (Success())         /*是否成功*/
   {
    PlaySound("Success.wav", NULL, SND_FILENAME | SND_ASYNC);
    MessageBox(hWnd, "恭喜你,你过关了!", "提示", MB_OK);
    switch(g_Segment)               /*记录步数*/
    {
    case EASY:
     if (g_step < g_EasyMinStep)
     {
      g_EasyMinStep = g_step;
     }
     break;
    case MIDDLE:
     if (g_step < g_MiddleMinStep)
     {
      g_MiddleMinStep = g_step;
     }
     break;
    case HARD:
     if (g_step < g_HardMinStep)
     {
      g_HardMinStep = g_step;
     }
     break;
    default:
     break;
    }
    g_Success = true;
    g_step = 0;
   }
   InitWindow(hWnd);
   memset(g_MapInfo, 0, sizeof(g_MapInfo));
   times = 0;
  }
 }

}
/*
*Name:      Success
*Input: 
*Output:
*Return:    bool
*Description:  判断地图是否按顺序存放 
*/
bool Success()
{
 int i = 0;
 int j = 0;
 int k = 1;
 bool result = true;
 for (i = 0; i < g_Segment; i++)
 {
  for (j = 0; j < g_Segment; j++)
  {
   if (g_Map[i][j] != k++)
   {
    result = false;
    break;
   }
  }
 }
 return result;
}
/*
*Name:     InitMap
*Input:
*Output:
*Return:
*Description:  按顺序存放
*/
void InitMap()
{
 int i = 0;
 int j = 0;
 int k = 1;
 for (i = 0; i < g_Segment; i++)
 {
  for (j = 0; j < g_Segment; j++)
  {
   g_Map[i][j] = k++;
  }
 }
}
/*
*Name:       AutoMode
*Input:      HWND hWnd
*Output:    
*Return:     int
*Description: 自动排列
*/
int AutoMode(HWND hWnd)
{
 int k = 0;
 int i = 0;
 int j = 0;
 int t = 0;
 int d = 0;
 int q = g_Segment*g_Segment;
 int m = 0;
 int n = 0;
 int temp = 0;
 int der = 0;
 while(d < q)
 {
  t = 0;
  k++;
  while (t < q)             /*找到对应数字的位置*/
  {
   if (g_Map[t/g_Segment][t%g_Segment] == k)
   {
    i = t/g_Segment;
    j = t%g_Segment;
    break;
   }
   t++;
  }
  
  m = (k-1)/g_Segment;      /*数字目标位置*/
  n = (k-1)%g_Segment;     
  while (j != n)            /*先左右移动*/
  {
   if (j > n)
   {
    j--;
    temp = g_Map[i][j];
    g_Map[i][j] = g_Map[i][j+1];
    g_Map[i][j+1] = temp;
   }
   else
   {
    j++;
    temp = g_Map[i][j];
    g_Map[i][j] = g_Map[i][j-1];
    g_Map[i][j-1] = temp;
   }
   g_step++;
   return 0;
  }

  while (i != m)                     /*后上下移动*/
  {
   if (i > m)
   {
    i--;
    temp = g_Map[i][j];
    g_Map[i][j] = g_Map[i+1][j];
    g_Map[i+1][j] = temp;
   }
   else
   {
    i++;
    temp = g_Map[i][j];
    g_Map[i][j] = g_Map[i-1][j];
    g_Map[i-1][j] = temp;
   }
   g_step++;
   return 0;
  }
  d++;
 }
 g_Success = true;
 g_Automode = false;
 g_step = 0;
 PlaySound("Success.wav", NULL, SND_FILENAME | SND_ASYNC);
 MessageBox(hWnd, "恭喜你,你过关了!", "提示", MB_OK);
 KillTimer(hWnd, 0);
 return 0;
}

 

你可能感兴趣的:(游戏,null,input,callback,Exchange,output)