朵云

I'm OK

简易五子棋实现

一开始看见别人在写五子棋,我不太认同他的算法。于是,我自己试着写了一个。

1 #include <windows.h>
2 LRESULT CALLBACK WndProc (HWND, UINT,WPARAM, LPARAM);
wuziqi.h
 1 #include "wuziqi.h"
 2 
 3 LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
 4 
 5 int WINAPI WinMain (HINSTANCE hInstance,//实例句柄
 6                     HINSTANCE hPrevInstance,//应用程序的先前实例的句柄;对于一个32位程序,该参数总为NULL
 7                     PSTR szCmdLine,//指定传递给应用程序的命令行参数
 8                     int iCmdShow)//启动参数,表示“常规窗口”、“最小化”、“最大化”等启动状态
 9 {
10      static TCHAR szAppName[] = TEXT ("HelloWin") ;
11      HWND         hwnd ;//窗口句柄
12      MSG          msg ;//消息结构体;用于从消息队列获取消息的结构体参数
13      WNDCLASS     wndclass ;//窗口结构体
14 
15      wndclass.style         = CS_HREDRAW | CS_VREDRAW ; //窗口样式
16      wndclass.lpfnWndProc   = WndProc ; //窗口消息处理过程函数
17      wndclass.cbClsExtra    = 0 ;  //预留的位元组数
18      wndclass.cbWndExtra    = 0 ;  //预留的位元组数
19      wndclass.hInstance     = hInstance ;  //赋予实例句柄
20      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;//图标
21      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ; //游标
22      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ; //背景色
23      wndclass.lpszMenuName  = NULL ;  //菜单栏名字表
24      wndclass.lpszClassName = szAppName ; //程序类名
25 
26      //检测是否是NT之类的系统
27      if (!RegisterClass (&wndclass))//注册一个窗口类别
28      {
29           MessageBox (NULL, TEXT ("This program requires Windows NT!"),  //显示消息框
30                       szAppName, MB_ICONERROR) ;
31           return 0 ;
32      }               
33 
34      //基于窗口类创建一个窗口
35      hwnd = CreateWindow (szAppName,                  //窗口类名称  
36                           TEXT ("五子棋"), //窗口标题
37                           WS_OVERLAPPEDWINDOW,        //窗口风格
38                           CW_USEDEFAULT,              //初始X坐标
39                           CW_USEDEFAULT,              //初始y坐标
40                           360,              //初始x尺寸
41                           380,              //初始y尺寸
42                           NULL,                       //父窗口句柄
43                           NULL,                       //窗口菜单句柄
44                           hInstance,                  //程序实例句柄
45                           NULL) ;                     //创建参数
46 
47      ShowWindow (hwnd, iCmdShow) ; //显示窗口
48      UpdateWindow (hwnd) ;//优先WM_PAINT绘制窗口
49 
50      //从消息队列获取消息 
51      while (GetMessage (&msg, NULL, 0, 0))
52      {
53           TranslateMessage (&msg) ;//翻译一些键盘消息
54           DispatchMessage (&msg) ;//将消息发送给窗口过程
55      }
56 
57      return msg.wParam ;
58 }
main.c
  1 #include "wuziqi.h"
  2 
  3 #define Bai 1        //白色
  4 #define Hei 2        //黑色
  5 #define Wu  3        //
  6 
  7 typedef struct
  8 {
  9     int x;
 10     int y;
 11     int color;
 12 }
 13 qizi;                       //定义棋子结构
 14 
 15 static qizi map[256];    //棋盘
 16 
 17 static     int   color=Bai;
 18 
 19 void MapData()//初始化棋盘
 20 {
 21     int i,j;
 22     for(i=0;i<16;i++)
 23         for(j=0;j<16;j++)
 24         {
 25             map[i*16+j].x=i*20+20;
 26             map[i*16+j].y=j*20+20;
 27             map[i*16+j].color=Wu;
 28         }
 29 }
 30 
 31 int GetColor(int x,int y)//输入棋子坐标查找颜色
 32 {
 33     int i;
 34     for(i=0;i<256;i++)
 35     {
 36         if(map[i].x==x)
 37             if(map[i].y==y)
 38                 return map[i].color;
 39     }
 40     return 0;
 41 }
 42 
 43 void SetColor(int x,int y,int color)//更改颜色
 44 {
 45     int i;
 46     for(i=0;i<256;i++)
 47     {
 48         if(map[i].x==x)
 49             if(map[i].y==y)
 50             {
 51                 map[i].color=color;
 52                 break;
 53             }
 54     }
 55 }
 56 
 57 int GetMouse(int x,int y,int *x2,int *y2)   //鼠标点击范围获取
 58 {
 59         if(x%20<=8)
 60         {
 61             *x2=x-x%20;
 62             if(y%20<=8)
 63             {
 64                 *y2=y-y%20;
 65                 return 1;
 66             }
 67             if(y%20>=12)
 68             {
 69                 *y2=y-y%20+20;
 70                 return 1;
 71             }
 72         }
 73         if(x%20>=12)
 74         {
 75             *x2=x-x%20+20;
 76             if(y%20<=8)
 77             {
 78                 *y2=y-y%20;
 79                 return 1;
 80             }
 81             if(y%20>=12)
 82             {
 83                 *y2=y-y%20+20;
 84                 return 1;
 85             }
 86         }
 87         return 0;
 88 }
 89 
 90 void HuiZhiQiZi(HWND hwnd,HDC hdc,int color,int x,int y)//绘制棋子
 91 {
 92     if(color==Bai)
 93     {
 94         hdc=GetDC(hwnd);
 95         SelectObject(hdc,GetStockObject(WHITE_BRUSH));
 96         Ellipse(hdc,x-8,y-8,x+8,y+8);
 97         ReleaseDC(hwnd,hdc);
 98     }
 99     if(color==Hei)
100     {
101         hdc=GetDC(hwnd);
102         SelectObject(hdc,GetStockObject(BLACK_BRUSH));
103         Ellipse(hdc,x-8,y-8,x+8,y+8);
104         ReleaseDC(hwnd,hdc);

105     }
106 }
107 
108 void WinBox(int color)
109 {
110     if(color==Bai)
111         MessageBox (NULL, TEXT ("白棋获胜!!!"), TEXT ("duoyun"), MB_OKCANCEL) ;
112     if(color==Hei)
113         MessageBox (NULL, TEXT ("黑棋获胜!!!"), TEXT ("duoyun"), MB_OKCANCEL) ;
114 }
115 
116 int WinH1(int time,int color,int x,int y)
117 {
118     if(time==5)
119         return time;
120     else if(GetColor(x,y)==color)
121         WinH1(time+1,color,x-20,y);
122     else
123         return time;
124 }
125 
126 int WinH2(int time,int color,int x,int y)
127 {
128     if(time==5)
129         return time;
130     else if(GetColor(x,y)==color)
131         WinH2(time+1,color,x+20,y);
132     else
133         return time;
134 }
135 
136 int WinS1(int time,int color,int x,int y)
137 {
138     if(time==5)
139         return time;
140     else if(GetColor(x,y)==color)
141         WinS1(time+1,color,x,y-20);
142     else
143         return time;
144 }
145 
146 int WinS2(int time,int color,int x,int y)
147 {
148     if(time==5)
149         return time;
150     else if(GetColor(x,y)==color)
151         WinS2(time+1,color,x,y+20);
152     else
153         return time;
154 }
155 
156 int WinZX1(int time,int color,int x,int y)
157 {
158     if(time==5)
159         return time;
160     else if(GetColor(x,y)==color)
161         WinZX1(time+1,color,x-20,y-20);
162     else
163         return time;
164 }
165 
166 int WinZX2(int time,int color,int x,int y)
167 {
168     if(time==5)
169         return time;
170     else if(GetColor(x,y)==color)
171         WinZX2(time+1,color,x+20,y+20);
172     else
173         return time;
174 }
175 
176 int WinYX1(int time,int color,int x,int y)
177 {
178     if(time==5)
179         return time;
180     else if(GetColor(x,y)==color)
181         WinYX1(time+1,color,x+20,y-20);
182     else
183         return time;
184 }
185 
186 int WinYX2(int time,int color,int x,int y)
187 {
188     if(time==5)
189         return time;
190     else if(GetColor(x,y)==color)
191         WinYX2(time+1,color,x-20,y+20);
192     else
193         return time;
194 }
195 
196 void Win(int time,int color,int x,int y)
197 {
198     if(  ( WinH1(time,color,x,y)+WinH2(time,color,x,y) ) >5)
199         WinBox(color);
200     if(  ( WinS1(time,color,x,y)+WinS2(time,color,x,y) ) >5)
201         WinBox(color);
202     if(  ( WinZX1(time,color,x,y)+WinZX2(time,color,x,y) ) >5)
203         WinBox(color);
204     if(  ( WinYX1(time,color,x,y)+WinYX2(time,color,x,y) ) >5)
205         WinBox(color);
206 }
207 
208 LRESULT CALLBACK WndProc (HWND hwnd, //窗口句柄
209                           UINT message, //消息ID
210                           WPARAM wParam, //消息数据
211                           LPARAM lParam)//消息数据
212 {
213      HDC         hdc ;//设备环境句柄
214      PAINTSTRUCT ps ;//绘图信息结构体
215      //RECT        rect ;//矩形结构体
216      int         i,j;
217      //POINT       point;
218      int         x,y,x2=0,y2=0;
219      int         time=0;
220 
221      //处理消息ID
222      switch (message)
223      {
224      case WM_CREATE:
225          MapData();
226          return 0;     
227 
228      case WM_LBUTTONDOWN:
229          x=LOWORD(lParam);
230          y=HIWORD(lParam);
231          if(GetMouse(x,y,&x2,&y2) && (GetColor(x2,y2)==3) )
232          {
233              
234              hdc=GetDC(hwnd);
235              ReleaseDC(hwnd,hdc);
236              
237              SetColor(x2,y2,color);
238              HuiZhiQiZi(hwnd,hdc,color,x2,y2);
239              
240              Win(time,color,x2,y2);
241 
242              color=(Bai+Hei)-color;
243          }
244          return 0;
245 
246      case WM_PAINT:
247          hdc = BeginPaint (hwnd, &ps) ;//窗口绘制开始
248          //GetClientRect (hwnd, &rect) ; //获取窗口客户区的尺寸
249          for(i=20;i<=(16*20);i+=20)
250          {
251              MoveToEx(hdc,i,20,NULL);
252              LineTo(hdc,i,320);
253          }
254          for(j=20;j<=(16*20);j+=20)
255          {
256              MoveToEx(hdc,20,j,NULL);
257              LineTo(hdc,320,j);
258          }
259          for(i=0;i<256;i++)
260          {
261              if(map[i].color==Bai)
262                  HuiZhiQiZi(hwnd,hdc,Bai,map[i].x,map[i].y);
263              if(map[i].color==Hei)
264                  HuiZhiQiZi(hwnd,hdc,Hei,map[i].x,map[i].y);
265          }
266 
267          EndPaint (hwnd, &ps) ; //结束窗口绘制
268          return 0 ;
269      case WM_DESTROY:
270          PostQuitMessage (0) ;//将‘退出’消息插入消息队列
271          return 0 ;
272      }
273      return DefWindowProc (hwnd, message, wParam, lParam) ;//执行默认的消息处理
274 }

 

posted on 2015-08-26 19:33  朵云  阅读(198)  评论(0)    收藏  举报

导航