今天用Windows API写的扫雷程序
[code] #include #include #include #include "resource.h" // 菜单资源头文件 #define SHADOW_WIDTH1 1 //阴影宽度 #define SHADOW_WIDTH2 2 #define SHADOW_WIDTH3 3 #define BORDER 8 //边界的宽度 #define BOX_WIDTH 20//box的宽度 #define BOX_HEIGHT 20 //box的高度 #define RESERVED_AREA 32 //保留区高度 //****************************** //自定义的函数声明 //****************************** LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ; void DrawBorder(HDC hdc,int x,int y,int width,int height,int BorderWidth,COLORREF &color1,COLORREF &color2,bool WTop); void AutoOpenBoxes(HDC &hdc,int i,int j); void drawRect(HDC &hdc,int x,int y); bool allRoundIsOpened(int row,int col); void InitBox(); void UpdateTheWindow(HWND &hwnd,int rows,int cols,int mineNO); void ComputeRoundMineNo(); void RandomSetMines(); //************************************** //结构体Box,roundMineNO表示周围雷的个数 //containMine表示是否里面是否有雷, //isOpened表示box是否已经打开 //************************************** static HMENU hMenu; struct Box { int roundMineNO; bool containMine; bool isOpened; }; static int mineNumber; static int ROWS; //行数 static int COLS; //列数 static int iSM_BorderX; static int iSM_BorderY; static int iSM_CaptionY; static int iSM_MenuY; static Box **box; static int i,j,numberOpened; static RECT mineRect; //******** //main函数 //******** int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { static TCHAR szAppName[] = TEXT ("SineWave") ; HWND hwnd ; MSG msg ; WNDCLASS wndclass ; mineNumber = 5; ROWS = 10; COLS = 10; mineNumber = 10; box = new Box* [COLS]; for(int k=0;k0&&box[i-1][j].containMine==true) { box[i][j].roundMineNO++; } if(i>0&&j0 && box[i+1][j-1].containMine == true) { box[i][j].roundMineNO++; } if(j>0 && box[i][j-1].containMine == true) { box[i][j].roundMineNO++; } if(i>0 && j>0 && box[i-1][j-1].containMine == true) { box[i][j].roundMineNO++; } } } //随机布雷 void RandomSetMines() { int row,col; srand((UINT)GetCurrentTime()); for(i=0;i= 0 && y >= 0 && x < ROWS && y < COLS && box[x][y].roundMineNO == 0&& !allRoundIsOpened(x,y)) { drawRect(hdc,x-1,y); drawRect(hdc,x-1,y+1); drawRect(hdc,x,y+1); drawRect(hdc,x+1,y+1); drawRect(hdc,x+1,y); drawRect(hdc,x+1,y-1); drawRect(hdc,x,y-1); drawRect(hdc,x-1,y-1); AutoOpenBoxes(hdc,x-1,y); AutoOpenBoxes(hdc,x-1,y+1); AutoOpenBoxes(hdc,x,y+1); AutoOpenBoxes(hdc,x+1,y+1); AutoOpenBoxes(hdc,x+1,y); AutoOpenBoxes(hdc,x+1,y-1); AutoOpenBoxes(hdc,x,y-1); AutoOpenBoxes(hdc,x-1,y-1); } } //绘制矩形 void UpdateTheWindow(HWND &hwnd,int rows,int cols,int mineNO) { ROWS = rows; COLS = cols; mineNumber = mineNO; mineRect.left = SHADOW_WIDTH3+BORDER+SHADOW_WIDTH2; mineRect.right =mineRect.left + BOX_WIDTH * COLS; mineRect.top = SHADOW_WIDTH3+2*BORDER+RESERVED_AREA+3*SHADOW_WIDTH2; mineRect.bottom = mineRect.top + BOX_HEIGHT * ROWS; box = new Box* [COLS]; for(int k=0;k< COLS;k++) box[k] = new Box[ROWS]; InitBox(); RandomSetMines(); ComputeRoundMineNo(); int cx = BOX_WIDTH * COLS + 2 * SHADOW_WIDTH3 + 2 * SHADOW_WIDTH2 + 2 * iSM_BorderX + 2 * BORDER; int cy = BOX_HEIGHT * ROWS + 2 * SHADOW_WIDTH3 + 4 * SHADOW_WIDTH2 + 2 * iSM_BorderY + 3 * BORDER + RESERVED_AREA+iSM_CaptionY+iSM_MenuY; MoveWindow(hwnd,0,0,cx,cy,true); } void drawRect(HDC &hdc,int x,int y) { RECT rect1; COLORREF color1 = RGB(200,200,200), color2 = RGB(120,120,200); TCHAR szBuffer[2]={"0"}; if(x<0 || y<0 || x >= ROWS || y >= COLS) return; rect1.left = mineRect.left + BOX_WIDTH * y; rect1.right = rect1.left + BOX_WIDTH; rect1.top = mineRect.top + BOX_HEIGHT * x; rect1.bottom = rect1.top + BOX_HEIGHT; if(!box[x][y].isOpened) { DrawBorder(hdc,rect1.left,rect1.top, BOX_WIDTH-2,BOX_HEIGHT-2,SHADOW_WIDTH2,color1,color1,true); DrawBorder(hdc,rect1.left,rect1.top, BOX_WIDTH,BOX_HEIGHT,SHADOW_WIDTH1,color2,color2,true); if(!box[x][y].containMine && box[x][y].roundMineNO != 0) { wsprintf(szBuffer,TEXT("%d"),box[x][y].roundMineNO); DrawText(hdc,szBuffer,-1,&rect1,DT_SINGLELINE|DT_CENTER|DT_VCENTER); } box[x][y].isOpened = true; numberOpened ++; if(numberOpened == ROWS * COLS - mineNumber ) { MessageBox (NULL, TEXT ("祝贺你赢了!"),TEXT("扫雷游戏"), MB_ICONWARNING) ; } } } //判断周围的所有box是否已经open bool allRoundIsOpened(int x,int y) { bool flag = true; if(x > 0 && !box[x-1][y].isOpened) flag = false; if(x > 0 && y < COLS-1 && !box[x-1][y+1].isOpened) flag = false; if(y < COLS-1 && !box[x][y+1].isOpened) flag = false; if(x < ROWS-1 && y< COLS-1 && !box[x+1][y+1].isOpened) flag = false; if(x < ROWS-1 && !box[x+1][y].isOpened) flag = false; if(x < ROWS-1 && y > 0 && !box[x+1][y-1].isOpened) flag = false; if(y > 0 && !box[x][y-1].isOpened) flag = false; if(x > 0 && y > 0 && !box[x-1][y-1].isOpened) flag = false; return flag; } [/code]