共有回帖数 0 个
- 全代码]C&SDK网络版五子棋...求喷...求砖...求建议....
-
只看楼主
收藏
回复
-
这个小程序主要难点并没有什么.但我觉得.相对于我这种Windows程序设计才刚看到鼠标一章的新手来说.他需求的知识点特别多.
代码在二楼贴上...
首先要贴上两个头文件...两头文件都是一些函数的声明..没有其它的作用..
/**********************MyWnd.h********************/
#include windows.h
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg,WPARAM wParam, LPARAM lParam );
int RegWndClass();
int CreateWnd(LPCTSTR lpTitle);
int Run();
int OnPaint(HWND hWnd, WPARAM wParam, LPARAM lParam);
int OnChar(HWND hWnd, WPARAM wParam, LPARAM lParam);
int OnBlackClick(HWND hWnd, WPARAM wParam, LPARAM lParam);
int GameOver(POINT pt);
void initGame();
int OnBtnClick(HWND hWnd, WPARAM wParam, LPARAM lParam);
/***************MySocket.h*******************/
#define WM_BLACK WM_USER+1
void initSocket();
int MyListen();
int MyConnect();
int SendMyMsg(POINT pt);
DWORD WINAPI RecvMyMsg(LPVOID lpVoid);
int SocketRun(BOOL bListen);
我擦了...度娘吃我的空格和回车....- -
主函数..../***********************main**********************/
#include windows.h
#include stdio.h
#include "MyWnd.h"
HINSTANCE g_hInstance;
HWND hwnd;
int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // command line
int nCmdShow // show state
)
{
g_hInstance = hInstance;
RegWndClass();
CreateWnd("五子棋V1.0");
Run();
return 0;
}
/*************************MySocket.cpp*********************/
#include windows.h
#include winsock.h
#include "MySocket.h"
#pragma comment(lib, "ws2_32.lib")
LPCTSTR m_lpHost = "117.42.47.203"; // 客户端连接IP地址
int m_iPort = 1788; // 端口
static SOCKET m_socket;
extern HWND hwnd;
//初始化套接字
void initSocket()
{
WSADATA wsa;
WSAStartup(MAKEWORD(1,1),&wsa);
}
//监听
int MyListen()
{
SOCKADDR_IN sock_in;
SOCKET sock = socket(AF_INET,SOCK_STREAM,0);
sock_in.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
sock_in.sin_family = AF_INET;
sock_in.sin_port = htons(m_iPort);
bind(sock,(SOCKADDR *)&sock_in,sizeof(SOCKADDR));
listen(sock,10);
MessageBox(hwnd,"等待用户连接","",0);
int m = sizeof(SOCKADDR_IN);
memset(&sock_in, 0, sizeof(sock_in));
m_socket = accept(sock,(SOCKADDR *)&sock_in,&m);
MessageBox(hwnd,"用户连接成功","",0);
return 0;
}
//连接
int MyConnect()
{
SOCKADDR_IN sock_in;
m_socket = socket(AF_INET,SOCK_STREAM,0);
sock_in.sin_addr.S_un.S_addr = inet_addr(m_lpHost);
sock_in.sin_family = AF_INET;
sock_in.sin_port = htons(m_iPort);
return connect(m_socket,(SOCKADDR *)&sock_in,sizeof(SOCKADDR)) == 0 ? 0 : 1;
}
//发送消息
int SendMyMsg(POINT pt)
{
int iBack = send(m_socket, (char*)&pt, sizeof(pt), 0);
return iBack != SOCKET_ERROR ? 0 : 1;
}
//接收消息
DWORD WINAPI RecvMyMsg(LPVOID lpVoid)
{
HWND hwnd = (HWND)lpVoid;
POINT pt;
while (1)
{
int iBack = recv(m_socket, (char*)&pt, sizeof(pt), 0);
if (iBack == SOCKET_ERROR )
{
continue;
}
PostMessage(hwnd, WM_BLACK, (WPARAM)&pt, NULL);
}
}
//连接运行
int SocketRun(BOOL bListen)
{
initSocket();
switch (bListen)
{
case TRUE:
{
MyListen();
break;
}
case FALSE:
{
if (MyConnect() == 0)
{
MessageBox(hwnd,"连接成功","",0);
}
break;
}
default:break;
}
HANDLE hThread = CreateThread(NULL, 0, RecvMyMsg, hwnd, 0, NULL);
CloseHandle(hThread);
return 0;
}
/*****************************MyWnd.cpp*****************************/
#include windows.h
#include "MyWnd.h"
#include "MySocket.h"
#define N 24
extern HINSTANCE g_hInstance;
extern HWND hwnd;
static int iArray[24][24];
static int g_Ver = 0; //1表示黑子.2表示白子.0表示没有子
static int g_bClick = FALSE; //是否能下子
static int cxClient,cyClient;
BOOL bListen = FALSE; //是否是主机
BOOL g_over1 = FALSE; //黑子是否赢了.
BOOL g_over2 = FALSE; //白子是否赢了.
POINT pt;
int RegWndClass()
{
WNDCLASS wndclass;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hbrBackground = CreateSolidBrush(RGB(225,221,44));
wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wndclass.hInstance = g_hInstance;
wndclass.lpfnWndProc = WindowProc;
wndclass.lpszClassName = "wuziqi";
wndclass.lpszMenuName = NULL;
wndclass.style = 0;
RegisterClass(&wndclass);
return 0;
}
int CreateWnd(LPCTSTR lpTitle)
{
hwnd = CreateWindow("wuziqi",lpTitle,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,582,582,NULL,NULL,g_hInstance,NULL);
ShowWindow(hwnd,SW_SHOWNORMAL);
return 0;
}
int Run()
{
MSG msg;
UpdateWindow(hwnd);
while (GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
int OnPaint(HWND hWnd, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
RECT rect;
GetClientRect(hWnd,&rect);
hdc = BeginPaint(hWnd,&ps);
cxClient = rect.right;
cyClient = rect.bottom;
int i,j;
//画棋盘
for (i = 10; i cxClient; i += 24)
{
MoveToEx(hdc,i,10,NULL);
LineTo(hdc,i,cyClient - 10);
}
for (i = 10; i cyClient; i+= 24)
{
MoveToEx(hdc,10,i,NULL);
LineTo(hdc,cxClient - 10,i);
}
//画棋子
for (i = 0; i N; i++)
{
for (j = 0; j N; j++)
{
if (iArray[j] == 1)
{
SelectObject(hdc,GetStockObject(BLACK_BRUSH));
int x, y;
x = 24 * i;
y = 24 * j;
Ellipse(hdc,x,y,x + 20,y + 20);
}
else if (iArray[j] == 2)
{
SelectObject(hdc,GetStockObject(WHITE_BRUSH));
int x, y;
x = 24 * i;
y = 24 * j;
Ellipse(hdc,x,y,x + 20,y + 20);
}
}
}
EndPaint(hWnd,&ps);
return 0;
}
//响应键盘按键消息
int OnChar(HWND hWnd, WPARAM wParam, LPARAM lParam)
{
if (wParam == VK_F2)
{
bListen = TRUE;
g_bClick = TRUE;
bListen == TRUE? g_Ver = 1 : g_Ver = 2;
SocketRun(bListen);
}
if (wParam == VK_F1)
{
bListen = FALSE;
g_bClick = FALSE;
bListen == TRUE? g_Ver = 1 : g_Ver = 2;
SocketRun(bListen);
}
if (wParam == VK_F3)
{
initGame();
return 0;
}
return 0;
}
//重新开始游戏
void initGame()
{
if (g_over1 == TRUE)
{
g_over1 = FALSE;
if (g_Ver == 1)
{
g_bClick = FALSE;
}
if (g_Ver == 2)
{
g_bClick = TRUE;
}
}
if (g_over2 == TRUE)
{
g_over2 = FALSE;
if (g_Ver == 1)
{
g_bClick = TRUE;
}
if (g_Ver == 2)
{
g_bClick = FALSE;
}
}
ZeroMemory(iArray, sizeof(iArray));
InvalidateRect(hwnd,NULL,TRUE);
}
//WM_BLACK消息处理函数
int OnBlackClick(HWND hWnd, WPARAM wParam, LPARAM lParam)
{
POINT *pt = (POINT*)wParam;
if (pt-x == -1 && pt-y == -1)
{
g_over1 = TRUE;
g_bClick = FALSE;
if (IDYES == MessageBox(hWnd,"黑子赢,是否重来.~","",MB_YESNO))
{
initGame();
return 0;
}
else
{
DestroyWindow(hWnd);
}
}
else if (pt-x == -2 && pt-y == -2)
{
g_over2 = TRUE;
g_bClick = FALSE;
if (MessageBox(hWnd,"白子赢,是否重来.","",MB_YESNO))
{
initGame();
return 0;
}
else
{
DestroyWindow(hWnd);
}
}
else
{
g_bClick = TRUE; // 对方落子后设置当前可落子
if (g_Ver == 1)
{
iArray[pt-x][pt-y] = 2;
}
if (g_Ver == 2)
{
iArray[pt-x][pt-y] = 1;
}
}
InvalidateRect(hWnd, NULL, TRUE);
return 0;
}
LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
switch (uMsg)
{
case WM_PAINT:
{
OnPaint(hwnd,wParam,lParam);
return 0;
}
case WM_CREATE:
{
ZeroMemory(iArray, sizeof(iArray));
return 0;
}
case WM_LBUTTONDOWN:
{
OnBtnClick(hwnd,wParam,lParam);
return 0;
}
case WM_KEYDOWN:
{
OnChar(hwnd,wParam,lParam);
return 0;
}
case WM_BLACK:
{
OnBlackClick(hwnd,wParam,lParam);
return 0;
}
case WM_CLOSE:
{
if (IDYES == MessageBox(hwnd,"正在游戏是否退出?","五子棋",MB_YESNO | MB_ICONQUESTION))
{
DestroyWindow(hwnd);
}
return 0;
}
case WM_DESTROY:
{
PostQuitMessage(0);
}
}
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}






楼主 2015-12-05 12:45 回复
Copyright © 2010~2015 直线网 版权所有,All Rights Reserved.沪ICP备10039589号
意见反馈 |
关于直线 |
版权声明 |
会员须知