Click here to Skip to main content
15,883,883 members
Articles / Artificial Intelligence

Nura Othello - A WTL Based Board Game

Rate me:
Please Sign up or sign in to vote.
4.40/5 (14 votes)
6 Feb 2007CPOL23 min read 79.3K   2.3K   29  
An example of using the WTL library in an artificial intelligence game.
///////////////////////////////////////////////////////////////////////////////
// OthelloWindowMFC.h Ver. 1.7
// Programmer: PARK Youngho
///////////////////////////////////////////////////////////////////////////////

#pragma once

#include <Mmsystem.h>
#include <afxmt.h>
#include "../OthelloCore/Othello.h"

#pragma comment (lib, "Winmm.lib")

#ifdef	OTHELLO_NOT_THREAD
	#define INITIALIZECS(x)
	#define DELETECS(x)
	#define LOCKCS(x)
	#define UNLOCKCS(x)
#elif	defined(USING_MFC_SYNC)
	#define INITIALIZECS(x)		(x = new CSingleLock(new CCriticalSection))
	#define DELETECS(x)			(delete x)
	#define LOCKCS(x)			x->Lock();	if (x->IsLocked())	{
	#define UNLOCKCS(x)			x->Unlock(); }
#else
	#define INITIALIZECS(x)		::InitializeCriticalSection(&x)
	#define DELETECS(x)			::DeleteCriticalSection(&x)
	#define LOCKCS(x)			::EnterCriticalSection(&x)
	#define UNLOCKCS(x)			::LeaveCriticalSection(&x)
#endif


// OthelloWindow Class name Definition
#define OTHELLO_CLASSA		"Othello"
#define OTHELLO_CLASSW		L"Othello"

#ifdef _UNICODE
	#define OTHELLO_CLASS		OTHELLO_CLASSW
#else
	#define OTHELLO_CLASS		OTHELLO_CLASSA
#endif


// Macro functions for NM_SELF and NM_OTHELLO
#define GETSCORE1(x)	LOWORD(x)
#define GETSCORE2(x)	HIWORD(x)


// OthelloWindow Constants
#define OTHELLO_STONEWIDTH              50
#define OTHELLO_BOARDWIDTH              400


// OthelloWindow Message Definition
#define NM_OTHELLO			(WM_APP + 1)

#define ON_START			1
#define ON_STOP				2
#define ON_LEVEL			3
#define ON_FINISH			4
#define ON_MYSTONE			5
#define ON_YOURSTONE		6
#define ON_PASS				7
#define ON_NOPLACE			8
#define OTHELLO_USER		0x100


// OthelloWindow Style Definition
#define NS_HOVERING			1
#define NS_OTHELLO			(WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS)
#define NS_OTHELLO2			(NS_OTHELLO | WS_BORDER)
#define NS_OTHELLO3			(NS_OTHELLO2 | NS_HOVERING)


class OthelloWindowMFC : public CWnd
{
	OthelloCore*	m_pOthello;
	char			m_Board[OTHELLO_MARGIN][OTHELLO_MARGIN];
	char			m_oldBoard[OTHELLO_MARGIN][OTHELLO_MARGIN];
	UINT			m_Message;
	CPoint			m_Pos, m_OldPos;
	bool			m_bGoing;		// Start�� Stop�� �����ϱ� ����
	bool			m_bProc;		// �����߿� Start�� Stop�� ���ϰ� �ϱ� ����
	char			m_FirstPlayer;
	bool			m_bAuto;	// ���� ���� �޽����� �� ���ΰ�?
	LPCTSTR			m_strWin, m_strLose, m_strDraw, m_strError;
	LPCTSTR			m_strPass, m_strWait, m_strNoPlace;

	CPoint			m_Aspect;			// ���� ���� ũ��
	CPoint			m_StoneWidth;

	CBitmap			m_BufferBM;			// ���� ���۸�
	CBitmap			m_BoardBM;			// ������ ������
	CBitmap			m_StoneBM[2][2];	// ������ ��
	CBitmap			m_LastStoneBM;		// ���������� �� ���� ǥ��

	CBitmap*		m_pOldBufferBM;
	CBitmap*		m_pOldBoardBM;
	CBitmap*		m_pOldStoneBM[2][2];
	CBitmap*		m_pOldLastStoneBM;

	CDC				m_BufferDC;
	CDC				m_BoardDC;
	CDC				m_StoneDC[2][2];
	CDC				m_LastStoneDC;

	LPCTSTR			m_StartSND;					// ������ �� ���� �Ҹ�
	LPCTSTR			m_StoneSND1, m_StoneSND2;	// ���� ���� �� ���� �Ҹ�
	LPCTSTR			m_PopSND;					// ������ �� ���� �Ҹ�
	LPCTSTR			m_CancelSND;				// ������ ����� �� ���� �Ҹ�

	HINSTANCE		m_hInst;

#ifdef OTHELLO_NOT_THREAD
#elif defined(USING_MFC_SYNC)
	CSingleLock*		cs;						// ����ȭ
#else
	CRITICAL_SECTION	cs;						// ����ȭ
#endif
	DECLARE_DYNAMIC(OthelloWindowMFC)

public:
	OthelloWindowMFC (UINT message = NM_OTHELLO, bool bAuto = true);
	OthelloWindowMFC (UINT message, int lang, bool bAuto);
	virtual ~OthelloWindowMFC ();

private:
	void InitVars (void);
	void InitSound (void);
	bool InitGraphics (void);
	void TermGraphics (void);

	void InitStrings (int lang);
	void InitStringKorean (void);
	void InitStringEnglish (void);

	void DoubleBuffering (void);
	void OnScreen (void);
	void Update (void);

	void Thread (void);
	bool PutFirst (void);			// ���� �δ� �Լ�

	void CommonConstructor (UINT message, int lang, bool bAuto);
	bool StartStop (bool bStart);

	void OnPass (LPARAM lParam);
	void OnFinish (LPARAM lParam);
	void OnNoPlace (LPARAM lParam);

protected:
	virtual void MyStone (void);

public:
	inline bool Start (void)	{ return StartStop (true); }
	inline bool Stop (void)		{ return StartStop (false);	}
	inline void Initialize ()
		{	Initialize(rand() % 8);	}
	inline void Initialize (const char FirstPlayer, int IQ = OTHELLO_SMART)
		{	Initialize(rand() % 8, FirstPlayer, IQ);	}

	void Initialize (int diag, const char firstPlayer = PLAYER2,
								int IQ = OTHELLO_SMART);

	bool Withdraw (void);					// ������ �Լ�
	int WhoWin (int* pPlayer1 = 0, int* pPlayer2 = 0) const;
	int WhoseTurn (void) const;
	int GetStep (void) const;

	// The reason why there is no function Create (..., const RECT* pRect, ...)
	// or Create (..., const POINT* pPt, ...) is to prevent a null pointer parameter.
	BOOL Create (CWnd* pParentWnd, int x, int y, UINT nID);
	BOOL Create (CWnd* pParentWnd, const POINT& pt, UINT nID);
	BOOL Create (CWnd* pParentWnd, int x, int y, int nWidth, int nHeight, UINT nID);
	BOOL Create (CWnd* pParentWnd, const RECT& rect, UINT nID);
	BOOL Create (CWnd* pParentWnd, int x, int y,
					DWORD dwStyle, DWORD dwExStyle, UINT nID);
	BOOL Create (CWnd* pParentWnd, const POINT& pt,
					DWORD dwStyle, DWORD dwExStyle, UINT nID);
	BOOL Create (CWnd* pParentWnd, int x, int y, int nWidth, int nHeight,
					DWORD dwStyle, DWORD dwExStyle, UINT nID);
	BOOL Create (CWnd* pParentWnd, const RECT& rect,
					DWORD dwStyle, DWORD dwExStyle, UINT nID);

	// The reason why there is no function Move (..., const RECT* pRect, ...)
	// or Move (..., const POINT* pPt, ...) is to prevent a null pointer parameter.
	void Move (int x, int y, BOOL bRepaint = TRUE);
	void Move (POINT& pt, BOOL bRepaint = TRUE);
	void Move (int x, int y, int nWidth, int nHeight, BOOL bRepaint = TRUE);
	void Move (RECT& rect, BOOL bRepaint = TRUE);

protected:
	DECLARE_MESSAGE_MAP()

public:
	afx_msg int OnCreate (LPCREATESTRUCT lpCreateStruct);
	afx_msg void OnDestroy();
	afx_msg void OnSize (UINT nType, int cx, int cy);
	afx_msg void OnPaint ();
	afx_msg void OnLButtonDown (UINT nFlags, CPoint point);
	afx_msg void OnTimer(UINT nIDEvent);
	afx_msg LRESULT OnSelf (WPARAM wParam, LPARAM lParam);

	friend static DWORD WINAPI OthelloThread (LPVOID lpParameter);
};

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Korea (Republic of) Korea (Republic of)
I like programming.
I am teaching at AUCA (American University of Central Asia) now.

Comments and Discussions