// MineWnd.cpp : 实现文件
//
#include "stdafx.h"
#include "Mine.h"
#include "MineWnd.h"
#include "MineDef.h"
//难度设置值
// CMineWnd
IMPLEMENT_DYNAMIC(CMineWnd, CWnd)
struct MINEWND;
CMineWnd::CMineWnd()
{
m_colBG=RGB(192,192,192);
m_clrDark=RGB(168,168,168);
m_clrLight=RGB(250,250,250);
m_uGameState=GS_WAIT;
m_bmpMine.LoadBitmap(IDB_MINE_COLOR);
for (int i=0;i<100;i++)
{
for(int j=0;j<100;j++)
{
m_pMines[i][j].uAttrib =ATTRIB_EMPTY;
m_pMines[i][j].nCol =i;
m_pMines[i][j].nRow =j;
m_pMines[i][j].uState =STATE_NORMAL;
m_pMines[i][j].uOldState =STATE_NORMAL;
}
}
m_nXNum=PRIMARY_XNUM;
m_nYNum=PRIMARY_YNUM;
m_uMineNum=PRIMARY_MINENUM;
m_bFirstDraw=true;
m_bSound=true;
//this->SizeWindow ();
}
CMineWnd::~CMineWnd()
{
}
BEGIN_MESSAGE_MAP(CMineWnd, CWnd)
ON_WM_PAINT()
ON_COMMAND(ID_PRIMARY, &CMineWnd::OnPrimary)
ON_COMMAND(ID_SECONDRY, &CMineWnd::OnSecondry)
ON_COMMAND(ID_ADVANCE, &CMineWnd::OnAdvance)
ON_UPDATE_COMMAND_UI(ID_PRIMARY, &CMineWnd::OnUpdatePrimary)
ON_UPDATE_COMMAND_UI(ID_SECONDRY, &CMineWnd::OnUpdateSecondry)
ON_UPDATE_COMMAND_UI(ID_ADVANCE, &CMineWnd::OnUpdateAdvance)
ON_COMMAND(ID_START, &CMineWnd::OnStart)
ON_WM_LBUTTONUP()
ON_WM_RBUTTONUP()
ON_COMMAND(ID_DIY, &CMineWnd::OnDiy)
ON_COMMAND(ID_MUSIC, &CMineWnd::OnMusic)
ON_UPDATE_COMMAND_UI(ID_MUSIC, &CMineWnd::OnUpdateMusic)
END_MESSAGE_MAP()
// CMineWnd 消息处理程序
void CMineWnd::OnPaint()
{
if(m_bFirstDraw==TRUE)
{
OnPrimary();
m_bFirstDraw=FALSE;
}
CPaintDC dc(this); // device context for painting
CDC dcMemory;
CBitmap bitmap;
if(!dc.IsPrinting ())
{
if(dcMemory.CreateCompatibleDC (&dc))
{
if(bitmap.CreateCompatibleBitmap (&dc,m_rcClient.right,m_rcClient.bottom ))
{
dcMemory.SelectObject (&bitmap);
dcMemory.FillSolidRect (&m_rcClient,m_colBG);
DrawShell((CPaintDC&)dcMemory);
DrawButton((CPaintDC&)dcMemory);
DrawNumber((CPaintDC&)dcMemory);
DrawMineArea((CPaintDC&)dcMemory);
dc.BitBlt (0,0,m_rcClient.right ,m_rcClient.bottom ,&dcMemory,0,0,SRCCOPY);
bitmap.DeleteObject ();
}
}
}
// 不为绘图消息调用 CWnd::OnPaint()
}
void CMineWnd::OnPrimary()
{
m_nXNum=PRIMARY_XNUM;
m_nYNum=PRIMARY_YNUM;
m_uMineNum=PRIMARY_MINENUM;
for (int i=0;i<100;i++)
{
for(int j=0;j<100;j++)
{
m_pMines[i][j].uAttrib =ATTRIB_EMPTY;
m_pMines[i][j].nCol =i;
m_pMines[i][j].nRow =j;
m_pMines[i][j].uState =STATE_NORMAL;
m_pMines[i][j].uOldState =STATE_NORMAL;
}
}
SizeWindow();
// TODO: 在此添加命令处理程序代码
}
void CMineWnd::OnSecondry()
{
m_nXNum=SECONDRY_XNUM;
m_nYNum=SECONDRY_YNUM;
m_uMineNum=SECONDRY_MINENUM;
for (int i=0;i<100;i++)
{
for(int j=0;j<100;j++)
{
m_pMines[i][j].uAttrib =ATTRIB_EMPTY;
m_pMines[i][j].nCol =i;
m_pMines[i][j].nRow =j;
m_pMines[i][j].uState =STATE_NORMAL;
m_pMines[i][j].uOldState =STATE_NORMAL;
}
}
SizeWindow();
// TODO: 在此添加命令处理程序代码
}
void CMineWnd::OnAdvance()
{
m_nXNum=ADVANCE_XNUM;
m_nYNum=ADVANCE_YNUM;
m_uMineNum=ADVANCE_MINENUM;
for (int i=0;i<100;i++)
{
for(int j=0;j<100;j++)
{
m_pMines[i][j].uAttrib =ATTRIB_EMPTY;
m_pMines[i][j].nCol =i;
m_pMines[i][j].nRow =j;
m_pMines[i][j].uState =STATE_NORMAL;
m_pMines[i][j].uOldState =STATE_NORMAL;
}
}
SizeWindow();
// TODO: 在此添加命令处理程序代码
}
void CMineWnd::OnUpdatePrimary(CCmdUI *pCmdUI)
{
// TODO: 在此添加命令更新用户界面处理程序代码
}
void CMineWnd::SizeWindow (void)
{
UINT uWidth=DEFAULT_FRAME_X+MINE_WIDTH*m_nXNum+LINE_WIDTH*3+SIDE_WIDTH*2;
UINT uHeight=DEFAULT_FRAME_Y+MINE_HEIGHT*m_nYNum+SHELL_S_H+LINE_WIDTH*3+SIDE_WIDTH*3;
SetWindowPos(&wndTopMost,0,0,uWidth,uHeight,SWP_NOZORDER|SWP_NOMOVE|SWP_NOCOPYBITS);
GetClientRect(&m_rcClient);
m_uSSHW=MINE_WIDTH*m_nXNum+6;
m_uLSHW=m_uSSHW;
m_uLSHH=MINE_HEIGHT*m_nYNum+6;
}
void CMineWnd::OnUpdateSecondry(CCmdUI *pCmdUI)
{
// TODO: 在此添加命令更新用户界面处理程序代码
}
void CMineWnd::OnUpdateAdvance(CCmdUI *pCmdUI)
{
// TODO: 在此添加命令更新用户界面处理程序代码
}
void CMineWnd::DrawButton(CPaintDC &dc)
{}
void CMineWnd::DrawShell(CPaintDC &dc)
{
CRect rc;
GetClientRect(&rc);
dc.FillSolidRect (0,0,LINE_WIDTH ,rc.bottom ,RGB(255,255,255));
dc.FillSolidRect (0,0,rc.right ,LINE_WIDTH,RGB(255,255,255));
dc.Draw3dRect (SHELL_S_START_X,SHELL_S_START_Y,m_uSSHW,SHELL_S_H,m_clrDark,m_clrLight);
dc.Draw3dRect (SHELL_S_START_X+1,SHELL_S_START_Y+1,m_uSSHW-1,SHELL_S_H-1,m_clrDark,m_clrLight);
dc.Draw3dRect (SHELL_S_START_X+2,SHELL_S_START_Y+2,m_uSSHW-2,SHELL_S_H-2,m_clrDark,m_clrLight);
dc.Draw3dRect (SHELL_L_START_X,SHELL_L_START_Y,m_uLSHW,m_uLSHH,m_clrDark,m_clrLight);
dc.Draw3dRect (SHELL_L_START_X+1,SHELL_L_START_Y+1,m_uLSHW-1,m_uLSHH-1,m_clrDark,m_clrLight);
dc.Draw3dRect (SHELL_L_START_X+2,SHELL_L_START_Y+2,m_uLSHW-2,m_uLSHH-2,m_clrDark,m_clrLight);
}
void CMineWnd::DrawNumber(CPaintDC &dc)
{}
void CMineWnd::DrawMineArea(CPaintDC &dc)
{
CDC dcMemory;
dcMemory.CreateCompatibleDC (&dc);
dcMemory.SelectObject (&m_bmpMine);
for (int i=0;i< m_nXNum;i++)
{
for(int j=0;j< m_nYNum;j++)
{
dc.StretchBlt (MINEAREA_FRAME_X+16*i,MINEAREA_FRAME_Y+16*j,16,16,&dcMemory,0,16*m_pMines[i][j].uState ,
16,16,SRCCOPY);
}
}
}
void CMineWnd::LayMines(UINT row, UINT col)
{
srand((unsigned)time(NULL));
UINT i,j;
for(UINT index=0;index<m_uMineNum;)
{
i=rand()%m_nXNum;
j=rand()%m_nYNum;
if(m_pMines[i][j].uAttrib != ATTRIB_MINE)
{
m_pMines[i][j].uAttrib =ATTRIB_MINE;
index++;
}
}
}
void CMineWnd::OnStart()
{
for (int i=0;i<100;i++)
{
for(int j=0;j<100;j++)
{
m_pMines[i][j].uAttrib =ATTRIB_EMPTY;
m_pMines[i][j].nCol =i;
m_pMines[i][j].nRow =j;
m_pMines[i][j].uState =STATE_NORMAL;
m_pMines[i][j].uOldState =STATE_NORMAL;
}
}
LayMines(m_nXNum,m_nYNum);
m_uGameState=GS_RUN;
Invalidate();
// TODO: 在此添加命令处理程序代码
}
void CMineWnd::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CRect rcBtn(0,0,0,0);
CRect rcMineArea(MINE_AREA_LEFT,MINE_AREA_TOP,MINE_AREA_LEFT+m_nXNum*MINE_WIDTH,
MINE_AREA_TOP+MINE_HEIGHT*m_nYNum);
if(rcBtn.PtInRect (point))
{
Invalidate();
OnStart();
}
else if( rcMineArea.PtInRect (point))
{
CPoint pt;
pt.x =point.x -MINE_AREA_LEFT;
pt.y=point.y -MINE_AREA_TOP;
UINT I=pt.x /16;
UINT J=pt.y /16;
switch(m_uGameState)
{
//case GS_WAIT:
case GS_RUN:
if(m_bSound)
MessageBeep(MB_ICONASTERISK);
m_pOldMine=&m_pMines[I][J];
if(!m_pOldMine)
{
//Dead(m_pOldMine->nCol ,m_pOldMine->nRow );
return;
}
if(m_pOldMine->uState ==STATE_NORMAL)
{
if(IsMine( m_pOldMine->nCol,m_pOldMine->nRow ))
{
Dead(m_pOldMine->nCol ,m_pOldMine->nRow );
return;
}
around=GetAroundNum(m_pOldMine->nCol ,m_pOldMine->nRow );
if(around==0) ExpandMines(m_pOldMine->nCol ,m_pOldMine->nRow );
else m_pOldMine->uState=15-around;
if(Victory())
{
AfxMessageBox(_T("You are the winner!"));
return;
}
}
break;
}
Invalidate();
}
CWnd::OnLButtonUp(nFlags, point);
}
//MINEWND CMineWnd::GetMine(UINT x, UINT y)
//{
// UINT X=x/16;
// UINT Y=y/16;
// CString str;
// str.Format ("%d,%d",X,Y);
// return m_pMines[X][Y];
//
//}
BOOL CMineWnd::IsMine(UINT col, UINT row)
{
return (m_pMines[col][row].uAttrib ==ATTRIB_MINE);
}
UINT CMineWnd::GetAroundNum(UINT col, UINT row)
{
UINT i,j;
UINT around=0;
UINT minRow=(row==0)? 0:row-1;
UINT maxRow=(row==m_nYNum-1)? m_nYNum-1:row+2;
UINT minCol=(col==0)? 0:col-1;
UINT maxCol=(col==m_nXNum-1)? m_nXNum-1:col+2;
for(i=minRow;i<maxRow;i+