home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / games / vmsnet.sources.games / life / part01 next >
Internet Message Format  |  1992-03-24  |  16KB

  1. Path: uunet!mcsun!chsun!pegasus.ch!superuser
  2. From: superuser@pegasus.ch
  3. Newsgroups: vmsnet.sources.games
  4. Subject: Game of Life 1/2
  5. Message-ID: <1992Mar23.223316.32@pegasus.ch>
  6. Date: 23 Mar 92 21:33:16 GMT
  7. Organization: Pegasus BBS, Switzerland
  8. Lines: 438
  9.  
  10.  
  11. Game of Life -- Read the docs for more information
  12.  
  13. Feel free to call our BBS to get in touch with more 'funtastic'
  14. VAX/VMS Games.
  15.  
  16. -- 
  17.  Internet: Superuser@pegasus.ch
  18.       FAX: ++41 [0]71 713837
  19.  
  20.   PEGASUS: Sysop of PEGASUS BBS     Call: ++41 [0]71 715577  >  27+ Lines
  21.               ( PEGASUS.CH )        X.25: +228 4752 1 2574   >  ChatSystem
  22.  
  23.  VAX/VMS is like sex - if you've tried it, you can't get along without it,
  24.     if you haven't you really have no idea what the fuss is about.
  25.     
  26. $! ................... Cut between dotted lines and save. ...................
  27. $!...........................................................................
  28. $! VAX/VMS archive file created by VMS_SHARE V06.10 7-FEB-1989.
  29. $!
  30. $! VMS_SHARE was written by James Gray (Gray:OSBUSouth@Xerox.COM) from
  31. $! VMS_SHAR by Michael Bednarek (U3369429@ucsvc.dn.mu.oz.au).
  32. $!
  33. $! To unpack, simply save, concatinate all parts into one file and
  34. $! execute (@) that file.
  35. $!
  36. $! This archive was created by user SUPERUSER
  37. $! on 23-MAR-1992 22:13:17.88.
  38. $!
  39. $! ATTENTION: To keep each article below 31 blocks (15872 bytes), this
  40. $!            program has been transmitted in 2 parts.  You should
  41. $!            concatenate ALL parts to ONE file and execute (@) that file.
  42. $!
  43. $! It contains the following 7 files:
  44. $!        AAAREADME.LIS
  45. $!        FUTURE_FEATURES.LIS
  46. $!        GETCHA.C
  47. $!        LIFE.C
  48. $!        LIFE.WORK
  49. $!        PATTERNS.LIS
  50. $!        SPARE_LIFE.C
  51. $!
  52. $!============================================================================
  53. $ SET SYMBOL/SCOPE=( NOLOCAL, NOGLOBAL )
  54. $ VERSION = F$GETSYI( "VERSION" )
  55. $ IF VERSION .GES "V4.4" THEN GOTO VERSION_OK
  56. $ WRITE SYS$OUTPUT "You are running VMS ''VERSION'; ", -
  57.     "VMS_SHARE V06.10 7-FEB-1989 requires VMS V4.4 or higher."
  58. $ EXIT 44 ! SS$_ABORT
  59. $VERSION_OK:
  60. $ GOTO START
  61. $!
  62. $UNPACK_FILE:
  63. $ WRITE SYS$OUTPUT "Creating ''FILE_IS'"
  64. $ DEFINE/USER_MODE SYS$OUTPUT NL:
  65. $ EDIT/TPU/COMMAND=SYS$INPUT/NODISPLAY/OUTPUT='FILE_IS'/NOSECTION -
  66.     VMS_SHARE_DUMMY.DUMMY
  67. b_part := CREATE_BUFFER( "{Part}", GET_INFO( COMMAND_LINE, "file_name" ) )
  68. ; s_file_spec := GET_INFO( COMMAND_LINE, "output_file" ); SET( OUTPUT_FILE
  69. , b_part, s_file_spec ); b_errors := CREATE_BUFFER( "{Errors}" ); i_errors 
  70. := 0; pat_beg_1 := ANCHOR & "-+-+-+ Beginning"; pat_beg_2 := LINE_BEGIN 
  71. & "+-+-+-+ Beginning"; pat_end := ANCHOR & "+-+-+-+-+ End"; POSITION
  72. ( BEGINNING_OF( b_part ) ); LOOP EXITIF SEARCH( SPAN( ' ' )@r_trail 
  73. & LINE_END, FORWARD) = 0; POSITION( r_trail ); ERASE( r_trail ); ENDLOOP
  74. ; POSITION( BEGINNING_OF( b_part ) ); i_append_line := 0; LOOP EXITIF MARK
  75. ( NONE ) = END_OF( b_part ); s_x := ERASE_CHARACTER( 1 )
  76. ; IF s_x = '+' THEN r_skip := SEARCH( pat_beg_1, FORWARD, EXACT ); IF r_skip 
  77. <> 0 THEN s_x := ''; MOVE_HORIZONTAL( -CURRENT_OFFSET ); ERASE_LINE; ENDIF
  78. ; ENDIF; IF s_x = '-' THEN r_skip := SEARCH( pat_end, FORWARD, EXACT )
  79. ; IF r_skip <> 0 THEN s_x := ''; MOVE_HORIZONTAL( -CURRENT_OFFSET ); m_skip 
  80. := MARK( NONE ); r_skip := SEARCH( pat_beg_2, FORWARD, EXACT ); IF r_skip 
  81. <> 0 THEN POSITION( END_OF( r_skip ) ); MOVE_HORIZONTAL( -CURRENT_OFFSET )
  82. ; MOVE_VERTICAL( 1 ); MOVE_HORIZONTAL( -1 ); ELSE POSITION( END_OF( b_part ) 
  83. ); ENDIF; ERASE( CREATE_RANGE( m_skip, MARK( NONE ), NONE ) ); ENDIF; ENDIF
  84. ; IF s_x = 'V' THEN s_x := ''; IF i_append_line <> 0 THEN APPEND_LINE
  85. ; MOVE_HORIZONTAL( -CURRENT_OFFSET ); ENDIF; i_append_line := 1
  86. ; MOVE_VERTICAL( 1 ); ENDIF; IF s_x = 'X' THEN s_x := ''; IF i_append_line 
  87. <> 0 THEN APPEND_LINE; MOVE_HORIZONTAL( -CURRENT_OFFSET ); ENDIF
  88. ; i_append_line := 0; MOVE_VERTICAL( 1 ); ENDIF; IF s_x <> '' THEN i_errors 
  89. := i_errors + 1; s_text := CURRENT_LINE; POSITION( b_errors ); COPY_TEXT
  90. ( "The following line could not be unpacked properly:" ); SPLIT_LINE
  91. ; COPY_TEXT( s_x ); COPY_TEXT( s_text ); POSITION( b_part ); MOVE_VERTICAL
  92. ( 1 ); ENDIF; ENDLOOP; POSITION( BEGINNING_OF( b_part ) ); LOOP r_x := SEARCH
  93. ( "`", FORWARD, EXACT ); EXITIF r_x = 0; POSITION( r_x ); ERASE_CHARACTER( 1 
  94. ); COPY_TEXT( ASCII( INT( ERASE_CHARACTER( 3 ) ) ) ); ENDLOOP
  95. ; IF i_errors = 0 THEN SET( NO_WRITE, b_errors, ON ); ELSE POSITION
  96. ( BEGINNING_OF( b_errors ) ); COPY_TEXT( FAO
  97. ( "The following !UL errors were detected while unpacking !AS", i_errors
  98. , s_file_spec ) ); SPLIT_LINE; SET( OUTPUT_FILE, b_errors, "SYS$COMMAND" )
  99. ; ENDIF; EXIT; 
  100. $ DELETE VMS_SHARE_DUMMY.DUMMY;*
  101. $ CHECKSUM 'FILE_IS
  102. $ WRITE SYS$OUTPUT " CHECKSUM ", -
  103.   F$ELEMENT( CHECKSUM_IS .EQ. CHECKSUM$CHECKSUM, ",", "failed!!,passed." )
  104. $ RETURN
  105. $!
  106. $START: 
  107. $ FILE_IS = "AAAREADME.LIS"
  108. $ CHECKSUM_IS = 1999775588
  109. $ COPY SYS$INPUT VMS_SHARE_DUMMY.DUMMY
  110. X`009`009`009`009LIFE
  111. X`009`009`009  By Mark Randall
  112. X
  113. XAAAREADME.LIS`009`009This file
  114. XFUTURE_FEATURES.LIS`009Features to be implemented in the (near?) future
  115. XGETCHA.C`009`009Single character input without a CR
  116. XLIFE.C`009`009`009Source code
  117. XLIFE.EXE`009`009Executable
  118. XLIFE.WORK`009`009Notes on functionality - see explanation below
  119. XPATTERNS.LIS`009`009Suggested patterns to try
  120. XSPARE_LIFE.C`009`009Attempt at different implemetation of life()
  121. X
  122. XABSTRACT
  123. X--------
  124. X`009The computer game of life has been famous ever since its introduction
  125. Xin Scientific American and Byte. Its concept is simple enough so that anyone
  126. Vcan use it, yet the patterns can get amazingly complex within very little tim
  127. Xe.
  128. XIt works like this: there is a large grid on which there are cells, either
  129. Xalive or dead. Over time, the grid changes, bringing new cells to life or
  130. Xkilling old cells. Each increment of time is called a generation. There are 3
  131. Vbasic rules which determine whether any given cell will be alive or dead in t
  132. Xhe
  133. Xnext generation:
  134. X
  135. X`0091. Each living cell that has exactly 2 or 3 living neighbors will be
  136. X`009   alive in the next generation.
  137. X
  138. V`0092. Each living cell that has other than 2 or 3 living neighbors will be`0
  139. X32
  140. X`009   dead in the next generation.
  141. X
  142. X`0093. If a cell that is currently not living (dead) has exactly 3 living
  143. X`009   neighbors, it will be alive in the next generation.
  144. X
  145. X`009Different patterns will produce drastically different results -
  146. Vsometimes one cell can make all the difference. Since the introduction of lif
  147. Xe
  148. Vin 1970, many different basic patterns have been discovered - see patterns.li
  149. Xs
  150. Vfor some patterns to try. Some patterns last forever, while some die out afte
  151. Xr
  152. Va few generations. Several will change for hundreds of generations, and final
  153. Xly
  154. Vreduce themseleves to small, basic patterns that either don't change or repea
  155. Xt
  156. Vevery 2 or 3 generations. See article list for articles in magazines that hav
  157. Xe
  158. Xdetails on various developments of life.
  159. X
  160. X`009Using the program is pretty simple. In the beginning, a grid is drawn
  161. Von the screen using periods. You can manipulate the cursor around the grid wi
  162. Xth
  163. Vthe numeric keypad or VTxxx series arrow keys. A cell, 'o', is either produce
  164. Xd
  165. Xor erased by hitting the 5 key. The cursor manipulation and cell placement is
  166. Xdone with escape sequences, so the output should work on any VTxxx or
  167. Xcompatible.
  168. X
  169. X`009The program itself is written in C. Please feel free and encouraged to
  170. Xmodify and improve it. If you do anything to drastically improve it, please
  171. XEMAIL it to me, and I'll try to make the improvement public. The cell storage
  172. Vis grossly memory-inefficient, but it was done for simplicity. Most all of th
  173. Xe
  174. Xcode is standard C, and should be portable to Unix with few modifications.
  175. XAgain, I'd appreciate it if you passed along such versions to me. The last
  176. Xitems of importance: the terminal width and hight can be changed within the
  177. Xsource code by changing the constants HIGHT and WIDTH. The character for a
  178. Xliving cell is currently hard-coded into the program, but it can be easily
  179. Vchanged given the brevity of the program itself at this stage. This program i
  180. Xs
  181. Vpublic-domain, and may be copied anywhere. Please keep my name as the origina
  182. Xl
  183. Xauthor, though.
  184. X
  185. X`009`009`009Enjoy!
  186. X`009`009`009Mark Randall
  187. X`009`009`009CI$ user id: [70471,2760]
  188. X
  189. XKNOWN BUGS
  190. X----------
  191. X`009First and foremost - there's no good way to stop the processing of the
  192. Xgenerations. If this were DOS (Ugh! Cough! YUK!), this would be easy - the
  193. Xscanf function would WORK RIGHT! Unfortunatly, it doesn't, so the best way to
  194. Vdate is a CTRL-Y. The processing for the corners of the grid doesn't work qui
  195. Xte
  196. Xright, either.
  197. X
  198. XARTICLES
  199. X--------
  200. X`009Several articles have been written on the game of life. Here's a list
  201. Xof the ones I know about:
  202. X
  203. X`009Scientific American:
  204. X`009`00910/70`009Original Article by John Horton Conway
  205. X`009`00911/70
  206. X`009`00901/71
  207. X`009`00902/71
  208. X`009`00903/71
  209. X`009`00904/71
  210. X`009`00911/71
  211. X`009`00901/72
  212. X`009Byte:
  213. X`009`00909/75
  214. X`009`00910/75
  215. X`009`00912/75
  216. X`009`00901/76
  217. X`009`00912/78
  218. X`009`00901/79
  219. X
  220. XLIFE.WORK
  221. X---------
  222. X`009This is just a text file that shows how to check the eight positions
  223. Varound a given cell in the middle, on the borders, or in the corners. It may
  224. X be
  225. Xhelpful in changing/debugging the source code. Set your term to 132 cols to
  226. Xdisplay.
  227. X
  228. XTO BUILD
  229. X--------
  230. X`009The building of Life is rediculously simple. Follow these commands:
  231. X
  232. X   $ cc life
  233. X   $ cc getcha
  234. X   $ link life,getcha
  235. X
  236. X`009and finally:
  237. X
  238. X   $ run life
  239. X
  240. XVARIATIONS
  241. X----------
  242. X`009I know there are variations of Life out there - I'm looking for them.
  243. XOne-dimensional Life is pretty easy, and I may write some code for it one of
  244. Vthese days. 3D Life is where a great deal of my interest lies, but I'm baffle
  245. Xd
  246. Xas to how to represent it on a screen, especially a character-based VTxxx.
  247. XThere are also other rule sets, but I have yet to ever lay eyes on them. I'd
  248. Xlove to create a DEC/X Windows interface for the game, but it'll be a while
  249. Xbefore I get to that. See future_features.lis for my current ambitions.
  250. X
  251. $ GOSUB UNPACK_FILE
  252.  
  253. $ FILE_IS = "FUTURE_FEATURES.LIS"
  254. $ CHECKSUM_IS = 878720711
  255. $ COPY SYS$INPUT VMS_SHARE_DUMMY.DUMMY
  256. X3d life
  257. Xdecwindows interface
  258. Xget out of infinite loop
  259. Xrandom cells/patterns
  260. Xrecall original pattern and edit it`032
  261. Xcheck for repetition n levels deep
  262. Xdisk io
  263. $ GOSUB UNPACK_FILE
  264.  
  265. $ FILE_IS = "GETCHA.C"
  266. $ CHECKSUM_IS = 624702643
  267. $ COPY SYS$INPUT VMS_SHARE_DUMMY.DUMMY
  268. X/*
  269. X`009getcha.c
  270. X`009get character from kb w/out echo (no CR)
  271. X*/
  272. X
  273. X#include ssdef
  274. X#include descrip
  275. X#include iodef
  276. Xint channel;
  277. Xchar bufaddr;
  278. X$DESCRIPTOR(DEVDSC,"TT:");
  279. Xgetcha()
  280. X`123
  281. Xshort iosb[4];
  282. Xint stat;
  283. X`009bufaddr=0;
  284. X`009stat = sys$assign(&DEVDSC,&channel,0,0);
  285. X`009if (stat != SS$_NORMAL) lib$stop(stat);
  286. X`009stat = sys$qiow(0,channel, IO$_READVBLK`124IO$M_NOECHO,&iosb,0,0,
  287. X`009`009&bufaddr,1,0,0,0,0);
  288. X`009stat = sys$dassgn(channel);
  289. X`009return(bufaddr);
  290. X`125
  291. $ GOSUB UNPACK_FILE
  292.  
  293. $ FILE_IS = "LIFE.C"
  294. $ CHECKSUM_IS = 847235763
  295. $ COPY SYS$INPUT VMS_SHARE_DUMMY.DUMMY
  296. X/*`009`009`009GAME OF LIFE
  297. X`009`009`009VAX C VERSION
  298. X`009`009`009BY MARK RANDALL
  299. X*/
  300. X
  301. X#include stdio
  302. X#include ctype
  303. X#define width 80
  304. X#define hight 40
  305. X#define esc '\33'
  306. X
  307. Xint `009screen[width+1][hight+1];
  308. Xint `009memory[width+1][hight+1];
  309. Xint `009cells_alive;
  310. Xint `009x,y;
  311. Xint`009charin;
  312. V/* ------------------------------------------------------------------------ *
  313. X/
  314. X
  315. Xmain()
  316. X`123
  317. X`009int generation;
  318. X`009for ( ; ; ) `123
  319. X`009    generation=0; cells_alive=1;
  320. X`009    drawscreen();
  321. X`009    input();
  322. X`009    post_input();
  323. X
  324. X`009    while (cells_alive != 0) `123
  325. X`009    `009printf("%c[%d;1fGeneration #%d", esc, hight+2, generation++);
  326. X`009    `009life();
  327. X
  328. X`009`009if (cells_alive==0) `123
  329. X`009`009    printf("%c[%d;1f     Last generation. ",esc,hight+3);
  330. X`009`009    printf("<<Enter>> to continue, E to end :");
  331. X`009`009    charin=getcha();
  332. X`009`009    if (charin>90) charin -= 32;
  333. X`009`009    if (charin==69) `123 printf("%c[2J%c[;f",esc,esc); exit(); `125
  334. X`009`009`125
  335. X`009    `125
  336. X`009`125
  337. X`125
  338. X
  339. Xdrawscreen()
  340. X`123
  341. Xchar incstr[width];
  342. X`009for (y=1; y<hight+1; y++) `123
  343. X`009    for (x=1; x<width+1; x++)
  344. X`009`009screen[x][y]=memory[x][y]=0;
  345. X`009`125
  346. X`009printf ("%c[2J%c[H",esc,esc);`009/* clear screen */
  347. X`009for(x=0;x<width;x++) strcat(incstr,".");
  348. X`009for(x=0;x<hight;x++) printf("%s\n",incstr);
  349. X`009for(x=0;x<width;x++)    incstr[x] = '\0';
  350. X`009for(x=0;x<width;x++) strcat(incstr,"-");
  351. X`009printf("%s\n",incstr);
  352. X`125
  353. X
  354. Xinput()
  355. X`123
  356. X`009printf("%c[%d;f",esc,hight+2);
  357. X`009printf("Use cursor keys or numeric keypad to move cursor.\n");
  358. X`009printf("Use period or 5 key to place/remove cell,");
  359. X`009printf(" zero or Enter to begin processing.");
  360. X`009x=1; y=1;`009`009/* upper left position */
  361. X`009printf("%c[1;1f",esc);  /* put the cursor there */
  362. X    for ( ; ; ) `123`009`009/* infinite loop */
  363. X`009charin=getcha();`009/* get ascii code from getcha */
  364. X`009if (charin==27) `123`009/* if char=esc, */
  365. X`009    charin=getcha();`009/* get the bracket */
  366. X`009    charin=getcha();`009/* then get the wanted char */
  367. X`009`125
  368. X`009switch(charin) `123`009/* move cursor, change index, flip dot */
  369. X`009    case 65:
  370. X`009    case 56:`009if (y!=1)       `123 printf("%c[A",esc); y--; `125 break;
  371. X`009    case 66:
  372. X`009    case 50:`009if (y!=hight)   `123 printf("%c[B",esc); y++; `125 break;
  373. X`009    case 67:
  374. X`009    case 54:`009if (x!=width)   `123 printf("%c[C",esc); x++; `125 break;
  375. X`009    case 68:
  376. X`009    case 52:`009if (x!=1)       `123 printf("%c[D",esc); x--; `125 break;
  377. X`009    case 55:`009if ((y!=1) & (x!=1)) `123
  378. X`009`009`009    printf("%c[A%c[D",esc,esc);  y--; x--; `125 break;
  379. X`009    case 51:`009if ((y!=hight) & (x!=width)) `123
  380. X`009`009`009    printf("%c[B%c[C",esc,esc);  y++; x++; `125 break;
  381. X`009    case 49:`009if ((x!=1) & (y!=hight)) `123
  382. X`009`009`009    printf("%c[B%c[D",esc,esc);  y++; x--; `125 break;
  383. X`009    case 57:`009if ((y!=1) & (x!=width)) `123
  384. X`009`009`009    printf("%c[A%c[C",esc,esc);  y--; x++; `125 break;
  385. X`009    case 46:
  386. X`009    case 53:`009changedot();    break;  /* period = switch dot */
  387. X`009    case 48:
  388. X`009    case 13:`009return;                 /* zero = end */
  389. X`009`125
  390. X    `125
  391. X`125
  392. X
  393. Xchangedot()
  394. X`123
  395. X`009if (screen[x][y]==1) `123`032
  396. X`009    screen[x][y]=0;
  397. X`009    printf(".%c[D",esc);
  398. X`009`125
  399. X`009else `123
  400. X`009    screen[x][y]=1;
  401. X`009    printf("o%c[D",esc);
  402. X`009`125
  403. X`125
  404. X
  405. Xpost_input()
  406. X`123
  407. X`009char incstr[width];
  408. X`009for(x=0;x<width;x++) incstr[x] = '\0';
  409. X`009printf("%c[1;1f",esc);
  410. X`009for (y=1; y<hight+1; y++) `123
  411. X`009    for (x=1; x<width+1; x++) `123
  412. X`009`009if (screen[x][y]==1) strcat(incstr,"o");
  413. X`009`009else strcat(incstr," ");
  414. X`009    `125
  415. X`009    printf("%s\n",incstr);
  416. X`009    for(x=0;x<width;x++)    incstr[x] = '\0';
  417. X`009`125
  418. X`009printf("%c[%d;f",esc,hight+2);
  419. X`009for(x=0;x<width;x++) incstr[x] = '\0';
  420. X`009for(x=0;x<width;x++) strcat(incstr," ");
  421. X`009printf("%s\n",incstr);
  422. X`009printf("%s\n",incstr);
  423. X`125
  424. X
  425. Xlife()
  426. X`123
  427. X`009int livecells=0;
  428. X`009/* general area */
  429. X`009for (y=2; y<hight; y++) for (x=2; x<width; x++) `123
  430. X`009`009if (memory[x+1][y]==1)`009`009livecells++;
  431. X`009`009if (memory[x-1][y]==1)`009`009livecells++;
  432. X`009`009if (memory[x][y+1]==1)`009`009livecells++;
  433. X`009`009if (memory[x][y-1]==1)`009`009livecells++;
  434. X`009`009if (memory[x-1][y-1]==1)`009livecells++;
  435. X`009`009if (memory[x+1][y-1]==1)`009livecells++;
  436. X`009`009if (memory[x-1][y+1]==1)`009livecells++;
  437. X`009`009if (memory[x+1][y+1]==1)`009livecells++;
  438. X`009`009livecells=lifeloop(livecells);
  439. X`009`125
  440. X
  441. X`009/* upper left corner */
  442. X`009x=1; y=1;
  443. X`009if (memory[2][1]==1)`009`009livecells++;
  444. X`009if (memory[1][2]==1)`009`009livecells++;
  445. X`009if (memory[width][1]==1)`009livecells++;
  446. X`009if (memory[1][hight]==1)`009livecells++;
  447. -+-+-+-+-+ End of part 1 +-+-+-+-+-
  448.