SDK Windows编程(五子棋)

写比较麻烦,不过没牵涉到什么算法,只是一些逻辑问题
#include<tchar.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<windows.h>

LRESULT CALLBACK WndProc (HWND , UINT , WPARAM , LPARAM) ;

//定义主函数
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPreInst,LPSTR lpCmdLine,int nCmdShow){
	
	WNDCLASS wndclass ;
	HWND hWnd ;
	MSG msg ;
	char szWindowClass[] = "窗口示例" ; //窗口类名
	char szTitle[] = "My Windows" ; //窗口标题
	
	
	wndclass.cbClsExtra = 0 ;//窗口类无扩展
	wndclass.cbWndExtra = 0 ;  //窗口实例无扩展
	wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH) ;//窗口背景为白色
	wndclass.hCursor = LoadCursor(NULL , IDC_ARROW) ;//窗口采用箭头光标
	wndclass.hIcon = LoadIcon(hInstance ,MAKEINTRESOURCE(IDI_APPLICATION)) ;//窗口的最小化图标为默认图标
	wndclass.hInstance = hInstance ;//当前实例句柄
	wndclass.lpfnWndProc = WndProc ;//窗口处理函数为WndProc
	
	wndclass.lpszMenuName = NULL ; //窗口中无菜单
	wndclass.lpszClassName = szWindowClass ; //窗口的类名 
	wndclass.style = CS_HREDRAW | CS_VREDRAW|CS_DBLCLKS  ; //窗口类型为默认类型
	
	//.
	//注册窗口类
	if(!RegisterClass(&wndclass)){
		
		MessageBox(NULL , _T("窗口类注册失败!") , _T("窗口注册") , NULL) ;
		return 1 ;
		
	}
	
	
	//创建窗口
	hWnd=CreateWindow(
		
		szWindowClass ,  //窗口类名
		szTitle , //窗口实例的标题名
		WS_OVERLAPPEDWINDOW , //窗口的样式 
		CW_USEDEFAULT , CW_USEDEFAULT , //窗口左上角坐标为默认 //窗口右上角坐标为默认
		700 , 700 ,  //窗口宽度为默认 //窗口高度为默认 
		
		NULL , //此窗口无父窗口
		NULL ,  //此窗口无主菜单
		hInstance,  //创建此窗口应用程序的当前句柄
		NULL  //不使用一个传递给窗口的参数值的指
		
		);
	
	if(!hWnd){
		
		MessageBox(NULL , "创建窗口失败!" ,("创建窗口") , NULL) ;
		return 1 ;
	}
	
	ShowWindow(hWnd,nCmdShow);  //显示窗口
	UpdateWindow(hWnd);  //绘制用户区
	
	
	//消息循环
	while(GetMessage(&msg,NULL,0,0)){
		
		TranslateMessage(&msg);  //将消息的虚拟键转换为字符信息
		DispatchMessage(&msg);   //将信消息发送到指定窗口函数
		
	}
	
	return(int) msg.wParam ; //程序终止时讲信息返回系统
}

HFONT CreateMyFont(){
	
	return CreateFont(
		0,//字体高度
		0,//字体宽度
		0,
		0,
		600,//字体粗细
		0,//字体倾斜 0为非倾斜
		0,//下划线 0为非
		0,//中划线 0为非
		ANSI_CHARSET,
		OUT_DEFAULT_PRECIS,
		CLIP_DEFAULT_PRECIS,
		DEFAULT_QUALITY,
		DEFAULT_PITCH,
		"楷体_GB2312"//字体名
		) ;
}

typedef struct point{
	int x ,y ;
	int color ;
	int sf_flag ;
}MAP;
MAP map[15][15] ;
int a , b ;

void init(){
	for(a = 0 ;a<15 ;a++){		
		for(b = 0 ; b<15 ;b++){
			map[a][b].sf_flag = 0 ;//初始化每个点
			map[a][b].color = -1 ;
		}
	}
}

int c_flag = 0 ;
int w_b = 0 ;//0下黑棋 非白棋 
int pos_x ,pos_y ;
int x ,y ;
int Lbd_flag = 0;
int i_flag ,j_flag ;
int step1 = 0 ,step2=0 ,step3=0 ,step4=0 ;
char *str[]={"黑方赢! 点击重新开始。" ,"白方赢!点击重新开始。","黑方","白方"};
int over_flag = 0 ;

