LLKDlg.cpp
资源名称:LLK.rar [点击查看]
上传用户:dengling
上传日期:2022-05-24
资源大小:3782k
文件大小:11k
源码类别:
其他游戏
开发平台:
Visual C++
- // LLKDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "LLK.h"
- #include "LLKDlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- #define ROWCOUNT 7
- #define COLCOUNT 12
- #define BLANK_STATE -1
- #define BKCOLOR RGB(128,128,128)
- #define FRONTWIDTH (39+2)
- #define FRONTHEIGHT (39+12)
- #define BKWIDTH 46
- #define BKHEIGHT 56
- /////////////////////////////////////////////////////////////////////////////
- // CAboutDlg dialog used for App About
- class CAboutDlg : public CDialog
- {
- public:
- CAboutDlg();
- // Dialog Data
- //{{AFX_DATA(CAboutDlg)
- enum { IDD = IDD_ABOUTBOX };
- //}}AFX_DATA
- // ClassWizard generated virtual function overrides
- //{{AFX_VIRTUAL(CAboutDlg)
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
- //}}AFX_VIRTUAL
- // Implementation
- protected:
- //{{AFX_MSG(CAboutDlg)
- //}}AFX_MSG
- DECLARE_MESSAGE_MAP()
- };
- CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
- {
- //{{AFX_DATA_INIT(CAboutDlg)
- //}}AFX_DATA_INIT
- }
- void CAboutDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CAboutDlg)
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
- //{{AFX_MSG_MAP(CAboutDlg)
- // No message handlers
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CLLKDlg dialog
- CLLKDlg::CLLKDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CLLKDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CLLKDlg)
- // NOTE: the ClassWizard will add member initialization here
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- m_nRow = ROWCOUNT;
- m_nCol = COLCOUNT;
- m_map = new int [m_nRow * m_nCol];
- m_nX1 = BLANK_STATE;
- m_nY1 = BLANK_STATE;
- }
- CLLKDlg::~CLLKDlg()
- {
- delete [] m_map;
- }
- void CLLKDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CLLKDlg)
- // NOTE: the ClassWizard will add DDX and DDV calls here
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CLLKDlg, CDialog)
- //{{AFX_MSG_MAP(CLLKDlg)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_WM_LBUTTONDOWN()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CLLKDlg message handlers
- BOOL CLLKDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // Add "About..." menu item to system menu.
- // IDM_ABOUTBOX must be in the system command range.
- ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
- ASSERT(IDM_ABOUTBOX < 0xF000);
- CMenu* pSysMenu = GetSystemMenu(FALSE);
- if (pSysMenu != NULL)
- {
- CString strAboutMenu;
- strAboutMenu.LoadString(IDS_ABOUTBOX);
- if (!strAboutMenu.IsEmpty())
- {
- pSysMenu->AppendMenu(MF_SEPARATOR);
- pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
- }
- }
- // Set the icon for this dialog. The framework does this automatically
- // when the application's main window is not a dialog
- SetIcon(m_hIcon, TRUE); // Set big icon
- SetIcon(m_hIcon, FALSE); // Set small icon
- // TODO: Add extra initialization here
- CDC *pDC = GetDC();
- m_men3DBkDC.CreateCompatibleDC(pDC);
- m_men3DBkBmp.LoadBitmap(IDB_BITMAP2);
- m_men3DBkDC.SelectObject(&m_men3DBkBmp);
- m_menAnimalDC.CreateCompatibleDC(pDC);
- m_menAnimalBmp.LoadBitmap(IDB_BITMAP1);
- m_menAnimalDC.SelectObject(&m_menAnimalBmp);
- m_menDC.CreateCompatibleDC(pDC);
- m_menBitmap.CreateCompatibleBitmap(pDC, m_nCol * FRONTWIDTH + 5, m_nRow * FRONTHEIGHT + 5);
- m_menDC.SelectObject(&m_menBitmap);
- StartNewGame();
- HWND hWnd = ::AfxGetMainWnd()->m_hWnd;
- ::SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0 , 0, SWP_NOMOVE|SWP_NOSIZE);
- return TRUE; // return TRUE unless you set the focus to a control
- }
- void CLLKDlg::OnSysCommand(UINT nID, LPARAM lParam)
- {
- if ((nID & 0xFFF0) == IDM_ABOUTBOX)
- {
- CAboutDlg dlgAbout;
- dlgAbout.DoModal();
- }
- else
- {
- CDialog::OnSysCommand(nID, lParam);
- }
- }
- // If you add a minimize button to your dialog, you will need the code below
- // to draw the icon. For MFC applications using the document/view model,
- // this is automatically done for you by the framework.
- void CLLKDlg::OnPaint()
- {
- CPaintDC dc(this); // device context for painting
- DrawGame(&m_menDC);
- dc.BitBlt(0, 0, m_nCol*FRONTWIDTH, m_nCol*FRONTHEIGHT, &m_menDC, 0, 0, SRCCOPY);
- }
- // The system calls this to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR CLLKDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- void CLLKDlg::StartNewGame()
- {
- int i = 0;
- for(i = 0; i < (m_nRow * m_nCol); i++)
- {
- m_map[i] = BLANK_STATE;
- }
- srand(time(NULL));
- CDWordArray tempMap;
- for (i = 0; i < (m_nRow * m_nCol) / 4; i++)
- {
- for (int j = 0; j < 4; j++)
- {
- tempMap.Add(i);
- }
- }
- for( i = 0; i < (m_nRow * m_nCol); i++)
- {
- int nIndex = (int (rand() * 0.1 + rand() * 0.01 + rand()))%tempMap.GetSize();
- m_map[i] = tempMap.GetAt(nIndex);
- tempMap.RemoveAt(nIndex);
- }
- Invalidate(TRUE);
- }
- BOOL CLLKDlg::IsLink(int x1, int y1, int x2, int y2)
- {
- if (x1 == x2)
- {
- if (X1_Link_X2(x1, y1, y2))
- {
- return TRUE;
- }
- }
- if (y1 == y2)
- {
- if (Y1_Link_Y2(x1, x2, y1))
- {
- return TRUE;
- }
- }
- if(OneCornerLink(x1, y1, x2, y2))
- {
- return TRUE;
- }
- if (TwoCornerLink(x1, y1, x2, y2))
- {
- return TRUE;
- }
- return FALSE;
- }
- BOOL CLLKDlg::X1_Link_X2(int x1, int y1, int y2)
- {
- int int_temp = 0;
- if ( y1 > y2)
- {
- int_temp = y1;
- y1 = y2;
- y2 = int_temp;
- }
- for (int i = y1 + 1; i <= y2; i++)
- {
- if (i == y2)
- {
- return TRUE;
- }
- if (m_map[i * m_nCol + x1] != BLANK_STATE)
- {
- break;
- }
- }
- if (XThrough(x1 - 1, y1, 0) &&XThrough(x1 - 1, y2, 0))
- {
- return TRUE;
- }
- if (XThrough(m_nCol- 1, y1, x1 + 1) &&XThrough(m_nCol - 1, y2, x1 + 1))
- {
- return TRUE;
- }
- return FALSE;
- }
- BOOL CLLKDlg::Y1_Link_Y2(int x1, int x2, int y1)
- {
- int int_temp = 0;
- if ( x1 > x2)
- {
- int_temp = x1;
- x1 = x2;
- x2 = int_temp;
- }
- for (int i = x1 + 1; i <= x2; i++)
- {
- if (i == x2)
- {
- return TRUE;
- }
- if (m_map[y1 * m_nCol + i] != BLANK_STATE)
- {
- break;
- }
- }
- if (YThrough(y1 - 1, x1, 0) &&YThrough(y1 - 1, x2, 0))
- {
- return TRUE;
- }
- if (YThrough(m_nRow - 1, x1, y1 + 1) &&YThrough(m_nRow - 1, x2, y1 + 1))
- {
- return TRUE;
- }
- return FALSE;
- }
- BOOL CLLKDlg::XThrough(int x, int y1, int temp )
- {
- for (int i = x; i >= temp ; i--)
- {
- if (m_map[y1 * m_nCol + i] != BLANK_STATE)
- {
- break;
- }
- }
- if (i < temp)
- {
- return TRUE;
- }
- return FALSE;
- }
- BOOL CLLKDlg::YThrough(int y, int x1, int temp )
- {
- for (int i = y; i >= temp ; i--)
- {
- if (m_map[i * m_nCol + x1] != BLANK_STATE)
- {
- break;
- }
- }
- if (i < temp)
- {
- return TRUE;
- }
- return FALSE;
- }
- BOOL CLLKDlg::OneCornerLink(int x1, int y1, int x2, int y2)
- {
- int temp_x = 0;
- int temp_y = 0;
- if (x2 < x1)
- {
- temp_x = x2;
- x2 = x1;
- x1 = temp_x;
- temp_y = y1;
- y1 = y2;
- y2 = temp_y;
- }
- if (x2 > x1 && y2 < y1)
- {
- if(YThrough(y1 - 1, x1 ,y2)&&XThrough(x2 - 1, y2, x1))
- {
- return TRUE;
- }
- if(XThrough(x2, y1, x1 + 1)&&YThrough(y1, x2, y2 + 1))
- {
- return TRUE;
- }
- }
- if(x2 > x1 && y2 > y1)
- {
- if(YThrough(y2, x1, y1 + 1)&&XThrough(x2 - 1, y2, x1))
- {
- return TRUE;
- }
- if (XThrough(x2, y1, x1 + 1)&&YThrough(y2 - 1, x2, y1))
- {
- return TRUE;
- }
- }
- return FALSE;
- }
- BOOL CLLKDlg::TwoCornerLink(int x1, int y1, int x2, int y2)
- {
- int temp_x = 0;
- int temp_y = 0;
- int count = 0;
- int i = 0;
- if (x2 < x1)
- {
- temp_x = x2;
- x2 = x1;
- x1 = temp_x;
- temp_y = y1;
- y1 = y2;
- y2 = temp_y;
- }
- if (x2 > x1 && y2 < y1)
- {
- count = y1 - y2 - 1;
- if (YThrough(y1 - 1, x1 , 0) &&YThrough(y2 - 1, x2 , 0) )
- {
- return TRUE;
- }//向上
- if (YThrough(m_nRow - 1, x1 , y1 + 1) &&YThrough(m_nRow - 1, x2 , y2 + 1) )
- {
- return TRUE;
- }//向下
- if (XThrough(x1 - 1, y1, 0) && XThrough(x2 -1, y2, 0))
- {
- return TRUE;
- }//向左
- if (XThrough(m_nCol - 1, y1, x1 + 1) && XThrough(m_nCol -1, y2, x2 + 1))
- {
- return TRUE;
- }//向右
- for (int i = 1; i <= count; i++)
- {
- if(OneCornerLink(x1, y1 - i, x2, y2))
- {
- return TRUE;
- }
- }
- count = x2 -x1 -1;
- for (i = 1; i <= count; i++)
- {
- if(OneCornerLink(x1 + i, y1, x2, y2))
- {
- return TRUE;
- }
- }
- }
- if (x2 > x1 && y2 > y1)
- {
- count = y2 - y1 - 1;
- for (i = 1; i <= count; i++)
- {
- if(OneCornerLink(x1, y1 + i, x2, y2))
- {
- return TRUE;
- }
- }
- count = x2 - x1 - 1;
- for (i = 1; i <= count; i++)
- {
- if(OneCornerLink(x1 + i, y1, x2, y2))
- {
- return TRUE;
- }
- }
- }
- return FALSE;
- }
- BOOL CLLKDlg::IsWin()
- {
- for(int i = 0; i < m_nRow * m_nCol; i++)
- {
- if (m_map[i] != BLANK_STATE)
- {
- return FALSE;
- }
- }
- return TRUE;
- }
- void CLLKDlg::OnLButtonDown(UINT nFlags, CPoint point)
- {
- // TODO: Add your message handler code here and/or call default
- int x = point.x/FRONTWIDTH + (point.x % FRONTWIDTH?1:0) - 1;
- int y = point.y/FRONTHEIGHT + (point.y % FRONTHEIGHT?1:0) - 1;
- if (x < m_nCol && y < m_nRow && m_map[y * m_nCol + x] != BLANK_STATE)
- {
- if (m_nX1 == BLANK_STATE)
- {
- m_nX1 = x;
- m_nY1 = y;
- CDC* pDC = GetDC();
- CPen pen;
- CPen *poldPen;
- pen.CreatePen(PS_SOLID, 4, RGB(255, 0, 0));
- poldPen = pDC->SelectObject(&pen);
- pDC->MoveTo(x * FRONTWIDTH, y * FRONTHEIGHT);
- pDC->LineTo(x * FRONTWIDTH, (y + 1) * FRONTHEIGHT );
- pDC->LineTo((x + 1) * FRONTWIDTH, (y + 1) * FRONTHEIGHT);
- pDC->LineTo((x + 1) * FRONTWIDTH, y * FRONTHEIGHT);
- pDC->LineTo(x * FRONTWIDTH, y * FRONTHEIGHT);
- pDC->SelectObject(poldPen);
- }
- else
- {
- if ((m_nX1 != x || m_nY1 != y)&&(m_map[m_nY1 * m_nCol + m_nX1] == m_map[y * m_nCol + x]))
- {
- if (IsLink(x, y, m_nX1, m_nY1))
- {
- m_map[m_nY1 * m_nCol + m_nX1] = BLANK_STATE;
- m_map[y *m_nCol + x] = BLANK_STATE;
- }
- }
- m_nX1 = BLANK_STATE;
- m_nY1 = BLANK_STATE;
- Invalidate(FALSE);
- }
- }
- if (IsWin())
- {
- MessageBox("success.........");
- StartNewGame();
- }
- CDialog::OnLButtonDown(nFlags, point);
- }
- void CLLKDlg::DrawGame(CDC * pDC)
- {
- pDC->FillSolidRect(0 , 0, m_nCol * FRONTWIDTH + 5, m_nRow * FRONTHEIGHT + 5,BKCOLOR);
- for (int i = 0; i < m_nRow ;i ++)
- {
- for (int j = 0; j < m_nCol; j ++)
- {
- if (m_map[i * m_nCol + j] == BLANK_STATE)
- {
- continue;
- }
- pDC->BitBlt(j * FRONTWIDTH, i * FRONTHEIGHT, BKWIDTH,
- BKHEIGHT, &m_men3DBkDC, 0, BKHEIGHT, SRCCOPY);
- pDC->BitBlt(j * FRONTWIDTH,i * FRONTHEIGHT,
- FRONTWIDTH - 2, FRONTHEIGHT - 12,
- &m_menAnimalDC,
- FRONTWIDTH - 2, m_map[i * m_nCol + j] * (FRONTHEIGHT - 12),
- SRCAND);
- pDC->BitBlt(j * FRONTWIDTH, i * FRONTHEIGHT,
- FRONTWIDTH - 2, FRONTHEIGHT - 12,
- &m_menAnimalDC,
- 0, m_map[i * m_nCol + j] * (FRONTHEIGHT - 12),
- SRCPAINT);
- }
- }
- }