home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a079 / 1.img / FPDG.LZH / VOL2NUM0 / FOXLIFE / FOXLIFE.APP (.txt) next >
Encoding:
MS Visual FoxPro App  |  1993-02-02  |  26.9 KB  |  263 lines

  1. * This represents a "glider gun" developed at M.I.T. that first proved 
  2. * that a Life pattern could continue generation infinitely.  After 39 
  3. * cylces it will emit a "glider" which will begin to move down the
  4. * screen.  A new glider is emitted each addtional 30 cycles.  For
  5. * this to work, you must play with the "flat" screen option.
  6. * (Taken from the excellent book "The Turing Omnibus" by A.K. Dewdney.)
  7.                                         
  8.                   XX                        
  9.                  X                                    
  10.                 X             X                              
  11.        X        X            XX                      
  12.       XX        X               XX                  
  13.                  X              XXX      X                
  14.                   XX            XX      XX                 
  15.                              XX                            
  16.                               X                         
  17.                                               
  18.                                                    
  19. * Simple "gliders" that move across the screen endlessly.
  20. * Use with "circular" option for best effect.  (This file was used
  21. * to test and debug this Life program.)
  22.  X         X         X         X         X        X        X         X
  23.   X         X         X         X         X        X        X         X
  24. XXX       XXX       XXX       XXX       XXX      XXX      XXX       XXX
  25.  X         X         X         X         X        X        X         X
  26.   X         X         X         X         X        X        X         X
  27. XXX       XXX       XXX       XXX       XXX      XXX      XXX       XXX
  28.  X         X         X         X         X        X        X         X
  29.   X         X         X         X         X        X        X         X
  30. XXX       XXX       XXX       XXX       XXX      XXX      XXX       XXX
  31.  X         X         X         X         X        X        X         X
  32.   X         X         X         X         X        X        X         X
  33. XXX       XXX       XXX       XXX       XXX      XXX      XXX       XXX
  34.  X         X         X         X         X        X        X         X
  35.   X         X         X         X         X        X        X         X
  36. XXX       XXX       XXX       XXX       XXX      XXX      XXX       XXX
  37. * These are some simple "gliders" that will move across the
  38. *   screen endlessly.
  39.        X
  40.         X
  41.       XXX
  42.              X
  43.               X 
  44.             XXX
  45.                    X
  46.                     X
  47.                   XXX
  48.                          X
  49.                           X
  50.                         XXX
  51. Adding New "Life" To FoxPro
  52. Tom Rombouts
  53. Introduction
  54. ============
  55. If you are the prototypical computer nerd, and spent much of your undergraduate waking hours in the middle of the night, staring at increasingly blurry screens or wading through multi-pound printouts, then "Life" needs no further introduction.  On the other hand, if you remember "Life" either as a breakfast cereal, or else a board game where you carefully prepared for the day of reckoning, then read on.
  56. "Life" is arguably one of the most popular computer games ever. (Although "Leisure Suit Larry" may likely pre-empt it soon!) Actually, Life is more than just a game.  It is actually a form of a mathematical theory that has been analyzed and written about more than you might think possible.
  57. Life was invented around 1970 by John Conway, and first presented to the world in Martin Gardner's "Mathematical Games" column in the October, 1970 "Scientific American."  A follow-up column appeared in the February, 1971 "Scientific American."  The rules are very simple:  
  58. 1.  The game is played on a two dimensional grid of rectangular cells.  (This version uses all but the bottom line of the screen as a 24 x 80 grid.)
  59. 2.  Each cell can be in one of two states, either "dead" (or empty) or else "alive" (or full).
  60. 3.  Each cell has eight neighbor cells, the four cells touching it, and the four cells "kitty corner" to it. 
  61. 4.  If a Life cell has less than two immediate neighbors, it dies of loneliness.  If a Life cell has more than three neighbors, it dies of overcrowding.
  62. 5.  If an dead Life cell has exactly three neighbors, it is brought to life.
  63. Essentially, you start with either a random pattern, or else a carefully prepared set of initial cells.  The computer then continually re-calculates each new generation based on the above rules, and updates the screen accordingly.  (In case it is not clear above, the entire pattern must be recalculated before any cells are either born or die.)
  64. When the game was first introduced, its creator, John Conway, made the prediction that any starting pattern would eventually either lead to no living cells at all, or else end up in a perpetually repeating pattern.  Soon, however, a group of M.I.T. students came up with a pattern known as a "glider gun" that would shoot out perpetually living "gliders" that moved across the screen, every 30 moves.  Given an infinite grid, such a pattern obviously increases the total population of cells perpetually.
  65. For some people, this game becomes fascinating and almost addictive.  (At least for a short period - I suspect many look back later and can not understand why they spent so many hours of fleeting youth on such frivolity! :-) )  The more you observe it, the more you will start to recognize some of the same patterns that appear again and again.  Note that this version allows you to save games in progress, and to edit ASCII files with a text editor to create new starting patterns.
  66. If you wish to know even more about Life, look up the subject "Cellular Automata" (or possibly "Finite State Automata") in your favorite CS library.  (Oddly enough, E.F. Codd, the man who gave us the 12 rules of relational databases, wrote one of the first works in this area titled simply "Cellular Automata" in 1968.) Over the years, the game has been mentioned many more times in computer and mathematical oriented publications.  In fact, as of 1978, there was even a quarterly magazine ("Lifeline") devoted solely to Life!  The address was Robert Wainwright, 1280 Edcris Road, Yorktown Heights, NY, 10598.
  67. Notes on this Implementation
  68. ============================
  69. This version started out as anonymous C source from USENET, which was very fast but did not enforce the above stated rules 100% correctly.  This FoxPro version is fairly slow by comparison, but does provide slightly more elegant saving and restore options.
  70. Due to a full time job and the impending arrival of my first child, I did not have time to fine tune this as much as I would have liked.  Still, this version does lay a good foundation for others who may wish to enhance it further.  In particular, the menuing needs a bit of work, and the interactive editing of a game in progress is not finished.  (Fragments of this can be seen in the code.)
  71. The seemingly obvious approach of using two matching arrays, one for the current display and the other for re-calculation, is not necessary.  Since the highest neighbor count can be 8, it was simple to use the first digit for the neighbor count, and then add this to 10 if the cell is alive, or 0 if it is not.  The new modulus operator (%) of FoxPro 2.0 is then used to determine if the cell is alive or not.
  72. Note also that I chose to make the screen "circular."  That is, the cells along the top row are considered adjacent to the bottom row, and the cells on the far left column are considered adjacent to the cells on the far right column.  This way, any patterns that move continuously (called "gliders" by Life buffs) will go off one side but begin to appear immediately on the other side.
  73. Regarding saving or restoring a data file, I chose to use a space to mean a "dead" cell, and any other character to mean a "living" cell.  This way, any text editor can be used to easily edit games.  As in Xbase, a line in the data file (a .DAT extension is assumed) beginning with a "*" is ignored, allowing for easy documentation of patterns within the file.  If a line ends before 79 columns, or a file before 24 rows, the missing cells are initialized as "dead."
  74. Since the game Life is so well known, this would make an interesting challenge to see who could come up with the fastest calculation - display algorithm.  (For example, one could display as soon as the neighbor calculation is done, saving one trip through the array.  However, the even, top to bottom re-display seems most natural, methinks.  To preserve this, the calculation of the corners and sides would need to be re-worked.)
  75. As I worked on this code, I spotted some ways in which its overall speed of calculation could be improved.  When determining if a cell is to live, die or be re-born, the rules are stated as looking at each of its eight neighbors.  Thus, a simple implementation goes through the entire grid in order, and looks at each cells eight neighbors.  Assuming a 25 x 80 grid (2000 cells) this gives us a rough total of 16,000 computations per generation.
  76. However, in my experience with other implementations of life, I have learned a total population of between 200 and 300 is typical on a 2000 cell grid.  Further, we can turn the rules around and state that if a cell is alive (has a value of 1), add one to the "neighbor count" of its eight neighbors.  By taking this approach, we can look at each cell and determine if it is alive. If (and only if) it is alive, we do the calculations.  Otherwise, we move immediately to the next cell.  This does add one IF test for every cell in the grid, but also saves us 8 computations for each dead (0) cell.
  77. Assuming a 2000 cell grid with 300 "live" cells, this results in 2000 IF tests, leading to 2400 (300 live cells x 8 neighbors) addition operations.  Thus, this strategy results in 4400 total computations, vs. 16,000 for the more simple approach.  This is a good example of a situation where knowledge of the typical case, in this example the percentage of life cells that tend to be alive, is critical to the proper selection of an algorithm.  In the real world, such information is often available.
  78. My second optimization reduces aesthetics, but increases throughput.  (Being of a hackerish bent, this seemed like a great trade-off to me!)  The problem is simple - Xbase screens are 0 based, but Xbase arrays are 1 based.  Thus, to handle an array set up to match the rows and columns of the screen, you need to either add or subtract one somewhere along the line to get the array subscripts to match the screen row and column co-ordinates.
  79. I did not try a final trick - using a one dimensional array or other clever data structures.  It is a rule of computer science that anything that can be done in a N-dimensional array (or matrix) can also be done in a linear array (or vector).  It was my belief that FoxPro would resolve the array subscripts internally far faster than I could by hand coding the arithmetic involved.  (This is due to the interpreted nature of current Xbase products.)  In a lower level language, however, where direct memory access is possible, this could be a fruitful approach.  The "live" cells could be treated as linked lists, so that only the live ones were processed every time.  
  80. Further, since the value of a cell's neighbors is limited to a maximum of 8, one byte could store both a cell's state and the value of its neighbors.  Bit twiddling could be used to adjust these values at maximum speed.  However, these approaches will be left as exercises for the reader.  :-) 
  81. * * * * * * *
  82. ABOUT THE AUTHOR:  Tom Rombouts has been working with Xbase products since 1985.  He was one of the original testers for Emerald Bay, worked in test and development for Ashton-Tate for three years, and has written for the ".DBF" newsletter and "The C Users Journal."  He currently works for Rettig-Miller Corporation in Los Angeles, and is working hard to help perfect Tom Rettig's Office for FoxPro 2.0, which he describes as a "breakthrough product that will change the way Xbase programmers develop applications."
  83. num_or_filef
  84. num_or_filef
  85. p_circularf
  86. STORE .T. TO stop_flag
  87. ACTIVATE POPUP lifeopts
  88. Fox\<Life
  89. ALT+L
  90. \<File
  91. \<Edit
  92. \<New
  93. \<Save
  94. \<About
  95. \<Exit
  96. DO FILE_GET IN FOXLIFE.PRG
  97. DO PATT_EDIT IN FOXLIFE.PRG
  98. DO INSTRUCT IN FOXLIFE.PRG
  99. DO FILE_SAVE IN FOXLIFE.PRG
  100. DO L_ABOUT IN FOXLIFE.PRG
  101. DO LIFE_EXIT IN FOXLIFE.PRG
  102. num_or_filef
  103. num_or_filef
  104. Cycle: FF
  105.   Cells: 
  106. lastlife.dat
  107. That's all, folks!
  108. P_NUM_OR_FP_CIRCULARP_CHAR
  109. e rGO_FLAG
  110. m.FILE_FLAG
  111. NUM_OR_FILF10
  112. LARG_ROWS
  113. lcuG_COLS
  114. PerACAL
  115. tatisADIS
  116. eraMENU_FLAG
  117. STOP_FLAG
  118. QUIT_FLAG
  119. POPULATIONGENERATIONEROW
  120. d priECOL
  121. LIFEMENU
  122. nLIFEOPTS
  123. vINSTRUCT
  124. LFILL_RAND
  125. PATT_LOAD
  126. CAL_EDGE_CCAL_EDGE_FCAL_CENTERSHOW_EM
  127. PATT_SAVE
  128. MAINMENU
  129.                 The game of Life by John Conroy
  130.       If started with a number, a random pattern starts the game.
  131.       If started with a file name, will load game based on data 
  132.       in that file. 
  133.       F10 will activate the menu.  Hit ESC to bail out.
  134.       Enter number of cells or data file name, or hit CR: 
  135. USER_VAL
  136. FSCR_TEMP
  137. Generating random pattern....
  138. NUMBER
  139. FP_ROWS
  140. RP_COLS
  141. rP_CHAR
  142. LE_FLAG
  143. OR_FILCOL
  144. LARSEED
  145. lcuRNUM
  146. PerPOPULATIONGENERATIONADIS
  147. P_ROWS
  148. FP_COLS
  149. RCROW
  150. rCCOL
  151. m.DNCOL
  152. UPCOL
  153. _FILLFCOL
  154. LARRTCOL
  155. lcuADIS
  156. PerACAL
  157. ATION
  158. P_ROWS
  159. FP_COLS
  160. RCROW
  161. rCCOL
  162. m.DNCOL
  163. UPCOL
  164. _FILLFCOL
  165. LARRTCOL
  166. lcuADIS
  167. PerACAL
  168. ATION:
  169. P_ROWS
  170. FP_COLS
  171. RACAL
  172. rADIS
  173. m.CROW
  174. UPROW
  175. _FILDNROW
  176. LARCCOL
  177. lcuLFCOL
  178. PerRTCOL
  179. TION-
  180. P_ROWS
  181. FP_COLS
  182. RCROW
  183. rCCOL
  184. m.ADIS
  185. UPROW
  186. _FILDNROW
  187. LARLFCOL
  188. lcuRTCOL
  189. PerACAL
  190. P_ROWS
  191. FP_COLS
  192. RP_CHAR
  193. rPOPULATIONCROW
  194. FILCUR_CELL
  195. RACAL
  196. lcuADIS
  197. Error creating file: 
  198. Error closing file: 
  199. SAVEFILE
  200. FP_ROWS
  201. RP_COLS
  202. rHANDLE
  203. IONSROW
  204. FILADIS
  205. New pattern: 
  206. NEWFILE
  207. FPATT_LOAD
  208. G_ROWS
  209. rG_COLS
  210. IONP_CHAR
  211. Reading in row FF
  212. DATA_FILE
  213. P_ROWS
  214. P_COLS
  215. rP_CHAR
  216. IONHANDLE
  217. FILCOL
  218. RCUR_LINE
  219. uPOPULATIONGENERATIONACAL
  220. IONADIS
  221. WIDTH
  222. Pattern editing not completed - any key to continue
  223. Edit mode   Arrow keys move - spacebar toggles - ESC or CR to end
  224. rADIS
  225.        The game of Life         
  226.  Invented by John Conway,  1970 
  227.  FoxPro version by Tom Rombouts 
  228.                                 
  229.   Press any key to continue...  
  230. IDAHO
  231. ABOUT
  232. Save game as: F
  233. SAVEFILE
  234. PATT_SAVE
  235. STOP_FLAG
  236. EXIT_FLAG
  237. Press any key...
  238. OP_FLAG
  239. IT_FLAG
  240. rINSTRUCT
  241. FILL_RAND
  242. CAL_EDGE_C
  243. CAL_EDGE_F
  244. CAL_EDGE_F
  245. CAL_CENTER
  246. SHOW_EM
  247. PATT_SAVE
  248. FILE_GET
  249. PATT_LOAD
  250. PATT_EDIT
  251. L_ABOUT
  252. FILE_SAVE
  253. LIFE_EXIT
  254. SHOW_ACAL
  255. f:\jebwin\vol2num0\foxlife\
  256. GLIDEGUN.DAT
  257. GLID_40.DAT
  258. GLID_5.DAT
  259. FOXLIFE.ART
  260. FOXLIFE.PRG
  261. h:\foxprow\
  262. FOXLIFE.FXP
  263.