#include <windows.h> #include <math.h> #define num 10 LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); void draw(HWND hwnd); void drawo(HWND hwnd); void win(HWND hwnd); BOOL check(int i,int j); static int cxBlock,cyBlock; HDC hdc; int x,y; static int ident=0; RECT rect; static int fstate[num][num]; HBRUSH hbrush1; int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow ) { static TCHAR szAppName[]=TEXT("Checker1"); 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(WHITE_BRUSH); wndclass.lpszMenuName=NULL; wndclass.lpszClassName=szAppName; if(!RegisterClass(&wndclass)) { MessageBox(NULL,TEXT("This programe requires windows NT"),szAppName,MB_ICONERROR); return 0; } hwnd = CreateWindow(szAppName, TEXT("Checker1"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL ); 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) { switch(message) { case WM_SIZE: cxBlock=LOWORD(lParam)/num; cyBlock=HIWORD(lParam)/num; return 0; case WM_MOVE: draw(hwnd); drawo(hwnd); break; case WM_LBUTTONDOWN: if (ident==0) { draw(hwnd); } x=LOWORD(lParam)/cxBlock; y=HIWORD(lParam)/cyBlock; if(x<num && y<num && fstate[x][y]==0) { hdc=GetDC(hwnd); if (ident%2==0) { fstate[x][y]=1; hbrush1=(HBRUSH)GetStockObject(BLACK_BRUSH); } else { fstate[x][y]=2; hbrush1=(HBRUSH)GetStockObject(WHITE_BRUSH); } SelectObject(hdc,hbrush1); Ellipse(hdc,(x+0.8)*cxBlock,(y+1)*cyBlock-0.2*cxBlock,(x+1.2)*cxBlock,(y+1)*cyBlock+0.2*cxBlock); ReleaseDC(hwnd,hdc); ident++; win(hwnd); } else MessageBeep(0); return 0; case WM_PAINT: // draw(hwnd); // drawo(hwnd); return 0; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hwnd,message,wParam,lParam); } void draw(HWND hwnd) { hdc=GetDC(hwnd); for (x=0;x<num;x++) for(y=0;y<num;y++) { Rectangle(hdc,x*cxBlock,y*cyBlock,(x+1)*cxBlock,(y+1)*cyBlock); } ReleaseDC(hwnd,hdc); } void drawo(HWND hwnd) { hdc=GetDC(hwnd); for (x=0;x<num;x++) for(y=0;y<num;y++) { if (fstate[x][y]) { if (fstate[x][y]==1) { hbrush1=(HBRUSH)GetStockObject(BLACK_BRUSH); } else hbrush1=(HBRUSH)GetStockObject(WHITE_BRUSH); SelectObject(hdc,hbrush1); Ellipse(hdc,(x+0.8)*cxBlock,(y+1)*cyBlock-0.2*cxBlock,(x+1.2)*cxBlock,(y+1)*cyBlock+0.2*cxBlock); } } ReleaseDC(hwnd,hdc); } void win(HWND hwnd) { BOOL check(int ,int); int i,j; for(i=0;i<num;i++) for(j=0;j<num;j++) { if (fstate[i][j]) if (check(i,j)==1) draw(hwnd); } return 0; } BOOL check(int i,int j) { int temp; int x,y; temp=0; for (x=i,y=j;y<num;y++) { if (fstate[x][y]==fstate[i][j]) temp++; else break; if (temp==5) return 1; } for (x=i,y=(j-1);y>=0;y--) { if (fstate[x][y]==fstate[i][j]) temp++; else break; if (temp==5) return 1; } temp=0; for (x=i,y=j;x<num;x++) { if (fstate[x][y]==fstate[i][j]) temp++; else break; if (temp==5) return 1; } for (x=i-1,y=j;x>=0;x--) { if (fstate[x][y]==fstate[i][j]) temp++; else break; if (temp==5) return 1; } temp=0; for (x=i,y=j;x<num && y<num;x++,y++) { if (fstate[x][y]==fstate[i][j]) temp++; else break; if (temp==5) return 1; } for (x=i,y=j;x>=0 && y>=0;x--,y--) { if (fstate[x][y]==fstate[i][j]) temp++; else break; if (temp==5) return 1; } return 0; }
posted on 2009-11-18 23:34 karying 阅读(193) 评论(0) 编辑 收藏 举报
Powered by: 博客园 Copyright © 2025 karying Powered by .NET 9.0 on Kubernetes