2017-11-15

posted Nov 15, 2017, 6:20 AM by Konstantinovich Samuel   [ updated Nov 15, 2017, 9:15 AM ]
Goal: Cellular Automata


If you think about your code using big ideas and write functions to do those ideas, you will meet with more success.

Most of your cellular automata can be written in the following way using 'big ideas':



  EVERYONE  pickYourNextColor  

  EVERYONE  changeToNextColor   


Notice you are NOT storing a count, you are deciding on your color and storing the intended color you wish to change to! This prevents many kinds of problems!


PickYourNextColor will be different for different automata, but the separation of deciding what you want to be, and the changing into that new state must be clear.


LabN08  Conway's game of life: 
(this is a 2 day lab/hw,  we will continue tomorrow and address any issues you have!)

CONWAY's GAME OF LIFE:

Patches are either Alive or Dead
Each type of patch exhibits different behavior. This is a variation on what you have done already with voting. 

Dead (pcolor is black)
    Dead patches sometimes stay dead, and sometimes are re-born.

Alive  (pcolor is ... NOT black, this allows any colors to be used!)
    Alive patches sometimes stay alive, and sometimes die.

*STYLE AND READABILITY*
Since you do not want random bits of code to just compare colors all the time, this is a good time to try to figure out how to write more meaningful comparisons!
How can you write code that uses:
isAlive     or    isDead        instead of comparing the pcolor?



You must be careful not to allow a patch to die and then to be reborn at the same time! 
In order to do this, do not change color right away, instead, make a variable to store the "nextColor" so that you can make sure everyone bases their next state on the current state. 

Similar to before, you calculate all patches first, then you change later:
ask patches [ calculate next color ]
ask all patches [ change to next color]

How do we store the next state?  (the state in this case is the color, there are many states that an object can be in)
Each patch needs to remmber what color it wants to be. How can you accomplish this?

How do we calculate the next state? We use different rules for each cellular automata:
Birth Survival  notation:
3B/23S is notation that means:

Dead cells 3B:    When a dead cell sees 3 living neighbors it will want to be born (otherwise stay dead)
Living cells  23S:  When a living cell sees 2 or 3 living neighbors the cell wants to stay alive (otherwise die)

Each cell should only do ONE of these. The easiest way of doing this is to have each patch decide what it wants to be next turn. After that completes you should have each patch change to that color. 

Here are some other rules:


Make slider:
Percent-Alive

Make Buttons:
* Setup
    -clear the world. Fill the world with some living patches (Use the percent-alive slider for the % you want to be alive)
* Go
    -Run your Conway model rules, calculating the next state and changing state.

To help test your code, allow yourself to paint living patches manually:
Make a forever observer button called paint, and have it call this function:

* Paint button (forever button, only have 1 forever button active at one time!)

to paint
  if mouse-down? and mouse-inside?
  [
    ask patch mouse-xcor mouse-ycor
    [
      set pcolor lime
    ]
  ]
end

Try painting a glider!

Where X is alive: 

XXX

X__

_X_



Finally!

You can save the patches and load them later! We can discuss this tomorrow.
Comments