Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Make a data structure to store the game board

Instructions

  1. 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.
  2. 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.
  3. 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, make a variable to represent the game board with a data type of std::vector>.
  4. Make a method in the main window header file with a return type of void that will initialize the grid.
  5. 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.
    1. 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.
    2. In order to change the grid size that is stored in the drawing panel, add a set grid size method to the drawing panel.
  6. Make a variable in the main window header file to store the grid size.  Again, this can be defaulted to 15.
  7. The game board vector can now be resized inside the grid initialization method.
    1. 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.
    2. Once completed, there should be a vector with 15 vectors, each containing 15 bools.
  8. 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.
  9. Finally, the grid initialization method needs to be called at the end of the main window constructor.

 

class DrawingPanel : public wxPanel { public:  DrawingPanel(wxFrame* parent, std::vector>& gameBoard);  ~DrawingPanel();

void CalculateCellSize(); void OnPaint(wxPaintEvent& event); wxPoint _point; void OnMouseUp(wxMouseEvent& event); private: int gridSize = 15; // Stores the size of the grid int cellWidth; int cellHeight;

std::vector>& _gameBoard; }; DrawingPanel::DrawingPanel(wxFrame* parent, std::vector>& gameBoard) : wxPanel(parent, wxID_ANY, wxPoint(0, 0), wxSize(200, 200)), _gameBoard(gameBoard) { this->SetBackgroundStyle(wxBG_STYLE_PAINT);

this->Bind(wxEVT_PAINT, &DrawingPanel::OnPaint, this); _point.x = 100; _point.y = 100;

this->Bind(wxEVT_LEFT_UP, &DrawingPanel::OnMouseUp, this); CalculateCellSize(); } DrawingPanel::~DrawingPanel() { }

void DrawingPanel::CalculateCellSize() { wxSize panelSize = GetParent()->GetClientSize(); cellWidth = panelSize.GetWidth() / gridSize; cellHeight = panelSize.GetHeight() / gridSize; }

void DrawingPanel::OnPaint(wxPaintEvent& event) { wxAutoBufferedPaintDC dc(this); dc.Clear();

wxGraphicsContext* context = wxGraphicsContext::Create(dc); if (!context) { return; }

context->SetPen(*wxBLACK); context->SetBrush(*wxWHITE);

for (int row = 0; row < gridSize; row++) { for (int col = 0; col < gridSize; col++) { int x = col * cellWidth; int y = row * cellHeight; context->DrawRectangle(x, y, cellWidth, cellHeight); } }

}

void DrawingPanel::OnMouseUp(wxMouseEvent& event) { _point.x = event.GetX(); _point.y = event.GetY();

Refresh(); }

class MainWindow : public wxFrame { private:

DrawingPanel* _drawingPanel = nullptr; wxBoxSizer* _sizer = nullptr;

std::vector> _gameBoard; public: MainWindow(); ~MainWindow(); void OnSizeChanged(wxSizeEvent& event); wxDECLARE_EVENT_TABLE(); }; MainWindow::MainWindow() :wxFrame(nullptr, wxID_ANY, "Game of Life", wxPoint(0, 0), wxSize(200, 200)) { _drawingPanel = new DrawingPanel(this, _gameBoard);

_sizer = new wxBoxSizer(wxVERTICAL);

_sizer->Add(_drawingPanel, 1, wxEXPAND | wxALL);

Bind(wxEVT_SIZE, &MainWindow::OnSizeChanged, this);

SetSizer(_sizer); } MainWindow::~MainWindow() {

} void MainWindow::OnSizeChanged(wxSizeEvent& event) { _drawingPanel->SetSize(event.GetSize()); _drawingPanel->Refresh(); }





Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Income Tax Fundamentals 2013

Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill

31st Edition

1111972516, 978-1285586618, 1285586611, 978-1285613109, 978-1111972516

More Books

Students also viewed these Algorithms questions

Question

What is the persons job (e.g., professor, student, clinician)?

Answered: 1 week ago