home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / vim45os2.zip / vim-4.5 / macros / life / life.mac < prev   
Text File  |  1996-10-15  |  7KB  |  251 lines

  1. " Macros to play Conway's Game of Life in vi
  2. " Version 1.0m: edges wrap
  3. " by Eli-the-Bearded (eli@netusa.net), Sept 1996
  4. " This file may be free distributed so long as these credits remain unchanged.
  5. "
  6. " Modified by Bram Moolenaar (mool@oce.nl), 1996 Sept 10
  7. " - Made it quite a bit faster, but now needs search patterns in the text
  8. " - Changed the order of mappings to top-down.
  9. " - Made "g" run the whole thing, "C" run one generation.
  10. " - Added support for any uppercase character instead of 'X'
  11. "
  12. " Rules:
  13. "   If a germ has 0 or 1 live neighbors it dies of loneliness
  14. "   If a germ has 2 or 3 live neighbors it survives
  15. "   If a germ has 4 to 8 live neighbors it dies of starvation
  16. "   If an empty box has 3 live neighbors a new germ is born
  17. "
  18. "   A new born germ is an "A".    Every generation it gets older: B, C, etc.
  19. "   A germ dies of old age when it reaches "Z".
  20. "
  21. " Notice the rules do not mention edges. This version has the edges wrap
  22. " around. I have an earlier version that offers the option of live edges or
  23. " dead edges. Email me if you are interested. -Eli-
  24. "
  25. " Note: This is slow!  One generation may take up to ten minutes (depends on
  26. " your computer and the vi version).
  27. "
  28. " Quite a lot of the messy stuff is to work around the vi error "Can't yank
  29. " inside global/macro".  Still doesn't work for all versions of vi.
  30. "
  31. " To use these macros:
  32. "
  33. " vi        start vi/vim
  34. "
  35. " :so life.mac    Source this file
  36. "
  37. " g        'g'o!  runs everything until interrupted: "IR".
  38. "
  39. " I        Initialize everything. A board will be drawn at the end
  40. "        of the current buffer. All line references in these macros
  41. "        are relative to the end of the file and playing the game
  42. "        can be done safely with any file as the current buffer.
  43. "
  44. "    Change the left field with spaces and uppercase letters to suit
  45. "    your taste.
  46. "
  47. " C        'C'ompute one generation.
  48. " +        idem, time running one generation.
  49. " R        'R'un 'C'ompute until interrupted.
  50. " 1Gi<nr><Esc>z    Make a number the only thing on the first line and use
  51. "        'z' to time that many generations.
  52. "
  53. " Time to run ten generations on my 100Mhz 486 (FreeBSD):
  54. "   vi      3.7        3 min 54 sec
  55. "   vim   4.5        1 min 45 sec
  56. "   nvi   1.78        1 min 02 sec
  57. "   Elvis 1.8pl4    failed
  58. "   Elvis 2.0        failed
  59. "
  60. "
  61. " And now the macros, more or less in top-down order.
  62. "
  63. "  ----- macros that can be used by the human -----
  64. "
  65. " 'g'o: 'I'nitialize and then 'R'un 'C'ompute recursively (used by the human)
  66. map g IR
  67. "
  68. "
  69. " 'R'un 'C'ompute recursively (used by the human and 'g'o)
  70. map R CV
  71. " work around "tail recursion" problem in vi, "V" == "R".
  72. map V R
  73. "
  74. "
  75. " 'I'nitialize the board (used by the human and 'g'o)
  76. map I G)0)0)0)0)1)0)0)2)0)0)0)0,ok,-11k,-,R,IIN
  77. "
  78. "
  79. " 'C'ompute next generation (used by the human and others)
  80. map C T>>>>>>>>B&
  81. "
  82. "
  83. " Time running one generation (used by the human)
  84. map + <1C<2
  85. "
  86. "
  87. " Time running N generations, where N is the number on the current line.
  88. " (used by the human)
  89. map z ,^,&,*,&<1,*<2
  90. "
  91. "  ----- END of macros that can be used by the human -----
  92. "
  93. "  ----- Initialisation -----
  94. "
  95. map ,- :s/./-/g
  96. map ,o oPut 'X's in the left box, then hit 'C' or 'R'
  97. map ,R 03stop
  98. "
  99. " Write a new line (used by 'I'nitialize board)
  100. map )0 o-                    --....................--....................-
  101. map )1 o-        VIM         --....................--....................-
  102. map )2 o-       LIVES        --....................--....................-
  103. "
  104. "
  105. " Initialisation of the pattern/command to execute for working out a square.
  106. " Pattern is: "#<germ><count>"
  107. " where <germ>   is " " if the current germ is dead, "X" when living.
  108. "       <count>  is the number of living neighbours (including current germ)
  109. "                expressed in X's
  110. "
  111. map ,Il8 O#XXXXXXXXXX .`a22lr 
  112. map ,Id8 o# XXXXXXXX .`a22lr 
  113. map ,Il7 o#XXXXXXXXX .`a22lr 
  114. map ,Id7 o# XXXXXXX .`a22lr 
  115. map ,Il6 o#XXXXXXXX .`a22lr 
  116. map ,Id6 o# XXXXXX .`a22lr 
  117. map ,Il5 o#XXXXXXX .`a22lr 
  118. map ,Id5 o# XXXXX .`a22lr 
  119. map ,Il4 o#XXXXXX .`a22lr 
  120. map ,Id4 o# XXXX .`a22lr 
  121. map ,Il3 o#XXXXX .,a
  122. map ,Id3 o# XXX .`a22lrA
  123. map ,Il2 o#XXXX .,a
  124. map ,Id2 o# XX .`a22lr 
  125. map ,Il1 o#XXX .`a22lr 
  126. map ,Id1 o# X .`a22lr 
  127. map ,Il0 o#XX .`a22lr 
  128. map ,Id0 o#  .`a22lr 
  129. "
  130. " Patterns used to replace a germ with it's next generation
  131. map ,Iaa o=AB =BC =CD =DE =EF =FG =GH =HI =IJ =JK =KL =LM =MN =NO =OP =PQ =QR
  132. map ,Iab o=RS =ST =TU =UV =VW =WX =XY =YZ =Z 
  133. "
  134. " Insert the searched patterns above the board
  135. map ,IIN G?^top,Il8,Id8,Il7,Id7,Il6,Id6,Il5,Id5,Il4,Id4,Il3,Id3,Il2,Id2,Il1,Id1,Il0,Id0,Iaa,Iab
  136. "
  137. "  ----- END of Initialisation -----
  138. "
  139. "  ----- Work out one line -----
  140. "
  141. " Work out 'T'op line (used by show next)
  142. map T G,c2k,!9k,@,#j>2k,$j
  143. "
  144. " Work out 'B'ottom line (used by show next)
  145. map B ,%k>,$
  146. "
  147. " Work out a line (used by show next, work out top and bottom lines)
  148. map > 0 LWWWWWWWWWWWWWWWWWW,rj
  149. "
  150. " Refresh board (used by show next)
  151. map & :%s/^\(-[ A-Z]*-\)\(-[ A-Z]*-\)\(-[.]*-\)$/\2\3\3/
  152. "
  153. "
  154. " Work around vi multiple yank/put in a single macro limitation
  155. " (used by work out top and/or bottom line)
  156. map ,$ dd
  157. map ,% "cp
  158. map ,! "byy
  159. map ,@ "cyy
  160. map ,# "bP
  161. map ,c c$
  162. "
  163. "  ----- END of Work out one line -----
  164. "
  165. "  ----- Work out one square -----
  166. "
  167. " The next three work out a square: put all nine chars around the current
  168. " character on the bottom line (the bottom line must be empty when starting).
  169. "
  170. " 'W'ork out a center square (used by work out line)
  171. map W makh,3`ah,3`ajh,3(
  172. "
  173. "
  174. " Work out a 'L'eft square (used by work out line)
  175. map L makf-h,1`ak,2`af-h,1`a,2`ajf-h,1`aj,2(
  176. "
  177. "
  178. " Work out a 'R'ight square (used by work out line)
  179. map ,r makh,2`akF-l,1`ah,2`aF-l,1`ajh,2`ajF-l,1(
  180. "
  181. " 'M'ove a character to the end of the file (used by all work out square
  182. " macros)
  183. "
  184. map ,1 y G$p
  185. map ,2 2y G$p
  186. map ,3 3y G$p
  187. "
  188. "
  189. "  ----- END of Work out one square -----
  190. "
  191. "  ----- Work out one germ -----
  192. "
  193. " Generate an edit command that depends on the number of living in the last
  194. " line, and then run the edit command. (used by work out square).
  195. " Leaves the cursor on the next character to be processed.
  196. "
  197. map ( ,s,i,X0i?^#A ,df.l,Y21h
  198. "
  199. " Delete 's'paces (deads);
  200. " The number of remaining characters is the number of living neighbours.
  201. map ,s :.g/ /s///g
  202. "
  203. " Insert current character in the last line
  204. map ,i `ay GP
  205. "
  206. " Replace any uppercase letter with 'X';
  207. map ,X :.g/[A-Z]/s//X/g
  208. "
  209. " Delete and execute the rest of the line
  210. map ,d "qd$@q
  211. "
  212. " Yank and execute the rest of the line
  213. map ,Y "qy$@q
  214. "
  215. " Yank the character under the cursor
  216. map ,j y 
  217. "
  218. " Put the current cut buffer after the cursor 
  219. map ,m p
  220. "
  221. " Delete the character under the cursor 
  222. map ,n x
  223. "
  224. " Replace a character by it's next, A --> B,  B --> C, etc.
  225. map ,a `a,jGi?=,ma,dll,j`a21l,ml,nh
  226. "
  227. "  ----- END of Work out one germ -----
  228. "
  229. "  ----- timing macros  -----
  230. "
  231. " Get current date (used by time a generation)
  232. map << :r!date
  233. map <1 G?^topO<<
  234. map <2 G?^topk<<
  235. "
  236. "
  237. " Turn number on current line into edit command (used by time N generations)
  238. map ,^ AiC
  239. "
  240. "
  241. " Delete current line and save current line (used by time N generations)
  242. map ,& 0"gd$
  243. "
  244. "
  245. " Run saved line (used by time N generations)
  246. map ,* @g
  247. "
  248. "  ----- END of timing macros  -----
  249. "
  250. " End of the macros.
  251.