最近学着写的扫雷(基本完成了,但有一个地方有逻辑问题啊 )
高手求解 郁闷啊 不明白啊程序代码:
// MyMineView.cpp : implementation of the CMyMineView class // #include <time.h> #include "stdafx.h" #include "MyMine.h" #include "MyMineDoc.h" #include "MyMineView.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CMyMineView IMPLEMENT_DYNCREATE(CMyMineView, CView) BEGIN_MESSAGE_MAP(CMyMineView, CView) //{{AFX_MSG_MAP(CMyMineView) ON_COMMAND(ID_START, OnStart) ON_WM_RBUTTONDOWN() ON_WM_LBUTTONDOWN() ON_WM_LBUTTONUP() ON_WM_CREATE() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CMyMineView construction/destruction CMyMineView::CMyMineView() {int n,i; // TODO: add construction code here for(int a=0;a<9;a++) cBitmap[a].LoadBitmap(IDB_Bitmap0+a); for(int b=0;b<4;b++) cFace[b].LoadBitmap(IDB_FACE1+b); for(int c=0;c<3;c++) cFalg[c].LoadBitmap(IDB_wenhao+c); srand(time(NULL)); GameOver=0; Row=0; List=0; MineNum=0; Second=0; LeftNum=50; Hour=0; Miute=0; for(n=0;n<16;n++) for(i=0;i<16;i++) {Mine[n][i].weitu=0; Mine[n][i].Num=0; } do {Row=rand()%15; List=rand()%15; if(Mine[Row][List].Num!=-1) {Mine[Row][List].Num=-1; MineNum++; } } while(MineNum!=50); for(int aa=0;aa<16;aa++) for(int b=0;b<16;b++) if(Mine[aa][b].Num==0) { for(int c=aa-1;c<aa+2;c++) for(int d=b-1;d<b+2;d++) if(c>=0&&c<16&&d>=0&&d<16) if(Mine[c][d].Num==-1) Mine[aa][b].Num++; } } CMyMineView::~CMyMineView() { } BOOL CMyMineView::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return CView::PreCreateWindow(cs); } ///////////////////////////////////////////////////////////////////////////// // CMyMineView drawing void CMyMineView::OnDraw(CDC* pDC) { int ii,jj; int Temp=0; CString str; CMyMineDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: add draw code for native data here if(Victor()) { AfxMessageBox("游戏胜利"); } CBrush cBrush1; cBrush1.CreateSolidBrush(RGB(192,192,192)); CRect Rect1(10,10,60,40); pDC->FillRect(Rect1,&cBrush1); CRect Rect2(200,10,250,40); pDC->FillRect(Rect2,&cBrush1); CRect Rect3(1,1,400,500); pDC->FillRect(Rect3,&cBrush1); CPen Pen1; Pen1.CreatePen(PS_SOLID,1,RGB(0,0,0)); pDC->SelectObject(Pen1); pDC->MoveTo(10,10); pDC->LineTo(60,10); pDC->LineTo(60,40); pDC->LineTo(10,40); pDC->MoveTo(10,10); pDC->LineTo(10,40); pDC->MoveTo(200,10); pDC->LineTo(250,10); pDC->LineTo(250,40); pDC->LineTo(200,40); pDC->MoveTo(200,10); pDC->LineTo(200,40); CPen Pen2; Pen2.CreatePen(PS_SOLID,2,RGB(255,255,255)); pDC->SelectObject(Pen2); for(ii=0;ii<16;ii++) for(jj=0;jj<16;jj++) { pDC->MoveTo(10+ii*15,50+jj*15+14); pDC->LineTo(10+ii*15,50+jj*15); pDC->LineTo(10+ii*15+14,50+jj*15); } pDC->SelectObject(Pen1); for(ii=0;ii<16;ii++) for(jj=0;jj<16;jj++) { pDC->MoveTo(10+ii*15,50+jj*15+14); pDC->LineTo(10+ii*15+15,50+jj*15+14); pDC->LineTo(10+ii*15+15,50+jj*15); } CDC dc; if(!dc.CreateCompatibleDC(pDC)) { AfxMessageBox("创建位图失败",0,0); } dc.SelectObject(cFace[0]); pDC->BitBlt(120,10,160,160,&dc,0,0,SRCCOPY); //weitu 1代表为按下了显示数字 //weitu 2代表显示为旗子 //weitu 3代表问号 for(ii=0;ii<16;ii++) for(jj=0;jj<16;jj++) { if(Mine[ii][jj].weitu==3) {dc.SelectObject(cFalg[0]); pDC->BitBlt(10+ii*15,50+15*jj,160,160,&dc,0,0,SRCCOPY); } if(Mine[ii][jj].weitu==2) {dc.SelectObject(cFalg[1]); pDC->BitBlt(10+ii*15,50+15*jj,160,160,&dc,0,0,SRCCOPY); } if(Mine[ii][jj].weitu==1) { dc.SelectObject(cBitmap[Mine[ii][jj].Num]); pDC->BitBlt(10+ii*15,50+15*jj,160,160,&dc,0,0,SRCCOPY); if(Mine[ii][jj].Num==10) ExpandMine(ii,jj); } if(GameOver==1&&Mine[ii][jj].Num==-1) { dc.SelectObject(cFalg[2]); pDC->BitBlt(10+ii*15,50+jj*15,160,160,&dc,0,0,SRCCOPY); } } int OldDC=pDC->SaveDC(); pDC->SetTextColor(RGB(255,0,0)); pDC->SetBkColor(RGB(0,0,0)); CFont cFont; if(!cFont.CreatePointFont(200,"Comic Sans MS")) { AfxMessageBox("字体失败"); } pDC->SelectObject(cFont); str.Format("剩%-4d",LeftNum); pDC->TextOut(11,10,str); if(Second==60) { Miute++; if(Miute==60) {Miute=0; Hour++; } Second=0; str.Format("0%d%d",Miute,Second); } str.Format("%d%d%d",Hour,Miute,Second); pDC->TextOut(201,10,str); pDC->RestoreDC(OldDC); } ///////////////////////////////////////////////////////////////////////////// // CMyMineView diagnostics #ifdef _DEBUG void CMyMineView::AssertValid() const { CView::AssertValid(); } void CMyMineView::Dump(CDumpContext& dc) const { CView::Dump(dc); } CMyMineDoc* CMyMineView::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyMineDoc))); return (CMyMineDoc*)m_pDocument; } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CMyMineView message handlers void CMyMineView::OnStart() { // TODO: Add your command handler code here int n,i; srand(time(NULL)); GameOver=0; Row=0; List=0; MineNum=0; Second=0; LeftNum=50; Hour=0; Miute=0; for(n=0;n<16;n++) for(i=0;i<16;i++) {Mine[n][i].weitu=0; Mine[n][i].Num=0; } do {Row=rand()%15; List=rand()%15; if(Mine[Row][List].Num!=-1) {Mine[Row][List].Num=-1; MineNum++; } } while(MineNum!=50); for(int aa=0;aa<16;aa++) for(int b=0;b<16;b++) if(Mine[aa][b].Num==0) { for(int c=aa-1;c<aa+2;c++) for(int d=b-1;d<b+2;d++) if(c>=0&&c<16&&d>=0&&d<16) if(Mine[c][d].Num==-1) Mine[aa][b].Num++; } Invalidate(); } void CMyMineView::OnRButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default if(GameOver) return; if((point.x>=10)&&(point.x<=385)&&(point.y>=50)&&(point.y<=290)) { int a=(point.x-10)/15; int b=(point.y-50)/15; if(Mine[a][b].weitu==0||Mine[a][b].weitu==3) { Mine[a][b].weitu=2; LeftNum--; CRect rect1(10,10,80,50);//重画剩余雷数 InvalidateRect(&rect1); CRect rect;//重画右击块区域 rect.left=a*15+10; rect.right=a*15+25; rect.top=b*15+50; rect.bottom=b*15+65; InvalidateRect(&rect); } else if(Mine[a][b].weitu==2) {Mine[a][b].weitu=3; LeftNum++; CRect rect1(10,10,80,50); InvalidateRect(&rect1); CRect rect; rect.left=a*15+10; rect.right=a*15+25; rect.top=b*15+50; rect.bottom=b*15+65; InvalidateRect(&rect); } } CView::OnRButtonDown(nFlags, point); } void CMyMineView::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and(or) call defaul if(GameOver==1) return; CDC *pDC=GetDC(); CDC Dc; CString str; if(!Dc.CreateCompatibleDC(pDC)) AfxMessageBox("Can't create DC"); //显示按下按钮 if(point.x>=120&&point.x<=150&&point.y>=10&&point.y<=40) { Dc.SelectObject(cFace[3]); pDC->BitBlt(120,10,160,160,&Dc,0,0,SRCCOPY); } if((point.x>=10)&&(point.x<=385)&&(point.y>=50)&&(point.y<=290)) { Dc.SelectObject(cFace[1]); pDC->BitBlt(120,10,160,160,&Dc,0,0,SRCCOPY); int aw=(point.x-10)/15;//lie////////////////////////////////////////就是这里啊,好郁闷啊,aw应该是列,bw是行啊 int bw=(point.y-50)/15;//hang str.Format("x is %-4d y is %-4d 行 %-4d 列 %-4d",point.x,point.y,bw,aw); pDC->TextOut(10,300,str); { if(Mine[aw][bw].weitu!=2)//////////////////// { /////////////////// if(Mine[aw][bw].Num==-1)///////////////////但这里Mine[][]应该是Mine[bw][aw]为什么是Mine[aw][bw] ///////////////////////////////////////////////////////////////////高手快来解救我吧 不明白啊 { GameOver=1; Invalidate(); } else { if(Mine[aw][bw].Num==0) ExpandMine(aw,bw); Mine[aw][bw].weitu=1; CRect rect; rect.left=aw*15+10; rect.right=aw*15+25; rect.top=bw*15+50; rect.bottom=bw*15+65; InvalidateRect(&rect); } } } } CView::OnLButtonDown(nFlags, point); } void CMyMineView::OnLButtonUp(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default PTCHAR str1="失败"; CDC *pDC=GetDC(); CDC Dc; if(!Dc.CreateCompatibleDC(pDC)) {AfxMessageBox(str1); } Dc.SelectObject(cFace[0]); pDC->BitBlt(120,10,160,160,&Dc,0,0,SRCCOPY); if(GameOver) { Dc.SelectObject(cFace[2]); pDC->BitBlt(120,10,160,160,&Dc,0,0,SRCCOPY); } if(point.x>=120&&point.x<=150&&point.y>=10&&point.y<=40) { OnStart(); } CView::OnLButtonUp(nFlags, point); } int CMyMineView::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CView::OnCreate(lpCreateStruct) == -1) return -1; // TODO: Add your specialized creation code here return 0; } int CMyMineView::Victor() { if(LeftNum==0) {for(int i=0;i<16;i++) for(int j=0;j<16;j++) {if(Mine[i][j].weitu==0||Mine[i][j].weitu==3) return FALSE; } return TRUE; } return FALSE; } void CMyMineView::ExpandMine(int Row,int Col) { int x,y; Mine[Row][Col].weitu=1; CRect Rect(10+15*Row,50+15*Col,25+15*Row,65+15*Col); InvalidateRect(Rect); if(Mine[Row][Col].Num==0) {for(x=Row-1;x<Row+2;x++) for(y=Col-1;y<Col+2;y++) if(x>=0&&x<=15&&y>=0&&y<=15&&!(x==Row&&y==Col)&&Mine[x][y].weitu==0&&Mine[x][y].Num!=-1) { ExpandMine(x,y); } } }