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.
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..
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!
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.
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!
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.