Storing button pointers on wxWidget?

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
monjinko
In need of some credit
In need of some credit
Posts: 5
Joined: Sun Nov 12, 2017 12:50 am

Storing button pointers on wxWidget?

Post by monjinko »

I currently am trying to make a simple Tic Tac Toe on wxWidget but I can't seem to figure out how make an array of button pointers on wxWidget. When a button is clicked, I want to first make sure that the button does not already contain a label (O or X) and then store that location to an array that holds pointers. Here is the code so far..

Code: Select all

void SubGUIClass::OnGameClick(wxCommandEvent& event)
{
    // TODO: Implement OnGameClick
    wxButton *b1 = (wxButton *)event.GetEventObject();
    wxButton *b2 [3][3]; 

    int i = 0;
    int j = 0;

    while (b1->GetLabel() != "X" || b1->GetLabel() != "O")
    {
        b2[i][j] = b1; 

        i++; 
        j++; 
    }

}
I was hoping to use the 2d array to deal with the logic of the Tic Tac Toe game, but I'm having trouble storing the button events into a 2d array. Any help would be hugely appreciated!
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Storing button pointers on wxWidget?

Post by doublemax »

The array that holds the button pointers should be a member variable, e.g. of SubGUIClass. You need to initialize it while you create the buttons, so that you know the position of each button.
Use the source, Luke!
monjinko
In need of some credit
In need of some credit
Posts: 5
Joined: Sun Nov 12, 2017 12:50 am

Re: Storing button pointers on wxWidget?

Post by monjinko »

How would you initialize the array when the Button is created and why would that let me know the position of the button?
Thank you!
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Storing button pointers on wxWidget?

Post by doublemax »

How do you create the buttons? By code or with a GUI editor?

If you do it by code, please show the code.

If the latter case you may need another way to mark the buttons, e.g. with a unique ID that tells you which button is where.
Use the source, Luke!
monjinko
In need of some credit
In need of some credit
Posts: 5
Joined: Sun Nov 12, 2017 12:50 am

Re: Storing button pointers on wxWidget?

Post by monjinko »

Hi thanks for the help.
Here is the code. I excluded other game logic checks (rows, columns, diagonal, etc..) for simplicity.
I get a memory allocation error on VS saying, " Exception thrown: read access violation. "
Thanks again for the help!

Code: Select all

#include "SubGUIClass.h"

SubGUIClass::SubGUIClass( wxWindow* parent )
:
GUIClass( parent )
{
	b2[0][0] = g00;  b2[0][1] = g01;  b2[0][2] = g02;
	b2[1][0] = g10;  b2[1][1] = g11;  b2[1][2] = g12;
	b2[2][0] = g20;  b2[2][1] = g21;  b2[2][2] = g22;

}

void SubGUIClass::OnGameClick(wxCommandEvent& event)
{
	// TODO: Implement OnGameClick
	wxButton *b1 = (wxButton *)event.GetEventObject();
	int player1 = 1;
	int player2 = 2;

	if (b1->GetLabel() == "") {

		switch (playerTurn) {
		case 'X':
			b1->SetLabel("X");
			if (gameWin(player1) == true) {
				wxMessageBox(wxT("X has won!!"), wxT("Tic Tac Toe"), wxYES_NO, this);
			}
			else {
				playerTurn = 'O';
				m_statusBar2->SetStatusText(wxT("O's Turn"), 0);
			}
			break;
		case 'O':
			b1->SetLabel("O");

			if (gameWin(player1) == true) {
				wxMessageBox(wxT("O has won!!"), wxT("Tic Tac Toe"), wxYES_NO, this);
			}
			else {
				playerTurn = 'X';
				m_statusBar2->SetStatusText(wxT("X's Turn"), 0);
			}
			break;
		}
	}

}

bool SubGUIClass::gameWin(int p) {


	wxString temp;
	int col = 0;
	int row = 0;
	int loopchk = 0;

	if (p == 1) {
		temp = "X";
	}

	else if (p == 2) {
		temp = "O";
	}

	for (col = 0; col < 3; col++) {
		for (row = 0; row < 3; row++) {

			while (b2[row][col]->GetLabel() == temp) {

				col++;
				loopchk++;
				if (loopchk == 3) {
					return true;
				}

			}

			loopchk = 0;
		}

	}
}
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Storing button pointers on wxWidget?

Post by doublemax »

Use the debugger to find out where and why it crashes. My guess would be the while loop inside the for loop and "col" running out of bounds.

For simplicity i would suggest to split the "gameWin" check into 4 loops: One checks for horizontal lines, one for vertical lines and 2 for the two diagonals. That will make the code easier to understand.
Use the source, Luke!
coderrc
Earned some good credits
Earned some good credits
Posts: 141
Joined: Tue Nov 01, 2016 2:46 pm

Re: Storing button pointers on wxWidget?

Post by coderrc »

Code: Select all

 while (b2[row][col]->GetLabel() == temp) {

            col++;
Let col=2 and b2[row][col]->GetLabel() == temp
col++
b2[row][col]->GetLabel()

womp womp
Post Reply