home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / basic / compiler / asic / life.asi < prev    next >
Text File  |  1994-03-01  |  6KB  |  237 lines

  1. dim a(1600)
  2. dim b(1600)
  3. rem LIFE -- An ASIC Demonstration Program
  4. rem Copyright 1990 by David A. Visti
  5.  
  6. REM LIFE was developed by John Conway in Scientific American.  It utilizes a
  7. REM simple alogrithm to generate interesting patterns on the screen.
  8. REM The playing field is 20x80 characters in size.  Each character position can 
  9. REM be empty, or it can contain a single "cell" of life.  The rules of LIFE
  10. REM are simple.  Any "cell" which has less then 2 neighboring "cells" dies of
  11. REM isolation.  Any "cell" which has more than 3 neighboring "cells" dies of
  12. REM overcrowding.  Any "cell" with 2 or 3 neighbors will continue to live.
  13. REM Finally, any empty character position which has exactly 3 neighbors which
  14. REM contain "cells" will give birth to a new "cell".
  15.  
  16. REM The following are notes specific to this implementation of LIFE:
  17.  
  18. REM 1. Any valid ASCII character value 1-255 can be used to represent the
  19. REM    life cells.  The default value is 177 ("▒").  Just change the
  20. REM    line containing "symbol=nnn" to the value you prefer.
  21. REM 2. The colors on the screen (for color graphics equipped systems) are
  22. REM    set in the "initgame" routine.  The first "color n,n" statement
  23. REM    sets the color in the "Cell" area of the screen to yellow foreground
  24. REM    on a blue background.  The second "color n,n" statement sets the color
  25. REM    in the "Text" area of the screen to yellow foreground on a green
  26. REM    background.
  27. REM 3. Two arrays are used to hold "cell" information.  Array "B" is used to
  28. REM    calculate births/deaths.  Array "A" is used to temporarily hold a new
  29. REM    generation of cells, and it is used to copy the cells directly to
  30. REM    the video memory.
  31. REM 4. It is assumed that the edges of the playing area are unsuitable for life 
  32. REM    cells, so no cells will thrive in the outside row/col of the playing area
  33.  
  34. REM program begins here
  35.    gosub initgame:
  36.    gosub music:
  37.    gosub updatedisplaymatrix:
  38.    gosub updatescreen:
  39. loop: gosub computenewgen:
  40.    time=time+1
  41.    gosub updatecalcmatrix:
  42.    gosub updatescreen:
  43.    x$=inkey$
  44.    if x$="" then loop:
  45.    cls
  46.    end
  47.  
  48. updatescreen: rem this code updates the screen after each gen is calculated
  49.    rem it utilizes direct video RAM writes for maximum speed
  50.    for i=0 to 3199
  51.       varaddr=varptr(a(0))
  52.       varaddr=varaddr+2
  53.       varaddr=varaddr+i
  54.       pokeval=peek(varaddr)
  55.       rem point to video memory segment
  56.       defseg=vidmemseg
  57.       poke i,pokeval
  58.       rem reset data segment register to default
  59.       defseg=-1
  60.       i=i+1
  61.    next i
  62.    locate 24,12
  63.    print  time;
  64.    return
  65.  
  66. updatedisplaymatrix: rem copy calculation matrix to display matrix
  67.    for i=0 to 19
  68.        for j = 1 to 80
  69.            k=i*80
  70.            k=k+j
  71.            a(k)=b(k)
  72.        next j
  73.    next i
  74.    return
  75.  
  76. initgame: rem initialize game
  77.  
  78.    randomize
  79.    symbol=177
  80.    crowddeath=symbol*3
  81.    lonelydeath=symbol*2
  82.    newbirth=symbol*3
  83.    time=0
  84.    cls
  85.    rem initially set video memory segment to monochrome adapter
  86.    vidmemseg=-20480
  87.    vidtype=zmode
  88.    rem check for color graphics adapter
  89.    if vidtype=0 then leavedefaultcolors:
  90.    rem we have a CGA or other color graphics card
  91.    vidmemseg=-18432
  92.    color 14,1
  93.    rem set first 20 lines attribute bytes to BLUE
  94.    for i=1 to 42
  95.       print "                                        ";
  96.    next i
  97.    rem set remaining 5 lines to GREEN
  98.    locate 21,0
  99.    color 14,2
  100.    for i=1 to 8
  101.       print "                                        ";
  102.    next i
  103. leavedefaultcolors:
  104.    locate 20,25
  105.    print "    <  L  I  F  E  >";
  106.    locate 21,10
  107.    print "        A Demonstration Program written in ASIC";
  108.    locate 22,15
  109.    print "     <Copyright 1990 by David A. Visti>";
  110.    locate 23,15
  111.    print "           <All Rights Reserved>";
  112.    locate 24,1
  113.    print "Generation";
  114.    locate 24,56
  115.    print "<Press any key to EXIT>";
  116.    for i=0 to 19
  117.        for j=1 to 80
  118.            k=i*80
  119.            k=k+j
  120.            k1=rnd(0)
  121.            if k1>28000 then makefullcell:
  122. makeempytcell:
  123.            b(k)=0
  124.            goto continit:
  125. makefullcell:
  126.            b(k)=symbol
  127. continit:
  128.        next j
  129.    next i
  130.    return
  131.  
  132. computenewgen:
  133.    for i=0 to 19
  134.        for j=1 to 80
  135.            k=i*80
  136.            k=k+j
  137.            if i=0 then clearcell:
  138.            if i=19 then clearcell:
  139.            if j=1 then clearcell:
  140.            if j=80 then clearcell:
  141.            n=0
  142.            k2=k-81
  143.            n=n+b(k2)
  144.            k2=k-80
  145.            n=n+b(k2)
  146.            k2=k-79
  147.            n=n+b(k2)
  148.            k2=k-1
  149.            n=n+b(k2)
  150.            k2=k+1
  151.            n=n+b(k2)
  152.            k2=k+79
  153.            n=n+b(k2)
  154.            k2=k+80
  155.            n=n+b(k2)
  156.            k2=k+81
  157.            n=n+b(k2)
  158.            if n> crowddeath then clearcell:
  159.            if n<lonelydeath then clearcell:
  160.            if n<newbirth then survivecell:
  161.            a(k)=symbol
  162.            goto nextcell:
  163. survivecell: a(k)=b(k)
  164.            goto nextcell:
  165. clearcell: a(k)=0
  166. nextcell:
  167.        next j
  168.    next i
  169. return
  170.  
  171. updatecalcmatrix:
  172.    for i=0 to 19
  173.        for j = 1 to 80
  174.            k=i*80
  175.            k=k+j
  176.            b(k)=a(k)
  177.        next j
  178.    next i
  179.    return
  180.  
  181. music:  rem play a short little tune
  182.  
  183.    tune$="<<<<GD<GD<<<<<GD<500"
  184.  
  185.  
  186. rem laser sound
  187. k=150
  188. n=0
  189. for m=1 to 5
  190. for i=n to k
  191.     sound i,1
  192. next i
  193. j=k
  194. for i=n to k
  195.     sound j,1
  196.     j=j-1
  197. next i
  198. next m
  199.  
  200. rem siren
  201. k=450
  202. for i=n to k
  203.     sound i,1
  204. next i
  205. j=k
  206. for i=n to k
  207.    sound j,1
  208.    j=j-1
  209. next i
  210. for i=1 to 10000
  211. next i
  212.  
  213. rem fanfare
  214. base=10
  215. n=len(tune$)
  216. for i=1 to n
  217.    note$=mid$(tune$,i,1)
  218.    note=asc(note$)
  219.    note=note*base
  220.    i=i+1
  221.    length$=mid$(tune$,i,1)
  222.    length=asc(length$)
  223.    length=length*base
  224.    sound note,length
  225.    gosub d1:
  226. next i
  227. return
  228.  
  229. d1:for killtime=1 to 750
  230. next killtime
  231. return
  232.  
  233.  
  234.  
  235.  
  236.  
  237.