Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help fixing this code, I am currently working on the Game of Life and I am having issues with vectors. I keep getting

I need help fixing this code, I am currently working on the Game of Life and I am having issues with vectors. I keep getting a debug assertation and when I continue into debugging it says that there is a vector subscript out of range. I just need help to solve the problem. This is in C++ as well.

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed
class MainWindow : public wxFrame public: Ma inWindow(wxWindow* parent, wxWindowID id = wxID_ANY, const wxString& title = "Game of Life", const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize); ~MainWindow() ; void OnSizeChange(wxSizeEvent& event) ; wxStatusBar* statusBar; void UpdateStatusBar(); int getNeighborCount(int row, int column) ; void nextGeneration() ; void OnTimer(wxTimerEvent& event); wxTimer* timer; private: int interval = 50; //std: : vector> sandbox; void ClearBoard() ; int generationCount; int numRows; int numColumns ; bool cells [10000] [10000] ; wxToolBar* toolbar; void OnPlay (wxCommandEvent& event) ; void OnPause(wxCommandEvent& event) ; void OnNext (wxCommandEvent& event) ; void OnTrash (wxCommandEvent& event) ; wxBoxSizer* boxSizer; DrawingPanel* drawingPanel; std: : vector> gameBoard; int gridsize = 15; void initGameBoard() ; void SetGridSize(int newSize); int generations = 0; int livingCells = 0; WXDECLARE_EVENT_TABLE() ;#include "MainWindow.h" #include #include "play.xpm" #include "pause.xpm" #include "next.xpm" #include "trash.xpm" WXBEGIN_EVENT_TABLE(MainWindow, wxFrame) EVT_SIZE(MainWindow: : OnSizeChange) EVT_MENU(wXID_ANY, MainWindow: : OnPlay) EVT_MENU(wXID_ANY, MainWindow: : OnPause) EVT_MENU(wXID_ANY, MainWindow: : OnNext) EVT_MENU(wXID_ANY, MainWindow: : OnTrash) EVT_TIMER(wXID_ANY, MainWindow: : OnTimer) WXEND_EVENT_TABLE() MainWindow: : MainWindow(wxWindow* parent, wxWindowID id, const wxStrings title, const wxPoint& pos, const wxSize& size) : wxFrame(parent, id, title, pos, size) this->statusBar = this->CreateStatusBar() ; toolbar = CreateToolBar() ; wxBitmap playIcon(play_xpm) ; toolbar->AddTool(wxID_ANY, "Play", playIcon) ; wxBitmap pauseIcon(pause_xpm) ; toolbar->AddTool(wxID_ANY, "Pause", pauseIcon) ; wxBitmap nextIcon(next_xpm) ; toolbar->AddTool(wxID_ANY, "Next", nextIcon) ; wxBitmap trashIcon(trash_xpm) ; toolbar->AddTool(wxID_ANY, "Trash", trashIcon) ; toolbar->Realize() ; timer = new wxTimer(this, wxID_ANY) ; boxSizer = new wxBoxSizer(wxVERTICAL) ; drawingPanel = new DrawingPanel(this, gameBoard) ; boxSizer->Add(drawingPanel, 1, wxEXPAND | WXALL); this->SetSizer(boxSizer) ; //this->Bind(wxFVT_ST7F. &MainWindow: :OnSizeChange. this) ://this->Bind (wxEVT_SIZE, &MainWindow: : OnSizeChange, this) ; initGameBoard() ; this->Layout() ; void MainWindow: : OnSizeChange(wxSizeEvent& event) wxSize windowSize = event . GetSize() ; drawingPanel->SetSize(windowSize) ; event . Skip() ; MainWindow: : ~MainWindow() void MainWindow: : initGameBoard() gameBoard . resize(gridsize) ; for (int i = 0; i SetStatusText(status_text, 0);return count; avoid MainWindow: : nextGeneration() std: : vector> sandbox(gameBoard . size(), std: : vector(gameBoard[0] . size())) ; int LivingCount = 0; for (int i = 0; i 3) sandbox [i] [j] = false; else sandbox [i] [j] = true; LivingCount++; else if (livingNeighbors == 3) sandbox[i] [j] = true; LivingCount++; else sandbox [i] [j] = false;gameBoard . swap(sandbox) ; generationCount++; UpdateStatusBar() ; Refresh() ; Avoid MainWindow: : ClearBoard() - for (int i = 0; i #include #include #include Eclass DrawingPanel : public wxPanel public: DrawingPanel(wxWindow* parent, std: :vector>& board) ; ~DrawingPanel() ; void SetSize(wxSize& windowSize) ; void OnPaint(wxPaintEvent& event) ; void OnMouseUp(wxMouseEvent& event) ; private: std: : vector>& gameBoard; wxGraphicsContext* context; int gridSize; int cellsize; const int numCells = 20; DECLARE_EVENT_TABLE( #endif // !DRAWINGPANEL_H#include "DrawingPanel. h" #include BEGIN_EVENT_TABLE(DrawingPanel, wxPanel) EVT_PAINT (DrawingPanel: : OnPaint) EVT_MOUSE_EVENTS (DrawingPanel: : OnMouseUp) END_EVENT_TABLE( DrawingPanel: : DrawingPanel(wxWindow* parent, std: :vector>& board) : wxPanel(parent), gameBoard(board) this->SetBackgroundStyle(wxBG_STYLE_PAINT); // this->Bind(wxEVT_PAINT, &DrawingPanel: : OnPaint, this) ; gridSize = 15; cellSize = 10; // this->Bind(wxEVT_LEFT_UP, &DrawingPanel: : OnMouseUp, this); DrawingPanel: : ~DrawingPanel() avoid DrawingPanel: : OnPaint(wxPaintEvent& event) wxPaintDC de(this); dc . SetBackground(*wxWHITE_BRUSH) ; dc . Clear(); dc. SetBrush(*wxLIGHT_GREY_BRUSH) ; int cellsize = GetClientSize() . GetWidth() / numCells; for (int i = 0; i subscript out of range For information on how your program can cause an assertion failure, see the Visual C+ + documentation on asserts. (Press Retry to debug the application) Abort Retry Ignore#+ GameOfLife "std:vector [operator(size_type_Off) 3176 if (size() 0 3185 STL_VERIFY(_Off _Mysize, "vector subscript out of range") ; 3186 #endif // _CONTAINER_DEBUG_LEVEL > 0 3187 3188 const_iterator _It = begin(); 3189 _It._Advance(_off) ; 3190 return *_It; 3191 3192 3193 _NODISCARD _CONSTEXPR20 reference operator (size_type _off) noexcept /* strengthened */ { 3194 #if _CONTAINER_DEBUG_LEVEL > 0 3195 STL_VERIFY(_Off _Mysize, "vector subscript out of range"); x 3196 #endif // _CONTAINER_DEBUG_LEVEL > 0 3197 Breakpoint Instruction Executed 3198 iterator _It = begin(); 3199 _It. _Advance(_off) ; A breakpoint instruction (debugbreak() statement or a similar call) 3200 return *_It; was executed in GameOfLife.exe. 3201 Show Call Stack | Copy Details | Start Live Share session. 3202 3203 NODISCARD _CONSTEXPR20 reference front() noexcept /* strengthened */ { 3204 #if _CONTAINER_DEBUG_LEVEL > 0 3205 STL_VERIFY(this->_Mysize != 0, "front() called on empty vector"); 3206 #endif // _CONTAINER_DEBUG_LEVEL > 0 3207 3208 return *begin() ; 3209 3210 3211 _NODISCARD _CONSTEXPR20 const_reference front() const noexcept /* strengthened */ { 3212 #if _CONTAINER_DEBUG_LEVEL > 0 3213 STL_VERIFY(this->_Mysize != 0, "front() called on empty vector"); 3214 #endif // _CONTAINER_DEBUG_LEVEL > 0

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

Mobile Communications

Authors: Jochen Schiller

2nd edition

978-0321123817, 321123816, 978-8131724262

More Books

Students also viewed these Programming questions

Question

What is virtual shopping?

Answered: 1 week ago

Question

i need 4 0 7 .

Answered: 1 week ago