Windows编程 简易五子棋

简易五子棋,望指正

#include <windows.h>
#include<commctrl.h>
#define SIZE 20
UINT IDC_BUTTON1=200;
HWND hwnd; 
HPEN penback=CreatePen(PS_SOLID,1,RGB(0,0,0));
HPEN penback2=CreatePen(PS_SOLID,1,RGB(255,255,255));
HBRUSH brushback=CreateSolidBrush(RGB(255,255,255));
HBRUSH chessblack=CreateSolidBrush(RGB(0,0,0));
HBRUSH chesswhite=CreateSolidBrush(RGB(255,255,255));
int map[SIZE-1][SIZE-1]={0}; 
const int white=2;
const int black=1;
int sum=1;


HDC hdc;
POINT pt={0,0};
int i=0;
void printbackground()
{hdc=GetDC(hwnd);
 SelectObject(hdc,brushback);  
 SelectObject(hdc,penback2);
 Rectangle(hdc,0,0,390,390);  
 SelectObject(hdc,penback);            	
      	for(int i=0;i<SIZE;i++)
			for(int j=0;j<SIZE;j++)
                 Rectangle(hdc,j*15+7,i*15+7,(j+1)*15+7,(i+1)*15+7);     //棋子放在方格点上 
                 //Rectangle(hdc,j*15,i*15,(j+1)*15,(i+1)*15);//棋子放在方格内 
                     
}

int whitewin()   //判断白棋是否赢 
{for(int i=0;i<SIZE;i++)
  for(int j=0;j<SIZE;j++)
    if(map[i][j]==white)
     {for(int dx=-1;dx<=1;dx++)
       for(int dy=-1;dy<=1;dy++)
        if(dx!=0||dy!=0)
         { sum=1;                                                                                
           for(int m=1;m<=4;m++)
            {int x=i+dx*m;
             int y=j+dy*m;
             if(x<0||x>=SIZE||y<0||y>=SIZE||map[x][y]!=white) break;
			 else sum++;		
		  }
		  if(sum==5) return 1;
	  }
    }
    return 0;      
}

int blackwin()    //判断黑棋是否赢 
{for(int i=0;i<SIZE;i++)
  for(int j=0;j<SIZE;j++)
    if(map[i][j]==black)
     {for(int dx=-1;dx<=1;dx++)
       for(int dy=-1;dy<=1;dy++)
        if(dx!=0||dy!=0)
         {  sum=1;
           for(int m=1;m<=9;m++)
            {int x=i+dx*m;
             int y=j+dy*m;
             if(x<0||x>=SIZE||y<0||y>=SIZE||map[x][y]!=black) break;
			 else sum++;		
		  }
		  if(sum==5) return 1;
		  
	  }
	  
     
    }
    return 0;      
}






LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
	switch(Message) {
		
	
		case WM_DESTROY: {
			PostQuitMessage(0);
			break;
		}

		case WM_LBUTTONDOWN:{
			GetCursorPos(&pt);
			ScreenToClient(hwnd,&pt);
			hdc=GetDC(hwnd);
            if(i==0){SelectObject(hdc,chessblack);i=1;map[pt.y/15][pt.x/15]=black;}
			else {SelectObject(hdc,chesswhite);i=0;map[pt.y/15][pt.x/15]=white;} 
            if(pt.y/15<=SIZE&&pt.x/15<=SIZE) Ellipse(hdc,pt.x/15*15,pt.y/15*15,pt.x/15*15+15,pt.y/15*15+15);
            if(blackwin()) {MessageBox(hwnd,"黑棋赢","游戏结束",MB_OKCANCEL);printbackground();memset(map,0,sizeof(map));i=0;}
            if(whitewin()) {MessageBox(hwnd,"白棋赢","游戏结束",MB_OKCANCEL);printbackground();memset(map,0,sizeof(map));i=0;}
            //searchblack(0,0,0);
            //searchwhite(0,0,0);
            
              //第pt.y/15行,pt.x/15列有棋子 
			
			break;
		}
		
		

		default:
			return DefWindowProc(hwnd, Message, wParam, lParam);
	}
	return 0;
}



int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
	WNDCLASSEX wc;
	MSG msg; 


	memset(&wc,0,sizeof(wc));
	wc.cbSize		 = sizeof(WNDCLASSEX);
	wc.lpfnWndProc	 = WndProc; 
	wc.hInstance	 = hInstance;
	wc.hCursor		 = LoadCursor(NULL, IDC_ARROW);
	
	
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
	wc.lpszClassName = "WindowClass";
	wc.hIcon		 = LoadIcon(NULL, IDI_APPLICATION); 
	wc.hIconSm		 = LoadIcon(NULL, IDI_APPLICATION); 

	if(!RegisterClassEx(&wc)) {
		MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
		return 0;
	}

	hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Caption",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, /* x */
		CW_USEDEFAULT, /* y */
		390, /* width */
		390, /* height */
		NULL,NULL,hInstance,NULL);

	if(hwnd == NULL) {
		MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
		return 0;
	}
	printbackground();

			
		
	while(GetMessage(&msg, NULL, 0, 0) > 0) { 
		TranslateMessage(&msg); 
		DispatchMessage(&msg); 
	}
	return msg.wParam;
}
发布了2 篇原创文章 · 获赞 2 · 访问量 52

猜你喜欢

转载自blog.csdn.net/weixin_45867464/article/details/104413895