Matrix's, MFC and Member functions

This is a discussion on Matrix's, MFC and Member functions within the Windows Programming forums, part of the Platform Specific Boards category; Mmmm, a tasty combination. First off, I picked up the book 'Visual C++ Bible' and i've read up to about ...

  1. #1
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802

    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()
    };

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    Ok...your logic seems ok for creating a matrix dynamically, but I think your implementation is wrong....


    Code:
    short int **m_connectFourArray;
    
    //.................
    
    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];
    }
    I think that should work...I havent tested it though.....

    Dont forget to call both delete [] and delete to clean this array out of memory

  3. #3
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Ahh, you're right again Fordy. Thanks a bunch.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    223
    While we are on the subject of board games, I created a game puzzle game. The kind with the sliding boards where there is always one empty slot and the boards can slide horizontally and vertically but only one board at a time.
    I was motivated to write this game after seen it on a cell phone.
    Anyway, I think that there is a bug in their code.
    Sometimes the puzzle results in what I consider unsolvable.

    Here is the problem.

    Sometimes just when you think that you are about to solve the game it ends up in what I think is unsolvable..

    My game works fine because I stopped doing the random shuffle and instead I used preconfigured mix-ups that I read from a file.

    Can someone tell me if this solvable...
    I will post my game here later to see if anyone can solve this or if indeed it is a bug. (like tomorrow when I have access to it or later this evening)

    Code:
    | 01 | 02 | 03 | 04 |
    | 05 | 06 | 07 | 08 |
    | 09 | 10 | 11 | 12 |
    | 13 | 15 | 14 |    |
    zMan

  5. #5
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    I don't know... it seems to me any combonation would be solvable. Same with rubics cubes, they may seem impossible to get back to same-colors on each side, but it's obviously possible.

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    25

    jigsaw

    Incorrect not all rubix cubes problems are solvable. Not if the rubix cube is taken apart at least. An inproper algorithm can result in this. I believe that the same goes for this 2 dimensional puzzle. Anyway.. here is the executable. Try it and see for your self.

    Thanks in advance for any help.


    Make sure to extract the files .exe and the .pzl. Then load the game named unsolvable.

    http://www.vvm.com/~fzamora/jigsaw.zip
    inZane
    --true programmer's don't comment--
    --programmer wannabes complain about it--

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MFC with .NET
    By dead_cell in forum C++ Programming
    Replies: 2
    Last Post: 08-20-2002, 12:02 AM
  2. MFC Assertion Failure
    By maxthecat in forum Windows Programming
    Replies: 5
    Last Post: 08-01-2002, 09:58 AM
  3. Modifying CDocument Member Objects :: MFC
    By kuphryn in forum C++ Programming
    Replies: 0
    Last Post: 04-12-2002, 05:52 PM
  4. Accessing CEdit Member Functions Inside CEditView ::MFC
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 04-10-2002, 08:52 PM
  5. MFC is Challenging :: C++
    By kuphryn in forum C++ Programming
    Replies: 8
    Last Post: 02-05-2002, 12:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21