[2017-05-18] Challenge #315 [Intermediate] Game of life that has a twist

This is one of those problems for which I absolutely love NetLogo. I made a quick and dirty simulation that completes the challenge and more.

The interface tab has a button for "setup", "go", and a slider for "frames."

Here's the code:

patches-own [next-color]

to setup clear-all ask patches [ if random 100 < 20 [set pcolor one-of (list red blue)] ] reset-ticks end

to go ask patches [ if live-neighbors = 3 [ set next-color majority-color ] if live-neighbors < 2 or live-neighbors > 3 [ set next-color black ] ]

ask patches [ set pcolor next-color ]

if ticks = frames [stop] tick end

to-report live-neighbors report count neighbors with [pcolor != black] end

to-report majority-color let blues count neighbors with [pcolor = blue] let reds count neighbors with [pcolor = red] if pcolor = blue [set blues blues + 1] if pcolor = red [set reds reds + 1]

if blues > reds [report blue] if reds > blues [report red] report pcolor


Here is a picture of the interface. I know technically this doesn't meet the challenge's requirements because there's no ASCII-like output... I'd know how to do it, though. NetLogo can output text files and I'd have it go through the patches from the top to bottom, putting a character corresponding to color and a "\n" at the end of each line.

/r/dailyprogrammer Thread