/* life.tc - rpb - 3/8/2010 /* see FB tiny-c group curs int r,c [ /* positions cursor at row r, col c MC 27,1 /* ESC MC 91,1 /* left bracket pos r;MC ';',1;pos c MC 'H',1 ] plot int r,c,onoff [ /* positions cursor at row r, col c MC 27,1 /* ESC MC 91,1 /* left bracket pos r;MC ';',1;pos c MC 'H',1 if (onoff) MC '*',1;else MC ' ',1 ] pos int rc [ /* helper function for plot if (rc>9) [ MC rc/10+48,1;rc=rc%10 ] MC rc+48,1 ] pause int k [ int l;while (++l p) return 0 else return 1 ] nextgencellval int data(0), i, r, c, n [ /* Function used to determine whether or not cell will be occupied on next generation. int x if (r > 1) [ if (c > 1) x = x + data(i-n-1) x = x + data(i-n) if (c < n) x = x + data(i-n+1) ] if (c > 1) x = x + data(i-1) if (c < n) x = x + data(i+1) if (r < n) [ if (c > 1) x = x + data(i+n-1) x = x + data(i+n) if (c < n) x = x + data(i+n+1) ] if (data(i)) [ if (x == 2) return 1 else if (x == 3) return 1 return 0 ] else [ if (x == 3) return 1 return 0 ] ] main [ /* Life simulator pl "";ps "life.tc - rpb - 3/8/2010";pl "";pl "" int r, c, n, p, i, j, data(1600), hold(1600), occ, gen, mgen, chg ps "Enter a seed number. ==> ";last = gn pl "" if ( last%2 == 0 ) ++last /* make number odd seed = last ps "Enter size of square grid (2 to 40). ==> ";n = gn pl "" if (n > 40) n = 0 while (n < 2) [ ps "Invalid entry. Enter number between 2 and 40. ==> ";n = gn pl "" if (n > 40) n = 0 ] ps "Enter approx pct of cells to begin as occupied (between 10 and 90). ==> ";p = gn pl "" if (p > 90) p = 0 while (p < 10) [ ps "Invalid entry. Enter number between 10 and 90. ==> ";p = gn pl "" if (p > 90) p = 0 ] ps "Enter max generations to run (between 1 and 1000). ==> ";mgen = gn pl "" if (mgen > 1000) mgen = 0 while (mgen < 1) [ ps "Invalid entry. Enter number between 1 and 1000. ==> ";mgen = gn pl "" if (mgen > 1000) mgen = 0 ] cls for ( r = 1; r <= n; ++r ) [ for ( c = 1; c <= n; ++c ) [ data(i) = firstgencellval p occ = occ + data(i) plot r, c, data(i) ++i ] ] if (occ == 0) [ curs n+2,1 ps "--- Simulation Complete ---";pl "" pl "";ps "Civilization never started";pl "" ] for (j=0;j<25;++j) pause 20000 while (occ) [ for (j=0;j 0) [ ++gen if (occ == 0) [ curs n+2,1 ps "--- Simulation Complete ---";pl "" pl "";ps "The civilization died out between Generations";pn gen-1;ps " and";pn gen;pl "" ] else if (gen >= mgen) [ curs n+2,1 ps "--- Simulation Complete ---";pl "" pl "";ps "Max Generations of";pn mgen;ps " has been reached";pl "" pl "";ps "May they live long and prosper!";pl "" occ = 0 ] ] else [ curs n+2,1 ps "--- Simulation Complete ---";pl "" pl "";ps "There was no change between Generations";pn gen;ps " and";pn gen+1;pl "" pl "";ps "The survivors will live forever!";pl "" occ = 0 ] pause 2000 ] ]