private:
char m_arGame[82];
public:
enum {eLoadGame, eSaveGame};
char* GetGame() { return m_arGame; };
BOOL CSudokuDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
return TRUE;
}
if (!CDocument::OnNewDocument())
return FALSE;
strcpy_s(m_arGame, sizeof(m_arGame), "000000000000000000000000000000000000000000000000000000000000000000000000000000000");
strcpy_s(m_arGame, sizeof(m_arGame), "073200000090600042001000708000000065000050000810000000104000500260003090000004210");
UpdateAllViews(NULL, eLoadGame, NULL);
return TRUE;
void CSudokuView::OnUpdate(CView* pSender, LPARAM lHint, CObject* /*pHint*/)
{
if(!m_bInitialised)
return;
char* pch = GetDocument()->GetGame();
switch(lHint)
{
case CSudokuDoc::eLoadGame:
//Fill the grid contents
for(int row = 0; row < 9; row++)
{
for(int col = 0; col < 9; col++)
{
m_arWndButtons[row * 9 + col].SetValue(*pch - '0');
m_arWndButtons[row * 9 + col].SetLock();
pch++;
}
}
Invalidate();
break;
case CSudokuDoc::eSaveGame:
//Update the doc 'game' string with the current grid contents
for(int row = 0; row < 9; row++)
{
for(int col = 0; col < 9; col++)
{
*pch = m_arWndButtons[row * 9 + col].GetValue() + '0';
pch++;
}
}
break;
default:
ASSERT(FALSE);
break;
}
}
CButton m_arWndButtons[81];
CGridButton m_arWndButtons[81];
#pragma once
#include "GridButton.h"
class CGridButton : public CButton
{
DECLARE_DYNAMIC(CGridButton)
public:
CGridButton();
virtual ~CGridButton();
protected:
DECLARE_MESSAGE_MAP()
};
class CGridButton : public CButton
{
DECLARE_DYNAMIC(CGridButton)
public:
CGridButton();
virtual ~CGridButton();
protected:
DECLARE_MESSAGE_MAP()
private:
bool m_bLock;
int m_iValue; //temporary
public:
void SetValue(int i);
int GetValue() { return m_iValue; };
void SetLock() { m_bLock = (GetValue() != 0); }; //Lock only if no value entered
};
void CGridButton::SetValue(int i)
{
m_iValue = i;
if(GetValue() > 0)
{
CString s((char)(GetValue() + '0'));
SetWindowText(s);
}
else
SetWindowText("");
}
CGridButton::CGridButton()
: m_iValue(0)
, m_bLock(false)
void CSudokuDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
}
void CSudokuDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
UpdateAllViews(NULL, eSaveGame, NULL);
COleDateTime dte = COleDateTime::GetCurrentTime();
m_szInfo = dte.Format("%d.%m.%y %H:%M");
CString szGame(m_arGame);
ar << m_szInfo << szGame;
}
else
{
CString szGame;
ar >> m_szInfo >> szGame;
strcpy_s(m_arGame, sizeof(m_arGame), szGame);
UpdateAllViews(NULL, eLoadGame, NULL);
}
}
private:
CString m_szInfo;
char m_arGame[82];
strcpy_s(m_arGame, sizeof(m_arGame), "000000000000000000000000000000000000000000000000000000000000000000000000000000000");
//strcpy_s(m_arGame, sizeof(m_arGame), "073200000090600042001000708000000065000050000810000000104000500260003090000004210");
m_szInfo.Empty();
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)