O'Reilly logo

Genetic Algorithms and Machine Learning for Programmers by Frances Buontempo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Let’s Make Cellular Automata

Your cells live on a grid with a fixed Height and Width. You’ll need to know which cells are Alive, and you’ll want to Update them together, so make a World class to hold your grid and Update your cells:

 class​ World
 {
 public​:
  World(​size_t​ max_x, ​size_t​ max_y, ​bool​ wrap);
  World(​size_t​ max_x, ​size_t​ max_y, ​bool​ wrap,
 size_t​ start_width, ​size_t​ start_height,
 size_t​ number);
 size_t​ Width() ​const​ { ​return​ max_x; }
 size_t​ Height() ​const​ { ​return​ max_y; }
 size_t​ Alive() ​const​;
 
 bool​ Alive(​size_t​ x, ​size_t​ y) ​const
  {
 return​ state[y*max_x + x];
  }
 
 void​ Spark(​size_t​ x, ​size_t​ y)
  {
 if​(Alive(x,y))
 throw​ std::invalid_argument(​ ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required