这是简单的五子棋程序,你自己去运行一下看看吧:
#include <windows.h>
#include <math.h>
#define CB_SIDE_X 50 //棋盘左上角X轴位置
#define CB_SIDE_Y 60 //棋盘左上角Y轴位置
#define CB_LEN 15 //棋盘长度(行列数)
#define CB_WIDTH 30 //棋盘格宽
#define CHESS_WIDTH 13
#define IDE_RESULT 101
#define IDB_NUM7 117
#define IDB_NUM8 118
typedef struct ChessDef
{
int CB_MaxWidth ;
COLORREF chessColor_White ;
COLORREF chessColor_Black ;
};
typedef struct Site
{
int x ;
int y ;
};
int chessMan[CB_LEN][CB_LEN] = {0} ;
Site mouse ;
int flag = 1, chessKey = 0;
char gameState = 0 ;
RECT moveSite ;
HWND hwnd ;
ChessDef chess ;
int cxClient, cyClient ;
HWND hEditResult, hButtonOptSum1_Restart, hButtonOptSum1_Exit ;
HINSTANCE hInst ;
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int drawChessBroad(HDC hdc, ChessDef chess) ;
void drawMouseSite(HDC hdc) ;
int checkChessWin(Site checkSize) ;
int Restart() ;
int outGame(int mode) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("SineWave") ;
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 (LTGRAY_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox ( NULL, TEXT ("Program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow ( szAppName, TEXT ("五子棋"),
WS_OVERLAPPEDWINDOW,
10, 10,
800, 600,
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)
{
HDC hdc ;
PAINTSTRUCT ps ;
RECT rect;
Site recordSize ;
chess.CB_MaxWidth = (CB_LEN-1)*CB_WIDTH;
chess.chessColor_White = RGB (255, 255, 255) ;
chess.chessColor_Black = RGB (0, 0, 0) ;
GetSysColorBrush(20);
switch (message)
{
case WM_CREATE:
hEditResult=CreateWindow("EDIT", //建立文本框
NULL,
WS_CHILD | WS_VISIBLE | ES_RIGHT | WS_BORDER,
530,60,
160,24,
hwnd,
(HMENU)IDE_RESULT,
hInst,
NULL);
SetWindowText(hEditResult,"进行时");
hButtonOptSum1_Restart=CreateWindow("BUTTON", //建立按钮7
"重新开始",
WS_CHILD | WS_VISIBLE,
530,140,
80,30,
hwnd,
(HMENU) IDB_NUM7,
hInst,
NULL);
hButtonOptSum1_Exit=CreateWindow("BUTTON", //建立按钮7
"退出",
WS_CHILD | WS_VISIBLE,
530,180,
80,30,
hwnd,
(HMENU) IDB_NUM8,
hInst,
NULL);
break ;
case WM_SIZE:
cxClient = LOWORD (lParam) ;
cyClient = HIWORD (lParam) ;
return 0 ;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
drawChessBroad(hdc, chess) ;
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_LBUTTONDOWN :
mouse.x = LOWORD (lParam) ;
mouse.y = HIWORD (lParam) ;
if(!gameState
&&mouse.x < CB_SIDE_X+chess.CB_MaxWidth+CHESS_WIDTH
&& mouse.y < CB_SIDE_Y+chess.CB_MaxWidth+CHESS_WIDTH
&& mouse.x > CB_SIDE_X-CHESS_WIDTH
&& mouse.y > CB_SIDE_X-CHESS_WIDTH)
{
InvalidateRect (hwnd, &moveSite, FALSE) ;
recordSize.x = (mouse.x-CB_SIDE_X)/CB_WIDTH ;
recordSize.y = (mouse.y-CB_SIDE_Y)/CB_WIDTH ;
if(!chessMan[recordSize.x][recordSize.y])
{
chessMan[recordSize.x][recordSize.y] = chessKey+1 ;
if(checkChessWin(recordSize) != 255)
{
if(chessKey)
SetWindowText(hEditResult,"黑棋胜利");
else
SetWindowText(hEditResult,"白棋胜利");
gameState = 1;
}
else
{
chessKey = !chessKey ;
if(chessKey)
SetWindowText(hEditResult,"黑棋");
else
SetWindowText(hEditResult,"白棋");
}
}
}
return 0;
case WM_MOUSEMOVE:
mouse.x = LOWORD (lParam) ;
mouse.y = HIWORD (lParam) ;
if(mouse.x < CB_SIDE_X+chess.CB_MaxWidth+CB_WIDTH
&& mouse.y < CB_SIDE_Y+chess.CB_MaxWidth+CB_WIDTH
&& mouse.x > CB_SIDE_X-CB_WIDTH
&& mouse.y > CB_SIDE_Y-CB_WIDTH)
{
if(flag == 1)
{
InvalidateRect (hwnd, NULL, FALSE) ;
}
else
{
InvalidateRect (hwnd, &moveSite, FALSE) ;
}
flag = 0;
}
else
{
flag = 1;
}
return 0;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDB_NUM7:
Restart() ;
break ;
case IDB_NUM8:
outGame(1);
break;
}
return 0;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
int drawChessBroad(HDC hdc, ChessDef chess)
{
int i, j;
HBRUSH hBrush ;
hBrush = CreateSolidBrush (RGB(255, 50, 255)) ;
SelectObject (hdc, hBrush) ;
Rectangle(hdc
, CB_SIDE_X-CB_WIDTH
, CB_SIDE_Y-CB_WIDTH
, CB_SIDE_X+chess.CB_MaxWidth+CB_WIDTH
, CB_SIDE_Y+chess.CB_MaxWidth+CB_WIDTH);
DeleteObject (hBrush) ;
if(mouse.x < CB_SIDE_X+chess.CB_MaxWidth+CB_WIDTH
&& mouse.y < CB_SIDE_Y+chess.CB_MaxWidth+CB_WIDTH
&& mouse.x > CB_SIDE_X-CB_WIDTH
&& mouse.y > CB_SIDE_Y-CB_WIDTH)
{
drawMouseSite(hdc);
}
for(i = 0;i < CB_LEN;i++)
{
MoveToEx (hdc, i*CB_WIDTH+CB_SIDE_X, CB_SIDE_Y, NULL) ;
LineTo(hdc, i*CB_WIDTH+CB_SIDE_X, chess.CB_MaxWidth+CB_SIDE_Y) ;
MoveToEx (hdc, CB_SIDE_X, i*CB_WIDTH+CB_SIDE_Y, NULL) ;
LineTo(hdc,chess.CB_MaxWidth+CB_SIDE_X , i*CB_WIDTH+CB_SIDE_Y) ;
}
for(i = 0;i < CB_LEN;i++)
for(j = 0;j < CB_LEN;j++)
{
if(chessMan[i][j] == 1)
{
hBrush = CreateSolidBrush (chess.chessColor_White) ;
SelectObject (hdc, hBrush) ;
Ellipse (hdc
, i*CB_WIDTH+CB_SIDE_X-CHESS_WIDTH
, j*CB_WIDTH+CB_SIDE_Y-CHESS_WIDTH
, i*CB_WIDTH+CB_SIDE_X+CHESS_WIDTH
, j*CB_WIDTH+CB_SIDE_Y+CHESS_WIDTH) ;
DeleteObject (hBrush) ;
}
if(chessMan[i][j] == 2)
{
hBrush = CreateSolidBrush (chess.chessColor_Black) ;
SelectObject (hdc, hBrush) ;
Ellipse (hdc
, i*CB_WIDTH+CB_SIDE_X-CHESS_WIDTH
, j*CB_WIDTH+CB_SIDE_Y-CHESS_WIDTH
, i*CB_WIDTH+CB_SIDE_X+CHESS_WIDTH
, j*CB_WIDTH+CB_SIDE_Y+CHESS_WIDTH) ;
DeleteObject (hBrush) ;
}
}
}
void drawMouseSite(HDC hdc)
{
HBRUSH hBrush ;
moveSite.top = ((mouse.y-CB_SIDE_Y)/CB_WIDTH)*CB_WIDTH+CB_SIDE_Y-CHESS_WIDTH-3 ;
moveSite.left = ((mouse.x-CB_SIDE_X)/CB_WIDTH)*CB_WIDTH+CB_SIDE_X-CHESS_WIDTH-3 ;
moveSite.bottom = ((mouse.y-CB_SIDE_Y)/CB_WIDTH)*CB_WIDTH+CB_SIDE_Y+CHESS_WIDTH+3 ;
moveSite.right = ((mouse.x-CB_SIDE_X)/CB_WIDTH)*CB_WIDTH+CB_SIDE_X+CHESS_WIDTH+3 ;
hBrush = CreateSolidBrush (RGB(252, 50, 0)) ;
SelectObject (hdc, hBrush) ;
Rectangle(hdc, moveSite.left, moveSite.top, moveSite.right, moveSite.bottom);
DeleteObject (hBrush) ;
}
/*
5 4 6
\|/
1 - - 3
/|\
7 2 8
*/
int checkChessWin(Site checkSize)
{
int i, j ;
int flagDirect = 0 ;
for(i = 1;i < 5;i++)
{
// 1
if(checkSize.x+i >= CB_LEN
|| chessMan[checkSize.x+i][checkSize.y] != chessKey+1)
{
flagDirect = flagDirect|1 ;
}
// 2
if(checkSize.y+i >= CB_LEN
|| chessMan[checkSize.x][checkSize.y+i] != chessKey+1)
{
flagDirect = flagDirect|2 ;
}
// 3
if(checkSize.x-i < 0
|| chessMan[checkSize.x-i][checkSize.y] != chessKey+1)
{
flagDirect = flagDirect|4 ;
}
// 4
if(checkSize.y-i < 0
|| chessMan[checkSize.x][checkSize.y-i] != chessKey+1)
{
flagDirect = flagDirect|8 ;
}
// 5
if(checkSize.x-i < 0 && checkSize.y-i < 0
|| chessMan[checkSize.x-i][checkSize.y-i] != chessKey+1)
{
flagDirect = flagDirect|16 ;
}
// 6
if(checkSize.x+i >= CB_LEN && checkSize.y-i < 0
|| chessMan[checkSize.x+i][checkSize.y-i] != chessKey+1)
{
flagDirect = flagDirect|32 ;
}
// 7
if(checkSize.x-i < 0 && checkSize.y+i >= CB_LEN
|| chessMan[checkSize.x-i][checkSize.y+i] != chessKey+1)
{
flagDirect = flagDirect|64 ;
}
// 8
if(checkSize.x+i >= CB_LEN && checkSize.y+i >= CB_LEN
|| chessMan[checkSize.x+i][checkSize.y+i] != chessKey+1)
{
flagDirect = flagDirect|128 ;
}
}
return flagDirect;
}
int Restart()
{
int i, j;
for(i = 0;i < CB_LEN;i++)
for(j = 0;j < CB_LEN;j++)
{
chessMan[i][j] = 0;
}
flag = 1 ;
chessKey = 0;
gameState = 0;
SetWindowText(hEditResult,"进行时");
InvalidateRect (hwnd, NULL, FALSE) ;
}
int outGame(int mode)
{
switch(mode)
{
case 0:
exit(0);
case 1:
if(MessageBox(hwnd, "是否要退出游戏", "五子棋", MB_YESNO) == 7)
{
InvalidateRect (hwnd, NULL, FALSE) ;
return 0;
}
exit(0);
}
}