Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help finishing my code im stuck. Objective in C++ Add an option to display the number of living neighbors to each cell Instructions

I need help finishing my code im stuck.

Objective in C++

Add an option to display the number of living neighbors to each cell

Instructions

  1. The Show Neighbor Count option will display the number of living neighbors for each cell, as long as there is at least one living neighbor.  You can choose
  2. Add an option to the Settings object for Show Neighbor Count so that the value will be saved and loaded when the program is closed and re-opened.
  3. Add a checked menu item option to the View menu for Show Neighbor Count and have it update the Setting object.
     
  4. Make a std::vector> to store the neighbor count for each cell.  The value of each cell will be updated when neighbor count is called within the next generation logic.  The collection of neighbor counts will need to be passed to the drawing panel for use in the OnPaint logic.
  5. Use the following steps in order to display text on the drawing panel.
    1. Set the font for the drawing context using SetFont.  SetFont takes in a wxFontInfo object, which requires a size as an argument.  SetFont also takes in a color.  In order to set the font to size 16 and red, call SetFont(wxFontInfo(16), *wxRED)
    2. Make a wxString that will store the text to be displayed.
    3. To help with positioning the text, the drawing context's GetTextExtent, which takes in a wxString (the text to be displayed) and two double references.  The two double variables (X and Y) will need to be declared before GetTextExtent is called.
    4. The text can be rendered to the screen by calling the context's DrawText method.  It will take the text to display, an X value, and a Y value.  You may need to use the doubles from the previous step as well as the current rectangle's location and size in order to calculate the coordinates.

 


struct GameSettings
{
   unsigned int livingCellColorRed;
   unsigned int livingCellColorGreen;
   unsigned int livingCellColorBlue;
   unsigned int livingCellColorAlpha;

   unsigned int deadCellColorRed;
   unsigned int deadCellColorGreen;
   unsigned int deadCellColorBlue;
   unsigned int deadCellColorAlpha;

   int gridSize;
   int interval;

   std::vector> neighborCounts;


   // Constructor with default values
   GameSettings()
       : livingCellColorRed(128), livingCellColorGreen(128), livingCellColorBlue(128), livingCellColorAlpha(255),
       deadCellColorRed(255), deadCellColorGreen(255), deadCellColorBlue(255), deadCellColorAlpha(255),
       gridSize(15), interval(50), showNeighborCount(false)
   {
       neighborCounts.resize(gridSize, std::vector(gridSize, 0));
   }


   void setLivingCellColor(const wxColor& color)
   {
       livingCellColorRed = color.Red();
       livingCellColorGreen = color.Green();
       livingCellColorBlue = color.Blue();
       livingCellColorAlpha = color.Alpha();

   }

   void setDeadCellColor(const wxColor& color)
   {
       deadCellColorRed = color.Red();
       deadCellColorGreen = color.Green();
       deadCellColorBlue = color.Blue();
       deadCellColorAlpha = color.Alpha();

   }

   //getters for color
   wxColor GetLivingCellColor()
   {
       wxColor color(livingCellColorRed, livingCellColorGreen, livingCellColorBlue, livingCellColorAlpha);
       return color;
   }

   wxColor GetDeadCellColor()
   {
       wxColor color(deadCellColorRed, deadCellColorGreen, deadCellColorBlue, deadCellColorAlpha);
       return color;
   }


   void SaveSettings()
   {
       std::ofstream file("settings.bin", std::ios::out | std::ios::binary);
       file.write((char*)this, sizeof(GameSettings));
       file.close();
   }

   void LoadSettings()
   {
       std::ifstream file("settings.bin", std::ios::binary | std::ios::in);
       file.read((char*)this, sizeof(GameSettings));
       file.close();
   }

   bool IsNeighborCountShown() const
   {
       return showNeighborCount;
   }

   void SetNeighborCountShown(bool value)
   {
       showNeighborCount = value;
   }
};

 

 

wxBEGIN_EVENT_TABLE(DrawingPanel, wxPanel)
EVT_PAINT(DrawingPanel::OnPaint)
EVT_LEFT_UP(DrawingPanel::OnMouseUp)
wxEND_EVENT_TABLE()

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

gridSize = 30;
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;
  // Checks the corresponding bool in the game board
  if (_gameBoard[row][col])
  {
   context->SetBrush(*wxLIGHT_GREY); // Living cell color
  }
  else
  {
   context->SetBrush(*wxWHITE); // Dead cell color
  }

  context->DrawRectangle(x, y, cellWidth, cellHeight);
 }
}

 

}
void DrawingPanel::SetGridSize(int size)
{
gridSize = size;
CalculateCellSize(); // Recalculates cell size based on the new grid size
}

void DrawingPanel::SetSize(const wxSize& size)
{
// Call SetSize on base wxPanel class
wxPanel::SetSize(size);

// Call Refresh
Refresh();
}

void DrawingPanel::OnMouseUp(wxMouseEvent& event)
{
int mouseX = event.GetX();
int mouseY = event.GetY();

int row = mouseY / cellHeight;
int col = mouseX / cellWidth;

if (row >= 0 && row < gridSize && col >= 0 && col < gridSize)
{
 // Toggles the state of the clicked cell
 _gameBoard[row][col] = !_gameBoard[row][col];
 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_2

Step: 3

blur-text-image_3

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

6. Name three ways a gene could influence alcoholism.

Answered: 1 week ago