/
//窗口函数(回调函数)
LRESULT CALLBACK WndProc( HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam){
	
	HDC hDC ;
	RECT rect ;
	PAINTSTRUCT ps ;
	HFONT hF ;
	HBRUSH hBR1 ;
	HBRUSH hBR2 ;
	HPEN hPen1 ;
	HPEN hPen2 ;
	HCURSOR hcursor ;
	int i ,j;
	SIZE size ;
	switch(message)
	{
		
		//鼠标信息
	case WM_LBUTTONDOWN:
		pos_x = LOWORD(lParam) ;
		pos_y = HIWORD(lParam) ;
		Lbd_flag = 1 ;
		InvalidateRect(hWnd ,NULL ,TRUE) ;	
		break ;		
	case WM_MOUSEMOVE:		
		pos_x = LOWORD(lParam) ;
		pos_y = HIWORD(lParam) ;
		if(over_flag==0)
		InvalidateRect(hWnd ,NULL ,TRUE) ;
		break ;
	case WM_LBUTTONUP:
		
		break ;
	case WM_DESTROY:
		PostQuitMessage(0) ;
		break ;                            
	case WM_SIZE :
		InvalidateRect(hWnd ,NULL ,1) ;
		break ;
		
	case WM_PAINT:
		hDC = BeginPaint(hWnd ,&ps) ;
		GetClientRect(hWnd, &rect) ;
		hPen1 = CreatePen(PS_SOLID ,0 ,RGB(0,0,0)) ;
		hPen2 = CreatePen(PS_SOLID ,2 ,RGB(255,0,0)) ;
		hBR1 = CreateSolidBrush(RGB(255,255,255)) ;
		hBR2 = CreateSolidBrush(RGB(0,0,0)) ;
		SelectObject(hDC,hPen1) ;
		if(c_flag)
			TextOut(hDC,5,5,str[2],strlen(str[2])) ;
		else{
			GetTextExtentPoint32(hDC ,str[3],strlen(str[3]),&size) ;
			TextOut(hDC,rect.right-size.cx-5,5,str[3],strlen(str[3])) ;
		}


		//确定坐标
		for(i = 0 ;i<15 ;i++){		
			for(j = 0 ; j<15 ;j++){
				map[i][j].x = rect.right*(j+2)/18 ;
				map[i][j].y = rect.bottom*(i+2)/18 ;
			}
		}
		//画图		
		for(j = 0 ;j<15 ;j++){			
			MoveToEx(hDC,map[0][j].x,map[0][j].y ,NULL);
			LineTo(hDC,map[14][j].x ,map[14][j].y) ;
		}
		for(i = 0 ;i<15 ;i++){
			MoveToEx(hDC,map[i][0].x,map[i][0].y ,NULL);
			LineTo(hDC,map[i][14].x ,map[i][14].y) ;
		} 
		//五个小黑点
		SelectObject(hDC,hBR2) ;
		Ellipse(hDC ,map[3][3].x-4 ,map[3][3].y-4,
			map[3][3].x+4 ,map[3][3].y+4) ;
		Ellipse(hDC ,map[3][11].x-4 ,map[3][11].y-4,
			map[3][11].x+4 ,map[3][11].y+4) ;
		Ellipse(hDC ,map[7][7].x-4 ,map[7][7].y-4,
			map[7][7].x+4 ,map[7][7].y+4) ;
		Ellipse(hDC ,map[11][3].x-4 ,map[11][3].y-4,
			map[11][3].x+4 ,map[11][3].y+4) ;
		Ellipse(hDC ,map[11][11].x-4 ,map[11][11].y-4,
			map[11][11].x+4 ,map[11][11].y+4) ;
		//画小边界框与鼠标的变化
		for(i = 0 ;i < 15 ;i++){
			for(j = 0 ;j<15 ;j++){
				if(abs(pos_x-map[i][j].x)<rect.right/36 && abs(pos_y-map[i][j].y)<rect.bottom/36 ){
					x = map[i][j].x ;
					y = map[i][j].y ;
					hcursor = LoadCursor(NULL,IDC_SIZEALL);
					SetCursor(hcursor) ;
					break ;
				}
			}
		} 
		SelectObject(hDC,hPen2) ;
		MoveToEx(hDC,x-rect.right/36 ,y-rect.bottom/62 ,NULL) ;
		LineTo(hDC ,x-rect.right/36 ,y-rect.bottom/36) ;
		LineTo(hDC ,x-rect.right/62 ,y-rect.bottom/36) ;
		
		MoveToEx(hDC,x+rect.right/36 ,y-rect.bottom/62 ,NULL) ;
		LineTo(hDC ,x+rect.right/36 ,y-rect.bottom/36) ;
		LineTo(hDC ,x+rect.right/62 ,y-rect.bottom/36) ;
		
		MoveToEx(hDC,x-rect.right/36 ,y+rect.bottom/62 ,NULL) ;
		LineTo(hDC ,x-rect.right/36 ,y+rect.bottom/36) ;
		LineTo(hDC ,x-rect.right/62 ,y+rect.bottom/36) ;
		
		MoveToEx(hDC,x+rect.right/36 ,y+rect.bottom/62 ,NULL) ;
		LineTo(hDC ,x+rect.right/36 ,y+rect.bottom/36) ;
		LineTo(hDC ,x+rect.right/62 ,y+rect.bottom/36) ;
		
		//点击下棋
		if(Lbd_flag&&over_flag==0){
			for(i = 0 ;i<15 ;i++){		
				for(j = 0 ; j<15 ;j++){
					if(!map[i][j].sf_flag &&abs(pos_x-map[i][j].x)<rect.right/36 && abs(pos_y-map[i][j].y)<rect.bottom/36 ){
						map[i][j].sf_flag = 1 ;//标示该点已经被下了
						i_flag = i ;
						j_flag = j ;
						if(c_flag){//1代表黑色 0为白色
							map[i][j].color = c_flag;
							c_flag = 0 ;
						}else{
							map[i][j].color = c_flag ;
							c_flag = 1 ;
						}
					}		
				}
				
			}
			
		}
		//初始化
		if(over_flag){
			init();
			over_flag = 0 ;
		}
		SelectObject(hDC,hPen1) ;
		//画出棋子
		for(i = 0 ;i<15 ;i++){		
			for(j = 0 ; j<15 ;j++){
				if(map[i][j].sf_flag){
					if(map[i][j].color==1){
						SelectObject(hDC,hBR2) ;
						Ellipse(hDC,map[i][j].x-12,map[i][j].y-12,
							map[i][j].x+12,map[i][j].y+12) ;
					}else{
						SelectObject(hDC,hBR1) ;
						Ellipse(hDC,map[i][j].x-12 ,map[i][j].y-12,
							map[i][j].x+12,map[i][j].y+12) ;
					}
				}
			}
		}
		
		if(Lbd_flag){
			//判断一行是否有五子
			for(j=j_flag ;j<15 ;j++){
				//往右判断
				if(map[i_flag][j].sf_flag){
					if(map[i_flag][j].color==map[i_flag][j_flag].color){
						step1++ ;	
					}else
						break ;
				}
			}
			for(j=j_flag;j>0;j--){
				//往左
				if(map[i_flag][j].sf_flag){
					if(map[i_flag][j].color==map[i_flag][j_flag].color)
						step1++ ;
					else
						break ;
				}
			}

			//判断一列是否有五子
			for(i=i_flag ;i<15 ;i++){
				//往上判断
				if(map[i][j_flag].sf_flag){
					if(map[i][j_flag].color==map[i_flag][j_flag].color){
						step2++ ;	
					}else
						break ;
				}
			}
			for(i=i_flag;i>0;i--){
				//往下
				if(map[i][j_flag].sf_flag){
					if(map[i][j_flag].color==map[i_flag][j_flag].color)
						step2++ ;
					else
						break ;
				}
			}
			//判断\的方向
			for(i=i_flag,j=j_flag ;i<15 ,j<15;i++,j++){
				//往上判断
				if(map[i][j].sf_flag){
					if(map[i][j].color==map[i_flag][j_flag].color){
						step3++ ;	
					}else
						break ;
				}
			}
			for(i=i_flag,j=j_flag;i>0,j>0;i--,j--){
				//往下
				if(map[i][j].sf_flag){
					if(map[i][j].color==map[i_flag][j_flag].color)
						step3++ ;
					else
						break ;
				}
			}
			//判断/的方向
			for(i=i_flag,j=j_flag ;i>0 ,j<15;i--,j++){
				//往上判断
				if(map[i][j].sf_flag){
					if(map[i][j].color==map[i_flag][j_flag].color){
						step4++ ;	
					}else
						break ;
				}
			}
			for(i=i_flag,j=j_flag;i<15,j>0;i++,j--){
				//往下
				if(map[i][j].sf_flag){
					if(map[i][j].color==map[i_flag][j_flag].color)
						step4++ ;
					else
						break ;
				}
			}
			
			if(step1>5||step2>5||step3>5||step4>5){
				SetTextColor(hDC,RGB(255,0,0));
				SelectObject(hDC,hF) ;	
				GetTextExtentPoint32(hDC ,str[0],strlen(str[0]),&size) ;
				if(map[i_flag][j_flag].color==1)
					TextOut(hDC,(rect.right/2-size.cx/2),rect.bottom/18,str[0],strlen(str[0])) ;
				if(map[i_flag][j_flag].color==0)
					TextOut(hDC,(rect.right/2-size.cx/2),rect.bottom/18,str[1],strlen(str[1])) ;
				over_flag = 1 ;
			} 
			step1 = 0 ;
			step2 = 0 ;
			step3 = 0 ;
			step4 = 0 ;
			Lbd_flag = 0 ;
		}
		DeleteObject(hBR1) ;
		DeleteObject(hBR2) ;		
		DeleteObject(hPen1) ;
		DeleteObject(hPen2) ;
		EndPaint(hWnd,&ps) ;
		break ;
	default:
		return DefWindowProc(hWnd , message , wParam ,lParam) ;
		break ;		
		}
		
		return  0 ;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值