📄 chessdlg.cpp
字号:
m_recMessage.SetWindowText(NULL);
if(m_pServerSocket)
{
delete m_pServerSocket;
m_pServerSocket=NULL;
}
if(m_pClientSocket)
delete m_pClientSocket,m_pClientSocket=NULL;
ShowSelDlg();
UpdateWindow();
}
void CChessDlg::OnHelpAbout()
{
// TODO: Add your command handler code here
CAboutDlg dlg;
dlg.DoModal();
}
void CChessDlg::OnGameExit()
{
// TODO: Add your command handler code here
if(m_pServerSocket)
delete m_pServerSocket,m_pServerSocket=NULL;
if(m_pClientSocket)
delete m_pClientSocket,m_pClientSocket=NULL;
KillTimer(1);
CDialog::OnOK();
}
//DEL void CChessDlg::OnOk()
//DEL {
//DEL return;}
//DEL void CChessDlg::OnCancel()
//DEL {
//DEL return;}
void CChessDlg::ShowSelDlg()
{
CSelDialog dlg;
if(dlg.DoModal()!=IDCANCEL)
{
// m_bReady=TRUE;
m_strIP=dlg.m_addIP;
if(dlg.IsServer())
{
m_pServerSocket=new CServerSocket(this);
if(!m_pServerSocket->Create(SOCKETPORT))
{
AfxMessageBox(IDS_SERSOCKET_FAILED);
return ;
}
m_pServerSocket->Listen();
m_bServer=TRUE;
}
else
{
m_pClientSocket=new CClientSocket(this);
if(!m_pClientSocket->Create())
AfxMessageBox("cannot create clientsocket");
m_bServer=FALSE;
if(!m_pClientSocket->Connect(m_strIP,6080))
m_recMessage.SetWindowText("对方服务器没有启动,请等待!"),m_bConnected=FALSE;
else
m_bConnected=TRUE;
}
}
else
{
m_bReady=FALSE;
}
}
void CChessDlg::OnOK()
{
// TODO: Add extra validation here
return;
CDialog::OnOK();
}
void CChessDlg::OnCancel()
{
// TODO: Add extra cleanup here
//return;
OnGameExit();
CDialog::OnCancel();
}
void CChessDlg::OnSend()
{
if(!m_bReady)
return; // TODO: Add your control notification handler code here
if(m_bServer)
{
int i=m_sendMessage.GetWindowTextLength();
CString str;
m_sendMessage.GetWindowText(str);
m_pServerSocket->m_pClient->Send(MESSAGE,(void*)str.GetBuffer(str.GetLength()),i);
}
else
{
int i=m_sendMessage.GetWindowTextLength();
CString str;
m_sendMessage.GetWindowText(str);
m_pClientSocket->Send(MESSAGE,(void*)str.GetBuffer(str.GetLength()),i);
}
m_sendMessage.SetWindowText(NULL);
}
void CChessDlg::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
CDialog::OnChar(nChar, nRepCnt, nFlags);
}
BOOL CChessDlg::PreTranslateMessage(MSG* pMsg)
{
if(pMsg->message==WM_KEYDOWN)
if(pMsg->wParam==VK_ESCAPE)
return TRUE;
return CDialog::PreTranslateMessage(pMsg);
}
BOOL CChessDlg::IsFive(BOOL bServer,int posx,int posy)
{
m_bServer=bServer;
int positionx,positiony;
if(m_bServer)
{
if(m_whitePosList.GetCount()<5)
return FALSE;
//判断水平方向
positionx=posx;
positiony=posy;
int chessCount=0;
while(1)
{
if((positionx-32)>=25&&InList(m_bServer,positionx-32,positiony))
chessCount++,positionx=positionx-32;
else
break;
}
positionx=posx;
positiony=posy;
while(1)
{
if((positionx+32)<=665&&InList(m_bServer,positionx+32,positiony))
chessCount++,positionx=positionx+32;
else
break;
}
if(chessCount>=4)
return TRUE;
//判断垂直方向
positionx=posx;
positiony=posy;
chessCount=0;
while(1)
{
if((positiony-32)>=85 && InList(m_bServer,positionx,positiony-32))
chessCount++,positiony=positiony-32;
else
break;
}
positionx=posx;
positiony=posy;
while(1)
{
if((positiony+32)<=565 && InList(m_bServer,positionx,positiony+32))
chessCount++,positiony=positiony+32;
else
break;
}
if(chessCount>=4)
return TRUE;
//判断反斜线方向
positionx=posx;
positiony=posy;
chessCount=0;
while(1)
{
if((positionx-32)>=25 && (positiony-32)>=85 && InList(m_bServer,positionx-32,positiony-32))
chessCount++,positiony=positiony-32,positionx=positionx-32;
else
break;
}
positionx=posx;
positiony=posy;
while(1)
{
if((positiony+32)<=565 && (positionx+32)<=665 && InList(m_bServer,positionx+32,positiony+32))
chessCount++,positiony=positiony+32,positionx=positionx+32;
else
break;
}
if(chessCount>=4)
return TRUE;
//判断正斜线方向
positionx=posx;
positiony=posy;
chessCount=0;
while(1)
{
if((positionx+32)<=665 && (positiony-32)>=85 && InList(m_bServer,positionx+32,positiony-32))
chessCount++,positiony=positiony-32,positionx=positionx+32;
else
break;
}
positionx=posx;
positiony=posy;
while(1)
{
if((positiony+32)<=565 && (positionx-32)>=25 && InList(m_bServer,positionx-32,positiony+32))
chessCount++,positiony=positiony+32,positionx=positionx-32;
else
break;
}
if(chessCount>=4)
return TRUE;
}
else
{
if(m_blackPosList.GetCount()<5)
return FALSE;
//判断水平方向
positionx=posx;
positiony=posy;
int chessCount=0;
while(1)
{
if((positionx-32)>=25&&InList(m_bServer,positionx-32,positiony))
chessCount++,positionx=positionx-32;
else
break;
}
positionx=posx;
positiony=posy;
while(1)
{
if((positionx+32)<=665&&InList(m_bServer,positionx+32,positiony))
chessCount++,positionx=positionx+32;
else
break;
}
if(chessCount>=4)
return TRUE;
//判断垂直方向
positionx=posx;
positiony=posy;
chessCount=0;
while(1)
{
if((positiony-32)>=85 && InList(m_bServer,positionx,positiony-32))
chessCount++,positiony=positiony-32;
else
break;
}
positionx=posx;
positiony=posy;
while(1)
{
if((positiony+32)<=565 && InList(m_bServer,positionx,positiony+32))
chessCount++,positiony=positiony+32;
else
break;
}
if(chessCount>=4)
return TRUE;
//判断反斜线方向
positionx=posx;
positiony=posy;
chessCount=0;
while(1)
{
if((positionx-32)>=25 && (positiony-32)>=85 && InList(m_bServer,positionx-32,positiony-32))
chessCount++,positiony=positiony-32,positionx=positionx-32;
else
break;
}
positionx=posx;
positiony=posy;
while(1)
{
if((positiony+32)<=565 && (positionx+32)<=665 && InList(m_bServer,positionx+32,positiony+32))
chessCount++,positiony=positiony+32,positionx=positionx+32;
else
break;
}
if(chessCount>=4)
return TRUE;
//判断正斜线方向
positionx=posx;
positiony=posy;
chessCount=0;
while(1)
{
if((positionx+32)<=665 && (positiony-32)>=85 && InList(m_bServer,positionx+32,positiony-32))
chessCount++,positiony=positiony-32,positionx=positionx+32;
else
break;
}
positionx=posx;
positiony=posy;
while(1)
{
if((positiony+32)<=565 && (positionx-32)>=25 && InList(m_bServer,positionx-32,positiony+32))
chessCount++,positiony=positiony+32,positionx=positionx-32;
else
break;
}
if(chessCount>=4)
return TRUE;
}
return FALSE;
}
BOOL CChessDlg::InList(BOOL bServer, int posx, int posy)
{
int i;
i=posx<<16|posy;
POSITION pos;
if(m_bServer)
{
pos=m_whitePosList.GetHeadPosition();
while(pos)
{
if(i==(int)m_whitePosList.GetNext(pos))
return TRUE;
}
return FALSE;
}
else
{
pos=m_blackPosList.GetHeadPosition();
while(pos)
{
if(i==(int)m_blackPosList.GetNext(pos))
return TRUE;
}
return FALSE;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -