Click here to Skip to main content
15,896,557 members
Articles / Desktop Programming / MFC

Network Renju Game

Rate me:
Please Sign up or sign in to vote.
3.00/5 (2 votes)
1 Jul 20032 min read 74K   3.9K   41  
This is a Renju game with network support.
// ClientSocket.cpp : implementation file
//

#include "stdafx.h"
#include "Renju.h"
#include "RenjuDoc.h"
#include "RenjuView.h"
#include "ClientSocket.h"

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

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

CClientSocket::CClientSocket(CRenjuView* 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("Renju - 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->DestroyClientSocket();
		m_pView->ResetCoords();
		m_pView->GetParent()->SetWindowText(_T("Renju - Ready"));
		AfxMessageBox(_T("Your partner disconnected!"));
	}
	// 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->ResetCoords();
		m_pView->m_nColor = -1;
		m_pView->GetParent()->SetWindowText(_T("Renju - 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("Renju - 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("Renju - 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("Renju - 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("Renju - 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("Renju - 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("Renju - Your turn"));
		}
		// just an ordinary step
		else if(buf[0] == COORDS)
		{
			m_pView->m_bMyTurn = TRUE;
			m_pView->GetParent()->SetWindowText(_T("Renju - Your turn"));
		}
	}
	
	CSocket::OnReceive(nErrorCode);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Hey you,
Out there in the cold,
Getting lonely, getting old,
Can you feel me?
Hey you,
Standing in the aisle,
With itchy feet and fading smile,
Can you feel me?
Hey you,
Don't help them to bury the light.
Don't give in without a fight.

Comments and Discussions