Matrix's, MFC and Member functions
Mmmm, a tasty combination.
First off, I picked up the book 'Visual C++ Bible' and i've read up to about page 200. I wanted to make a few test programs to test what I'd learned so I decided on good old TicTacToe and Connect Four :)
TicTacToe I finished in about 30 minutes.. while connect four is giving me a small problem involving a 2D array of variable length.
The user enters 2 values, height and width of the board and then clicks new game. Then, I divide the client area up based on these values, and initalize a matrix also based on these values.
Everything works perfectly.. exept when I click new game, it crashes :(
Here's the code for member function OnNewGame();
Code:
void CConnectFourDlg::OnNewgame()
{
m_iGameStarted = 1;
*m_connectFourArray = new short int[m_iWidth];
for (int i = 0; i < m_iWidth; i++)
{
m_connectFourArray[i] = new short int[m_iHeight];
}
}
and here's the class declaration
Code:
class CConnectFourDlg : public CDialog
{
// Construction
public:
bool m_iGameStarted;
short int **m_connectFourArray;
CConnectFourDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(CConnectFourDlg)
enum { IDD = IDD_CONNECTFOUR_DIALOG };
short m_iHeight;
short m_iWidth;
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CConnectFourDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
//{{AFX_MSG(CConnectFourDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnNewgame();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};