//五子棋 #include <windows.h> static int next; static int Flag[8][8]; bool IsGameOver(int x,int y); LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ; int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { static TCHAR szAppName[] = TEXT ("5子棋") ; HWND hwnd ; MSG msg ; WNDCLASS wndclass ; wndclass.style = CS_HREDRAW | CS_VREDRAW ; wndclass.lpfnWndProc = WndProc ; wndclass.cbClsExtra = 0 ; wndclass.cbWndExtra = 0 ; wndclass.hInstance = hInstance ; wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ; wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ; wndclass.hbrBackground= (HBRUSH) GetStockObject (GRAY_BRUSH) ; wndclass.lpszMenuName = NULL ; wndclass.lpszClassName= szAppName ; if (!RegisterClass (&wndclass)) { MessageBox ( NULL, TEXT ("This program requires Windows NT!"), szAppName, MB_ICONERROR) ; return 0 ; } RECT rcRect = { 0, 0, GetSystemMetrics(SM_CXFULLSCREEN), GetSystemMetrics(SM_CYFULLSCREEN) }; int minSize = min(rcRect.right, rcRect.bottom); rcRect.right = rcRect.bottom = minSize; hwnd = CreateWindow( szAppName, // window class name TEXT ("5子棋游戏"), // window caption WS_OVERLAPPEDWINDOW, // window style rcRect.left,// initial x position rcRect.top,// initial y position rcRect.right,// initial x size rcRect.bottom,// initial y size NULL, // parent window handle NULL, // window menu handle hInstance, // program instance handle NULL) ; // creation parameters ShowWindow (hwnd, iCmdShow) ; UpdateWindow (hwnd) ; while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage (&msg) ; DispatchMessage (&msg) ; } return msg.wParam ; } LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc ; PAINTSTRUCT ps ; RECT rect ; int i, j; static double cxClient,cyClient,cxSize,cySize; POINT Point; static RECT JX[8][8]; switch (message) { case WM_CREATE: return 0 ; case WM_SIZE: cxClient=LOWORD(lParam); cyClient=HIWORD(lParam); cxSize=cxClient/8.0; cySize=cyClient/8.0; for(i=0;i <8;i++) { for(j=0;j <8;j++) { JX[i][j].left=(int)(j*cxSize); JX[i][j].top=(int)(i*cySize); JX[i][j].right=(int)((j+1)*cxSize); JX[i][j].bottom=(int)((i+1)*cySize); } } return 0; case WM_LBUTTONDOWN: if(next==0) { Point.x=LOWORD(lParam); Point.y=HIWORD(lParam); for(i=0;i <8;i++) { for(j=0;j <8;j++) { if(PtInRect(&JX[i][j],Point)) { if(Flag[i][j]!=0) break; else { Flag[i][j]=1; hdc=GetDC(hwnd); Ellipse(hdc,JX[i][j].left,JX[i][j].top, JX[i][j].right,JX[i][j].bottom); next=1; ReleaseDC(hwnd,hdc); if(IsGameOver(i,j)) MessageBox(hwnd,TEXT("LeftButton WIN"),TEXT("结果"),MB_OK); } } } } } return 0; case WM_RBUTTONDOWN: if(next==1) { Point.x=LOWORD(lParam); Point.y=HIWORD(lParam); for(i=0;i <8;i++) { for(j=0;j <8;j++) { if(PtInRect(&JX[i][j],Point)) { if( Flag[i][j]!=0) break; else { Flag[i][j]=2; hdc=GetDC(hwnd); MoveToEx(hdc,JX[i][j].left,JX[i][j].top,NULL); LineTo(hdc,JX[i][j].right,JX[i][j].bottom); MoveToEx(hdc,JX[i][j].right,JX[i][j].top,NULL); LineTo(hdc,JX[i][j].left,JX[i][j].bottom); next=0; ReleaseDC(hwnd,hdc); if(IsGameOver( i,j)) MessageBox(hwnd,TEXT("LeftButton WIN"),TEXT("结果"),MB_OK); } } } } } return 0; case WM_PAINT: hdc = BeginPaint (hwnd, &ps) ; GetClientRect (hwnd, &rect) ; for(j=1;j <8;j++) { MoveToEx(hdc,(int)(j*cxSize),0,NULL); LineTo(hdc,(int)(j*cxSize), (int)cyClient); } for(i=1;i <8;i++) { MoveToEx(hdc,0,(int)(i*cySize),NULL); LineTo(hdc,(int)cxClient,(int)(i*cySize)); } return 0 ; case WM_DESTROY: PostQuitMessage (0) ; return 0 ; } return DefWindowProc (hwnd, message, wParam, lParam) ; } bool IsGameOver(int x,int y) { int X=x, Y=y, n=Flag[x][y], A=1; while(Flag[X+1][Y]==n) { if((X+1)>7) break; A++; X++; } X=x, Y=y; while(Flag[X-1][Y]==n) { if((X-1) <0) break; A++; X--; } if(A>=5) return true; else A=1; X=x, Y=y; while(Flag[X][Y+1]==n) { if((Y+1)>7) break; A++; Y++; } X=x, Y=y; while(Flag[X][Y-1]==n) { if((Y-1) <0) break; A++; Y--; } if(A>=5) return true; else A=1; X=x, Y=y; while(Flag[X+1][Y+1]==n) { if((X+1)>7|| (Y+1)>7) break; A++; X++; Y++; } X=x, Y=y; while(Flag[X-1][Y-1]==n) { if((X-1) <0||(Y-1) <0) break; A++; X--; Y--; } if(A>=5) return true; else A=1; X=x, Y=y; while(Flag[X+1][Y-1]==n) { if((X+1)>7||(Y-1) <0) break; A++; X++; Y--; } X=x, Y=y; while(Flag[X-1][Y+1]==n) { if((X-1) <0||(Y+1)>7) break; A++; X--; Y++; } if(A>=5) return true; else A=1; return false; }