clientsocket.cpp

来自「利用Visual c++编程思想方法实现五子棋游戏的程序设计整个过程」· C++ 代码 · 共 142 行

CPP
142
字号
// ClientSocket.cpp : implementation file
//

#include "stdafx.h"
#include "ff.h"
#include "ffView.h"
#include "ffDoc.h"
#include "ClientSocket.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CClientSocket

CClientSocket::CClientSocket(CFfView* pView)
{
	m_pView = pView;
}

CClientSocket::~CClientSocket()
{
}


// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CClientSocket, CSocket)
	//{{AFX_MSG_MAP(CClientSocket)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif	// 0

/////////////////////////////////////////////////////////////////////////////
// CClientSocket member functions

void CClientSocket::OnReceive(int nErrorCode) 
{ 
	// TODO: Add your specialized code here and/or call the base class
	int buf[3];
	Receive(buf, 3 * sizeof(int));

	// get a newgame request
	if(buf[0] == NEWGAME)
	{
		m_pView->SendMessage(WM_USER_NEWGAME);
	}
	// partner refused the newgame request
	else if(buf[0] == NONEWGAME)
	{
		m_pView->GetParent()->SetWindowText(_T("Ygj - Game over"));
		AfxMessageBox(_T("Your partner doesn't want to play a new game now"));
	}
	// partner disconnect the link to you
	else if(buf[0] == DISCONNECT)
	{
		m_pView->m_bLinked = FALSE;
		m_pView->m_bInGame = FALSE;
		m_pView->m_bFlag = TRUE;
		m_pView->ResetCoords();
		m_pView->GetParent()->SetWindowText(_T("Ygj - Ready"));
		AfxMessageBox(_T("Your partner disconnected!"));
		m_pView->DestroyClientSocket();
	}
	// partner accept the connection request or the newgame request
	else if(buf[0] == ACCEPT)
	{
		m_pView->m_bMyTurn = FALSE;
		m_pView->m_bLinked = TRUE;
		m_pView->m_bOpponentWin = FALSE;
		m_pView->m_bInGame = TRUE;
		m_pView->m_bFlag = TRUE;
		m_pView->ResetCoords();
		m_pView->m_nColor = -1;
		m_pView->GetParent()->SetWindowText(_T("Ygj - Opponent's turn"));
	}
	// partner refused the connection request
	else if(buf[0] == DECLINE)
	{
		AfxMessageBox(_T("Your partner cannot play with you right now!"));
		m_pView->GetParent()->SetWindowText(_T("Ygj - Ready"));
		m_pView->DestroyClientSocket();
	}
	// partner surrender to you
	else if(buf[0] == SURRENDER)
	{
		m_pView->m_bMyTurn = FALSE;
		m_pView->m_bInGame = FALSE;
		m_pView->GetParent()->SetWindowText(_T("Ygj - Game over"));
		AfxMessageBox(_T("Your partner surrendered to you.\n"
						"You won this game!"));
	}
	// other messages
	else
	{
		m_pView->OneStep(buf[1], buf[2]);
		// even game and game over
		if(buf[0] == EVEN)
		{
			m_pView->m_bMyTurn = FALSE;
			m_pView->m_bInGame = FALSE;
			m_pView->GetParent()->SetWindowText(_T("Ygj - Game over"));
			AfxMessageBox(_T("Even game!"));
		}
		// I lost this game
		else if(buf[0] == ULOSE)
		{
			m_pView->m_bMyTurn = FALSE;
			m_pView->m_bInGame = FALSE;
			m_pView->GetParent()->SetWindowText(_T("Ygj - Game over"));
			AfxMessageBox(_T("Sorry!\n"
							"You lost this game."));
		}
		// I won this game
		else if(buf[0] == UWIN)
		{
			m_pView->m_bMyTurn = FALSE;
			m_pView->m_bInGame = FALSE;
			m_pView->GetParent()->SetWindowText(_T("Ygj - Game over"));
			AfxMessageBox(_T("Congratulations!\n"
							"You won this game."));
		}
		// opponent won temporarily, waiting for my last step
		else if(buf[0] == IWIN)
		{
			m_pView->m_bMyTurn = TRUE;
			m_pView->m_bOpponentWin = TRUE;
			m_pView->GetParent()->SetWindowText(_T("Ygj - Your turn"));
		}
		// just an ordinary step
		else if(buf[0] == COORDS)
		{
			m_pView->m_bMyTurn = TRUE;
			m_pView->GetParent()->SetWindowText(_T("Ygj - Your turn"));
		}
	}
	CSocket::OnReceive(nErrorCode);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?