Conway's Game of Life... written in batch.

This is not meant to be a useful program, rather it is an exercise in batch programming. Real Life programs and information may be found at....

This batch sytem requires stock DOS 6 with an ANSI driver installed. If you're using a different version of DOS you will probably have to modify it somewhat to make it work.

Originally this consisted of one big batch but I separated the most-used subroutines into separate batches because it improves the generation speed considerably. The files LIFE.BAT, SUB0.BAT, SUB1.BAT, PLUS.BAT and MINUS.BAT should all be placed in their own directory. Run 'LIFE' to start the program. You can choose a random start or enter a cell pattern with the cell editor. Once cells have been made you also have the option of continuing or clearing the cells. Press Control-C or Control-Break at any time to return to the menu.

The following listings are HTML-converted for proper display with web browsers, download BATLIFE.ZIP for a ready-to-run copy and get ready to burn those CPU cycles...

------------------  File LIFE.BAT -------------------------
:: Conway's Game of Life (in batch!)
:: For DOS 6 with ANSI console driver
:: Uses SUB0.BAT SUB1.BAT PLUS.BAT MINUS.BAT
:: Written by Terry Newton  April 26, 1996
@echo off
if #%1==#sub goto %2
if #%comspec%==# set comspec=command
%comspec% /e:3000 /c %0 sub shell
if exist life$ del life$
if exist running %0
if exist newcells\nul deltree /y newcells>nul
goto lastline
:shell
:: (::) next 3 lines if your driver isn't named "ANSI"
mem /c|find "ANSI">nul
if errorlevel 1 echo This batch requires an ANSI driver
if errorlevel 1 goto lastline
set gen=I
set xgen=
if exist gencnt.bat call gencnt
::char for cell...
set c=O
::char for space...
set s= % %
::color codes...
::(text,dead,survive2,survive3,birth,cursor)
set tc=
set dc=
set s2c=%c%
set s3c=%c%
set bc=%c%
set cc=
if not exist cells\nul md cells
set keys=123Q
if exist cells\*. set keys=123Q4
echo %tc%
cls
echo.>running
echo. 
echo  Conway's Game of Life
echo  The Snail Version!
echo.
echo  Select...
echo.
echo   1) Random Start
echo   2) Edit Cells
if exist cells\*. echo   3) Remove All Cells
if exist cells\*. echo   4) Continue Growing
echo   Q) Quit
echo.
choice /c:%keys%>nul
if errorlevel 5 goto generate
if errorlevel 4 goto quit
if exist gencnt.bat del gencnt.bat
set gen=I
set xgen=
if errorlevel 3 goto clearcells
if errorlevel 2 goto editcells
if errorlevel 1 goto randomstart
echo  Sorry, this batch requires DOS 6
goto lastline
:quit
del running
exit
:clearcells
del cells\*.
goto shell
:randomstart
echo %tc%
cls
echo.
echo  Creating Random Cell Pattern...
echo  Press Ctrl-C for menu
if exist cells\*. del cells\*.
for %%a in (9 10 11 12) do call %0 sub iy %%a
goto generate
:iy
if %3==11 echo  Halfway done...
for %%b in (5 6 7 8 9 10 11 12 13 14 15 16) do call %0 sub ix %3 %%b
goto lastline
:ix
echo.|time>life$
set cell=off
find ".1"<life$>nul
if not errorlevel 1 set cell=on
find ".3"<life$>nul
if not errorlevel 1 set cell=on
find ".5"<life$>nul
if not errorlevel 1 set cell=on
find ".8"<life$>nul
if not errorlevel 1 set cell=on
del life$
if %cell%==off goto lastline
echo.>cells\%3-%4
goto lastline
:editcells
echo %tc%
cls
set next=lastline
call sub0 sub display
:edit1
echo Use keypad arrows to move
echo Press + to add a cell
echo Press - to remove a cell
echo Press G to generate cells
echo Press Ctrl-C for menu
set x=9
set y=9
:editloop
echo [%y%;%x%H%cc%%s%
if exist cells\%y%-%x% echo [%y%;%x%H%cc%%c%
choice /c:4862+-g>nul
echo [%y%;%x%H%dc%%s%
if exist cells\%y%-%x% echo [%y%;%x%H%dc%%c%
if errorlevel 7 goto gen1
if errorlevel 6 goto turnoff
if errorlevel 5 goto turnon
if errorlevel 4 goto move-s
if errorlevel 3 goto move-e
if errorlevel 2 goto move-n
call minus %x%
set x=%out%
goto editloop
:move-n
call minus %y%
set y=%out%
goto editloop
:move-e
call plus %x%
set x=%out%
goto editloop
:move-s
call plus %y%
set y=%out%
goto editloop
:turnoff
if exist cells\%y%-%x% del cells\%y%-%x%
goto editloop
:turnon
echo.>cells\%y%-%x%
goto editloop
:generate
sub0 sub generate
:gen1
sub0 sub gen1 
:lastline
 
------------------  File SUB0.BAT -------------------------
if #%1==#sub goto %2
:generate
echo %tc%
cls
set next=gen1
:display
echo  ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
for %%a in (3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18) do call %0 sub iy1 %%a
echo  ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
goto %next%
:emptyline
echo  ³%dc%%s%%s%%s%%s%%s%%s%%s%%s%%s%%s%%s%%s%%s%%s%%s%%s%%tc%³
goto lastline
:iy1
if not exist cells\%3-* goto emptyline
set line=
for %%b in (3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18) do call %0 sub ix1 %3 %%b
echo  ³%dc%%line%%tc%³
goto lastline
:ix1
if exist cells\%3-%4 set line=%line%%c%
if not exist cells\%3-%4 set line=%line%%s%
goto lastline
:gen1
echo %tc%Conway's Game of Life
echo The Snail Version!
echo 
echo 
echo Press Ctrl-C for menu
if not exist newcells\nul md newcells
if exist newcells\*. del newcells\*.
if exist cells\*. copy cells\*. newcells>nul
:loop1
echo Generation %xgen%%gen%
for %%a in (3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18) do call %0 sub iy2 %%a
if exist cells\*. del cells\*.
if exist newcells\*. copy newcells\*. cells>nul
set gen=%gen%I
if %gen%==IIII set gen=IV
if %gen%==IVI set gen=V
if %gen%==VIIII set gen=IX
if %gen%==IXI set xgen=%xgen%X
if %gen%==IXI set gen=
echo>gencnt.bat set gen=%gen%
echo>>gencnt.bat set xgen=%xgen%
goto loop1
:iy2
if exist cells\%3-* goto reallydoit
call minus %3
if exist cells\%out%-* goto reallydoit
call plus %3
if not exist cells\%out%-* goto lastline
:reallydoit
for %%b in (3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18) do call sub1 %3 %%b
:lastline
 
------------------  File SUB1.BAT -------------------------
if #%1==#sub goto %2
:ix2
echo Working on cell %1-%2...  
set count=
for %%c in (1 2 3 4 5 6 7 8) do call %0 sub cnt%%c %1 %2
if not exist cells\%1-%2 goto chk3
if #%count%==#... goto sur3
if #%count%==#.. goto sur2
echo (%1-%2 died)        
del newcells\%1-%2
echo [%1;%2H%dc%%s%%tc%
goto lastline
:chk3
if not #%count%==#... goto lastline
echo (%1-%2 born)        
echo.>newcells\%1-%2
echo [%1;%2H%bc%%tc%
goto lastline
:sur2
echo (%1-%2 lives by 2)  
echo [%1;%2H%s2c%%tc%
goto lastline
:sur3
echo (%1-%2 lives by 3)  
echo [%1;%2H%s3c%%tc%
goto lastline
:cnt1
call minus %3
set y=%out%
call minus %4
set x=%out%
if exist cells\%y%-%x% set count=%count%.
goto lastline
:cnt2
set y=%3
call minus %4
set x=%out%
if exist cells\%y%-%x% set count=%count%.
goto lastline
:cnt3
call plus %3
set y=%out%
call minus %4
set x=%out%
if exist cells\%y%-%x% set count=%count%.
goto lastline
:cnt4
call minus %3
set y=%out%
set x=%4
if exist cells\%y%-%x% set count=%count%.
goto lastline
:cnt5
call plus %3
set y=%out%
set x=%4
if exist cells\%y%-%x% set count=%count%.
goto lastline
:cnt6
call minus %3
set y=%out%
call plus %4
set x=%out%
if exist cells\%y%-%x% set count=%count%.
goto lastline
:cnt7
set y=%3
call plus %4
set x=%out%
if exist cells\%y%-%x% set count=%count%.
goto lastline
:cnt8
call plus %3
set y=%out%
call plus %4
set x=%out%
if exist cells\%y%-%x% set count=%count%.
:lastline
 
------------------  File PLUS.BAT -------------------------
if %1==3 set out=4
if %1==4 set out=5
if %1==5 set out=6
if %1==6 set out=7
if %1==7 set out=8
if %1==8 set out=9
if %1==9 set out=10
if %1==10 set out=11
if %1==11 set out=12
if %1==12 set out=13
if %1==13 set out=14
if %1==14 set out=15
if %1==15 set out=16
if %1==16 set out=17
if %1==17 set out=18
if %1==18 set out=3
 
------------------  File MINUS.BAT -------------------------
if %1==3 set out=18
if %1==4 set out=3
if %1==5 set out=4
if %1==6 set out=5
if %1==7 set out=6
if %1==8 set out=7
if %1==9 set out=8
if %1==10 set out=9
if %1==11 set out=10
if %1==12 set out=11
if %1==13 set out=12
if %1==14 set out=13
if %1==15 set out=14
if %1==16 set out=15
if %1==17 set out=16
if %1==18 set out=17
 
---------------- end of files -----------------