📄 gameclient.cpp
字号:
#include "stdafx.h"
#include"global.h"
#include "TCPClient.h"
#include "GobangDrawer.h"
#include "GameClient.h"
#include<string>
using namespace std;
CGameClient::CGameClient()
{
m_UserNum = 0;
m_bNetFree=true;
}
CGameClient::~CGameClient()
{
}
bool CGameClient::IsNetFree()
{
return m_bNetFree;
}
bool CGameClient::Register( string& strUserName ) //注册用户
{
if(!m_bConnected)
return(false);
int result;
int OpNum = REGISTER_USER;
int length = strUserName.length();
m_bNetFree=false;
Send( &OpNum, 4 );
Send( &length, 4 );
Send( strUserName.c_str(), length );
Receive( &result, 4 );
m_bNetFree=true;
return( result == OK );
}
//得到用户信息
bool CGameClient:: GetAllUserInfo( )
{
if(!m_bConnected)
return(false);
if(!m_bNetFree)
return(false);
int length;
int OpNum = QUERY_USERS;
m_bNetFree=false;
Send( &OpNum, 4 );
Receive( &length, 4 );
//info = new CUserInfo[length/sizeof(CUserInfo)];
//memset( info, 0, length );
Receive( m_info, length );
m_UserNum = length / sizeof(CUserInfo);
m_bNetFree=true;
return true;
}
//查询棋盘信息
bool CGameClient::QueryBoard()
{
if(!m_bConnected)
return(false);
int length;
char board[225];
int OpNum=QUERY_BOARD;
m_bNetFree=false;
Send(&OpNum,4);
Receive(&length,4);
Receive(board,length);
m_bNetFree=true;
drawer.SetBoard(board);
return(true);
}
//处理鼠标单击
bool CGameClient::MouseClick( int x, int y )
{
if(!m_bConnected)
return(false);
int OpNum = USER_ACTION;
int result;
ACTION action;
action.Action = PLAY;
int length = sizeof(ACTION);
char info[225]; //存储棋盘信息
//判断棋子是否则在棋盘内
if( (x < 55 ) || ( x > 370 ) || ( y < 40 ) || ( y > 350 ) )
return false;
int col=(x-55)/21;
int row=(y-40)/21;
if( (row < 0) || (row > 14) || (col < 0) || (col > 14) )
return false;
action.ActionInfo=(col<<8)+row;
m_bNetFree=false;
Send( &OpNum, 4 );
Send( &length, 4 );
Send( &action, length );
Receive( &result, 4 );
Receive( &length, 4 ); //接收长度
Receive( info, length );
m_bNetFree=true;
drawer.SetBoard( info ); //更新棋盘信息
return (result==OK);
}
// 画棋盘
bool CGameClient::Draw( CDC *pDC )
{
drawer.Draw( pDC );
return true;
}
//进入游戏室
bool CGameClient::EnterRoom( int num )
{
if(!m_bConnected)
return(false);
int OpNum = ENTER_ROOM;
int result;
m_bNetFree=false;
Send( &OpNum, 4 );
Send( &num, 4 );
Receive( &result, 4 );
m_bNetFree=true;
if(result!=OK)
return(false);
return(true);
}
// 退出游戏室
bool CGameClient::ExitRoom()
{
if(!m_bConnected)
return(false);
int OpNum = EXIT_ROOM;
int result;
m_bNetFree=false;
Send( &OpNum, 4 );
Receive( &result, 4 );
m_bNetFree=true;
return( result == OK );
}
//返回在线用户数
int CGameClient::GetUserCount()const
{
return m_UserNum;
}
//返回在线用户信息
CUserInfo CGameClient::GetUserInfo( const int num )const
{
return m_info[num];
}
//注销用户
bool CGameClient::Logout()
{
if(!m_bConnected)
return(false);
int OpNum=LOGOUT;
Send(&OpNum,4);
return(true);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -