Click here to Skip to main content
15,886,689 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.
// MainFrm.cpp : implmentation of the CMainFrame class
//
/////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "resource.h"

#include "aboutdlg.h"
#include "OthelloCustomizerView.h"
#include "MainFrm.h"

CMainFrame::CMainFrame (void)
{
	m_strTitle = _T("Nura Othello Customizer") _T(OTHELLOCUSTOMIZER_VERSION);
}

CMainFrame::~CMainFrame (void)
{
}

BOOL CMainFrame::PreTranslateMessage(MSG* pMsg)
{
	if(CFrameWindowImpl<CMainFrame>::PreTranslateMessage(pMsg))
		return TRUE;

	return m_view.PreTranslateMessage(pMsg);
}

BOOL CMainFrame::OnIdle()
{
	UIUpdateToolBar();

	CString		str;
	str		= m_view.GetCurDir();
	str		+= m_view.GetCurFile();

	::SendMessage(m_hWndStatusBar, SB_SETTEXT, 0, (LPARAM) (LPCTSTR) str);
	return FALSE;
}

LRESULT CMainFrame::OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/)
{
	CString		str;
	str = m_strTitle;
	str += _T(" - NuraOthello.ini");
	SetWindowText(str);

	// create command bar window
	HWND hWndCmdBar = m_CmdBar.Create(m_hWnd, rcDefault, NULL, ATL_SIMPLE_CMDBAR_PANE_STYLE);
	// attach menu
	m_CmdBar.AttachMenu(GetMenu());
	// load command bar images
	m_CmdBar.LoadImages(IDR_MAINFRAME);
	// remove old menu
	SetMenu(NULL);

	HWND hWndToolBar = CreateSimpleToolBarCtrl(m_hWnd, IDR_MAINFRAME, FALSE, ATL_SIMPLE_TOOLBAR_PANE_STYLE);

	CreateSimpleReBar(ATL_SIMPLE_REBAR_NOBORDER_STYLE);
	AddSimpleReBarBand(hWndCmdBar);
	AddSimpleReBarBand(hWndToolBar, NULL, TRUE);

	CreateSimpleStatusBar();

	m_hWndClient = m_view.Create(m_hWnd);

	UIAddToolBar(hWndToolBar);
	UISetCheck(ID_VIEW_TOOLBAR, 1);
	UISetCheck(ID_VIEW_STATUS_BAR, 1);

	// register object for message filtering and idle updates
	CMessageLoop* pLoop = _Module.GetMessageLoop();
	ATLASSERT(pLoop != NULL);
	pLoop->AddMessageFilter(this);
	pLoop->AddIdleHandler(this);

	CRect	rt(0, 0, HMAXRANGE + XSTART, VMAXRANGE + YSTART);

	AdjustWindowRect(rt);
	rt.MoveToY(0);
	rt.bottom = min(rt.bottom, 600);
	MoveWindow(rt);
	CenterWindow();

	return 0;
}

LRESULT CMainFrame::OnFileExit(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/)
{
	PostMessage(WM_CLOSE);
	return 0;
}

LRESULT CMainFrame::OnFileNew(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/)
{
	m_view.New();
	CString		str;
	str = m_strTitle;
	str += _T(" - NuraOthello.ini");
	SetWindowText(str);

	return 0;
}

LRESULT CMainFrame::OnViewToolBar(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/)
{
	static BOOL bVisible = TRUE;	// initially visible
	bVisible = !bVisible;
	CReBarCtrl rebar = m_hWndToolBar;
	int nBandIndex = rebar.IdToIndex(ATL_IDW_BAND_FIRST + 1);	// toolbar is 2nd added band
	rebar.ShowBand(nBandIndex, bVisible);
	UISetCheck(ID_VIEW_TOOLBAR, bVisible);
	UpdateLayout();
	return 0;
}

LRESULT CMainFrame::OnViewStatusBar(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/)
{
	BOOL bVisible = !::IsWindowVisible(m_hWndStatusBar);
	::ShowWindow(m_hWndStatusBar, bVisible ? SW_SHOWNOACTIVATE : SW_HIDE);
	UISetCheck(ID_VIEW_STATUS_BAR, bVisible);
	UpdateLayout();
	return 0;
}

LRESULT CMainFrame::OnAppAbout(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/)
{
	About();

	return 0;
}

void  CMainFrame::OnFileOpenSaveAs (BOOL bOpen)
{
	CFileDialog			fd(bOpen);
	CString				strFullPath;

	strFullPath	= m_view.GetCurDir();
	strFullPath	+= _T("*.ini");
	ZeroMemory(fd.m_szFileName, sizeof fd.m_szFileName);
	memcpy(fd.m_szFileName, (LPCTSTR) strFullPath, strFullPath.GetLength() * sizeof TCHAR);

	fd.m_ofn.lpstrFilter		= _T("Language Pack File (*.ini)\0*.ini\0");
	fd.m_ofn.nFilterIndex		= 1;
	fd.m_ofn.lpstrInitialDir	= m_view.GetHomeDir();
	fd.m_ofn.lpstrTitle			= _T("Open Nura Othello Language Pack File");

	if (fd.DoModal() == IDOK)
	{
		if (bOpen)
		{
			m_view.Open(fd.m_szFileName);
			CString		str;
			str = m_strTitle;
			str += _T(" - ");
			str += fd.m_szFileTitle;
			SetWindowText(str);
		}
		else
		{
			m_view.SaveAs(fd.m_szFileName);
			CString		str;
			str = m_strTitle;
			str += _T(" - ");
			str += fd.m_szFileTitle;
			SetWindowText(str);
		}
	}
}

LRESULT CMainFrame::OnFileOpen (WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/)
{
	OnFileOpenSaveAs(TRUE);

	return 0;
}

LRESULT CMainFrame::OnFileSave (WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/)
{
	m_view.Save();

	return 0;
}

LRESULT CMainFrame::OnFileSaveAs (WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/)
{
	OnFileOpenSaveAs(FALSE);

	return 0;
}



LRESULT CMainFrame::OnHelp (LPHELPINFO /*lpHelpInfo*/)
{
	About();

	return TRUE;
}

void CMainFrame::About (void)
{
	CAboutDlg dlg;
	dlg.DoModal();
}

LRESULT CMainFrame::OnEditCopy (WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/)
{
	CEdit*	pEdit = m_view.GetEditPtr();
	if (pEdit)
		pEdit->Copy();

	return 0;
}


LRESULT CMainFrame::OnEditCut (WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/)
{
	CEdit*	pEdit = m_view.GetEditPtr();
	if (pEdit)
		pEdit->Cut();

	return 0;
}

LRESULT CMainFrame::OnEditPaste(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/)
{
	CEdit*	pEdit = m_view.GetEditPtr();
	if (pEdit)
		pEdit->Paste();

	return 0;
}

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