Question: Objective Create a data structure to store the game board Instructions There should now be a grid of squares, but the code is not able

Objective
Create a data structure to store the game board
Instructions
There should now be a grid of squares, but the code is not able to do anything with the grid. Now is time to add some functionality.
Each box is going to have two possible states. It's either on or off. This can best be represented by a bool, where false meaning the box is dead and true meaning that the box is alive.
A data structure will be needed that is able to store data for both rows and columns. This could be an array of bool arrays or a vector of bool vectors. A vector of vectors will be the easiest to work with. In the main window header file, create a variable to represent the game board with a data type of std::vector>.
Create a method in the main window header file with a return type of void that will initialize the grid.
Inside of the grid initialization method, set the size of the game board vector to the size of the grid. This can be done by calling the resize method on the vector and passing the grid size variable in as an argument.
There is a problem with process though. The grid size is currently stored inside of the drawing panel. In order for the game to work, the grid size variable will need to be moved up to the main window. This will be beneficial when the grid size becomes configurable later on in the project.
In order to change the grid size that is stored in the drawing panel, add a set grid size method to the drawing panel.
Create a variable in the main window header file to store the grid size. Again, this can be defaulted to 15.
The game board vector can now be resized inside the grid initialization method.
It's not enough to just resize the game board vector, because the game board is a vector of vectors. Each vector inside of the game board must also be resized to the grid size. This can be done by looping through the game board vector and calling resize on each sub-vector.
Once completed, there should be a vector with 15 vectors, each containing 15 bools.
The grid initialization method needs to pass the grid size from the main window along to the drawing panel by call the grid size setter that was created in step 5b above.
Finally, the grid initialization method needs to be called at the end of the main window constructor.
Give the drawing panel access to the game board and provide mouse interaction with the drawing panel.
Instructions
Now that there is a place to store whether a cell is dead or alive, the drawing panel needs access to the game board.
Add a std::vector>& to the drawing panel header file that will be used to store a reference to the game board stored in the main window.
It will be necessary to use constructor initialization in order to set the reference value. The game board should be passed along as an argument in the drawing panel constructor.
Mouse Interactions
it's time to add the ability to interact with the drawing panel with the mouse.
Start by creating new event handler method in the drawing panel header file that takes in a wxMouseEvent& variable as a parameter. This event handler will be called after a mouse button is clicked.
In the constructor for the drawing panel, call bind on the wxEVT_LEFT_UP event using something similar to this this->Bind(wxEVT_LEFT_UP, &DrawingPanel::OnMouseUp, this);
When the mouse event handler method is implemented, the wxMouseEvent arguments gives two important pieces of information. It has a method for GetX() and GetY(), which allows us to know the coordinates of where the click happened. With a little math, it can be figured out which box was clicked.
Inside the handler method, call the GetX() and GetY() methods on the event argument and store them into separate variables.
Next the cell width and the cell height is needed. This can be calculated in the same way that it is within the on paint handler using GetSize() and the grid size.
The row and column that was clicked can be calculated by dividing the x coordinate of the click by the cell width and the y coordinate of the click by the cell height.
Store each of those in a variable so that they can be used to access the game board and change the value of that cell.
Using the square bracket notation to access indices of the vector, use the values that were just calculated to find the correct item in the game board. If the value is true, it should become false. If it is false, it should become true.
Since the game board is a vector of vectors, it will have two sets of square brackets. ex: variable[x][y].
Finally, call Refresh(); so that the onPaint event will fire.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!