GW-BASIC, 1086 1035 bytes (tokenised)
In tokenised form, this is 1035 bytes. (The ASCII form is, of course, a bit longer.) You get the tokenised form by using the SAVE"life
command without appending ",a
in the interpreter.
10 DEFINT A-Z:DEF SEG=&HB800:KEY OFF:COLOR 7,0:CLS:DEF FNP(X,Y)=PEEK((((Y+25)MOD 25)*80+((X+80)MOD 80))*2)
20 X=0:Y=0
30 LOCATE Y+1,X+1,1
40 S$=INKEY$:IF S$=""GOTO 40
50 IF S$=CHR$(13)GOTO 150
60 IF S$=" "GOTO 130
70 IF S$=CHR$(0)+CHR$(&H48)THEN Y=(Y-1+25)MOD 25:GOTO 30
80 IF S$=CHR$(0)+CHR$(&H50)THEN Y=(Y+1)MOD 25:GOTO 30
90 IF S$=CHR$(0)+CHR$(&H4B)THEN X=(X-1+80)MOD 80:GOTO 30
100 IF S$=CHR$(0)+CHR$(&H4D)THEN X=(X+1)MOD 80:GOTO 30
110 IF S$="c"THEN CLS:GOTO 20
120 GOTO 40
130 Z=PEEK((Y*80+X)*2):IF Z=42 THEN Z=32ELSE Z=42
140 POKE(Y*80+X)*2,Z:GOTO 40
150 LOCATE 1,1,0:ON KEY(1)GOSUB 320:KEY(1) ON
160 V!=TIMER+.5:FOR Y=0 TO 24:FOR X=0 TO 79:N=0
170 Z=FNP(X-1,Y-1):IF Z=42 OR Z=46 THEN N=N+1
180 Z=FNP(X,Y-1):IF Z=42 OR Z=46 THEN N=N+1
190 Z=FNP(X+1,Y-1):IF Z=42 OR Z=46 THEN N=N+1
200 Z=FNP(X-1,Y):IF Z=42 OR Z=46 THEN N=N+1
210 Z=FNP(X+1,Y):IF Z=42 OR Z=46 THEN N=N+1
220 Z=FNP(X-1,Y+1):IF Z=42 OR Z=46 THEN N=N+1
230 Z=FNP(X,Y+1):IF Z=42 OR Z=46 THEN N=N+1
240 Z=FNP(X+1,Y+1):IF Z=42 OR Z=46 THEN N=N+1
250 Z=PEEK((Y*80+X)*2):IF Z=32 THEN IF N=3 THEN Z=43
260 IF Z=42 THEN IF N<2 OR N>3 THEN Z=46
270 POKE(Y*80+X)*2,Z:NEXT:NEXT:FOR Y=0 TO 24:FOR X=0 TO 79:Z=PEEK((Y*80+X)*2):IF Z=46 THEN Z=32
280 IF Z=43 THEN Z=42
290 POKE(Y*80+X)*2,Z:NEXT:NEXT
300 IF TIMER<V!GOTO 300
310 IF INKEY$=""GOTO 160
320 SYSTEM
This is the maximum-golfed version, but still featureful: upon starting, you get an editor, in which you can move with the cursor keys; space toggles bacteria on/off on the current field, c
clears the screen, Return starts game mode.
Here follows a less-obfuscated version, which also sets an initial game board with two structures (a circulary-rotating thing and a glider):
1000 REM Conway's Game of Life
1001 REM -
1002 REM Copyright (c) 2012 Thorsten "mirabilos" Glaser
1003 REM All rights reserved. Published under The MirOS Licence.
1004 REM -
1005 DEFINT A-Z:DEF SEG=&hB800
1006 KEY OFF:COLOR 7,0:CLS
1007 DEF FNP(X,Y)=PEEK((((Y+25) MOD 25)*80+((X+80) MOD 80))*2)
1010 PRINT "Initial setting mode, press SPACE to toggle, RETURN to continue"
1020 PRINT "Press C to clear the board, R to reset. OK? Press a key then."
1030 WHILE INKEY$="":WEND
1050 CLS
1065 DATA 3,3,4,3,5,3,6,3,7,3,8,3,3,4,4,4,5,4,6,4,7,4,8,4
1066 DATA 10,3,10,4,10,5,10,6,10,7,10,8,11,3,11,4,11,5,11,6,11,7,11,8
1067 DATA 11,10,10,10,9,10,8,10,7,10,6,10,11,11,10,11,9,11,8,11,7,11,6,11
1068 DATA 4,11,4,10,4,9,4,8,4,7,4,6,3,11,3,10,3,9,3,8,3,7,3,6
1069 DATA 21,0,22,1,22,2,21,2,20,2,-1,-1
1070 RESTORE 1065
1080 READ X,Y
1090 IF X=-1 GOTO 1120
1100 POKE (Y*80+X)*2,42
1110 GOTO 1080
1120 X=0:Y=0
1125 LOCATE Y+1,X+1,1
1130 S$=INKEY$
1140 IF S$="" GOTO 1130
1150 IF S$=CHR$(13) GOTO 1804
1160 IF S$=" " GOTO 1240
1170 IF S$=CHR$(0)+CHR$(&h48) THEN Y=(Y-1+25) MOD 25:GOTO 1125
1180 IF S$=CHR$(0)+CHR$(&h50) THEN Y=(Y+1) MOD 25:GOTO 1125
1190 IF S$=CHR$(0)+CHR$(&h4B) THEN X=(X-1+80) MOD 80:GOTO 1125
1200 IF S$=CHR$(0)+CHR$(&h4D) THEN X=(X+1) MOD 80:GOTO 1125
1210 IF S$="c" THEN CLS:GOTO 1120
1220 IF S$="r" GOTO 1050
1225 IF S$=CHR$(27) THEN END
1230 GOTO 1130
1240 Z=PEEK((Y*80+X)*2)
1250 IF Z=42 THEN Z=32 ELSE Z=42
1260 POKE (Y*80+X)*2,Z
1270 GOTO 1130
1804 LOCATE 1,1,0
1900 ON KEY(1) GOSUB 2300
1910 KEY(1) ON
2000 V!=TIMER+.5
2010 FOR Y=0 TO 24
2020 FOR X=0 TO 79
2030 N=0
2040 Z=FNP(X-1,Y-1):IF Z=42 OR Z=46 THEN N=N+1
2050 Z=FNP(X ,Y-1):IF Z=42 OR Z=46 THEN N=N+1
2060 Z=FNP(X+1,Y-1):IF Z=42 OR Z=46 THEN N=N+1
2070 Z=FNP(X-1,Y ):IF Z=42 OR Z=46 THEN N=N+1
2080 Z=FNP(X+1,Y ):IF Z=42 OR Z=46 THEN N=N+1
2090 Z=FNP(X-1,Y+1):IF Z=42 OR Z=46 THEN N=N+1
2100 Z=FNP(X ,Y+1):IF Z=42 OR Z=46 THEN N=N+1
2110 Z=FNP(X+1,Y+1):IF Z=42 OR Z=46 THEN N=N+1
2120 Z=PEEK((Y*80+X)*2)
2130 IF Z=32 THEN IF N=3 THEN Z=43
2140 IF Z=42 THEN IF N<2 OR N>3 THEN Z=46
2150 POKE (Y*80+X)*2,Z
2160 NEXT X
2170 NEXT Y
2200 FOR Y=0 TO 24
2210 FOR X=0 TO 79
2220 Z=PEEK((Y*80+X)*2)
2230 IF Z=46 THEN Z=32
2240 IF Z=43 THEN Z=42
2250 POKE (Y*80+X)*2,Z
2260 NEXT X
2270 NEXT Y
2280 IF TIMER<V! GOTO 2280
2290 IF INKEY$="" GOTO 2000
2300 SYSTEM
I wrote this in 15 minutes while bored and waiting for a friend, who was code-golfing with his “apprentice” for Conway’s Game of Life at the same time.
It functions like this: It immediately uses the 80x25 text mode screen buffer (change the initial DEF SEG
to use &hB000
if you’re on a Hercules graphics card; these settings work with Qemu and (slower) dosbox). An asterisk *
is a bacterium.
It works two-pass: first, birthplaces are marked with +
and death marks its targets with .
. In the second pass, +
and .
are replaced with *
and
, respectively.
The TIMER
thing is to make it wait for half a second after each round, in case your Qemu host is very fast ☺
I’m not hoping for a shortest-wins price here but for a coolness one, especially considering the initial board setup. I’ve also got a version where the game engine was replaced by assembly code, in case you’re interested…
Excellent question! It's a shame I got to read this so late. Well, alas, too late, but anyway, I submitted Mathematica 333 character version with interactive features. I had to submit an entry for the only language with built in
– Vitaliy Kaurov – 2012-07-30T21:20:27.353CellularAutomaton
function ;-) http://wolfram.com/xid/0d6e8l44ia-oecI can use a conway's game of life engine to win this challenge, it technically only takes the input number of cells to run. :) (I'm just kidding though) – KrystosTheOverlord – 2019-02-09T22:44:09.003
8
Previously on Stack Overflow: Code Golf: Conway's Game of Life, and be sure to look at the APL implementation link in the comments.
– dmckee --- ex-moderator kitten – 2011-08-14T03:07:08.3931Ah, I did not see that. But this is slightly different no (save me deleting the work putting the challenge together? – Griffin – 2011-08-14T03:15:49.733
6It's not a problem. Many puzzles already run on Stack Overflow have been done here too, but people will tell you that I am obsessive about linking to similar challenges. – dmckee --- ex-moderator kitten – 2011-08-14T03:17:48.697
@Griffin: You can remove all those
;
before}
s. Alsovar
s can be eliminated at times (if it doesn't break your code). And for one-linefor
s,if
s etc, you can eliminate the{ }
completely:for(...) for(...) dosomething()
. – pimvdb – 2011-08-14T14:21:50.107@pimvdb, cheers, I haven't fully golfed it yet, haven't had the time. just wanted to show that I had a go too, rather than idly setting a challenge. Will golf it to the max soon. – Griffin – 2011-08-14T17:53:00.327
@pimvdb, how's that? :D – Griffin – 2011-08-19T08:27:46.387
@Griffin: Wow, more than 50%! – pimvdb – 2011-08-19T09:31:49.293
@pimvdb, squeezed a bit more out, but I think that may be the limit. I take my hat off to you if you find any more. – Griffin – 2011-08-19T09:36:42.880
You can save a couple more on your inner for loops since the third argument is optional. So for(j=0;j<r;++j) becomes for(j=0;j<r;) and you move the increment operator into the statement: document.getElementById(z+"_"+j).innerHTML=b[z][j++]?"#":"." – migimaru – 2011-08-19T10:01:05.230
Also, since K(z,j) always returns an integer, you can shorten your comparison n<=3&&n>=2:n==3 to n<4&&n>1:n==3. You also use document 3 times, so you can save a bit by making a variable to refer to document instead. – migimaru – 2011-08-19T10:15:46.647
@migimaru, well spotted, thanks – Griffin – 2011-08-19T11:07:30.910
What's the generally accepted rule for generations? Does it have to go until it reaches a stable state, or can it go forever, or for an arbitrary number of generations? – Igby Largeman – 2011-08-22T00:19:56.340