![]() |
Forum Index : Microcontroller and PC projects : [MMBasic] Game of Life (GOL)
Author | Message | ||||
twofingers Guru ![]() Joined: 02/06/2014 Location: GermanyPosts: 1520 |
Hi all. Just for fun and to train my abilities in MMBasic programing, I've written this piece of code. Some guys here probably still remember this The Game Of Life program from the very old days (pre-PC era). In order to increase the speed I use very short variable names and only a few comments. '***************************************
'*************************************** '* MMBasic 4.5 for Maximite/Duinomite '* twofingers at TBS 08-2014 '* No warranty, provided at your own risk. '* This code may be freely distributed without charge '* '* John Conway's '* "Game of Life" '* '* a code example/benchmark '*************************************** '*************************************** Cls '** plz edit field dimensions ** C_x =45 ' entire field width w. border C_y =40 ' entire field height w. border '******************************* C_x1=C_x -1 C_y1=C_y -1 L$=" ":X$="x" Option base 0 Dim w$(C_x,C_y,2) length 1 Dim a1(2),a2(2) t=0 clearworld '***** Plz select: Demo1 '***** or 'Demo2 gen=0:Timer=0:z=0 Line (1*6,1*8)-(C_x*6+6,C_y*8+12),1,BF a1(0)=2:a2(0)=C_x1 a1(1)=C_x1:a2(1)=2 Font #2,,1:fontbreite=12 titel$="The Game of LIFE" Print @((C_x*6/2)-(Len(titel$)/2*fontbreite),50) Titel$ Font #1 wait "Press any key!" '********** Mainloop *************** Do t1=Abs(t=0) println(5,C_y+3,Str$(t)+" "+Str$(t1)+" gen:"+Str$(gen)+" tm:"+Str$(Timer)) Timer=0 For x=a1(t) To a2(t) Step 1-t*2 Line (x*6,1*8+8)-(x*6+5,C_y*8+4),0,BF For y=2 To C_y1 c=n(x,y) a$=w$(x,y,t) If a$<>X$ Then If c=3 Then w$(x,y,t1)=X$ Else plotxy (x,y) If c=2 Or c=3 Then w$(x,y,t1)=X$ EndIf Next y Next x clearworld t=not t '0,1,0,1,0,... gen=gen+1 z=z+Timer println(25,C_y+3,"avg:"+Str$(Int(z*10/gen)/10)) Loop '********** END Mainloop ************* '* counting neighbors Function n (x,y) If w$(x-1,y,t)=X$ Then n=n+1 If w$(x+1,y,t)=X$ Then n=n+1 If w$(x-1,y-1,t)=X$ Then n=n+1 If w$(x,y-1,t)=X$ Then n=n+1 If w$(x+1,y-1,t)=X$ Then n=n+1 If w$(x-1,y+1,t)=X$ Then n=n+1 If w$(x,y+1,t)=X$ Then n=n+1 If w$(x+1,y+1,t)=X$ Then n=n+1 End Function Sub clearworld For x = 2 To C_x1 For y = 2 To C_y1 w$(x,y,t)=L$ Next Next End Sub Sub plotworld For x = 2 To C_x1 For y = 2 To C_y1 println (x,y,w$(x,y,t)) Next Next End Sub Sub println (x,y,s$) Print @(x*6,y*8) s$ End Sub Sub plotxy (x,y) Circle (x*6+3,y*8+4),2,F End Sub **** initial filling **** Sub Demo1 For x = 2 To C_x1 For y = 2 To C_y1 r=Rnd(randomize) If r<0.20 Then w$(x,y,t)=X$ EndIf Next Next End Sub Sub Demo2 ' Pentadecathlon, a period 15 oscillator ' (here you can create your own pattern) '1 2 3 4 5 6 7 8 9 10 11 Data "x","x","x","x","x","x","x","x" Data "x"," ","x","x","x","x"," ","x" Data "x","x","x","x","x","x","x","x" For y = 10 To 12 For x = 9 To 16 Read w$(x,y,t) Next Next End Sub Sub wait prompt$ If prompt$<>"" Then Print @((MM.HRes-Len(prompt$))/2,MM.VRes-12) prompt$ Do While Inkey$="":Loop If prompt$<>"" Then Print @((MM.HRes-Len(prompt$))/2,MM.VRes-12) String$(Len(prompt$)," ") End Sub Download: 2014-08-29_140916_TBS_LIFE.zip Always look on the bright side of life! ![]() Michael causality ≠ correlation ≠ coincidence |
||||
Geoffg![]() Guru ![]() Joined: 06/06/2011 Location: AustraliaPosts: 3267 |
Neat, I remember this program (it was a loooooong time ago). It was addictive watching the little creatures move around. Geoff Geoff Graham - http://geoffg.net |
||||
Grogster![]() Admin Group ![]() Joined: 31/12/2012 Location: New ZealandPosts: 9462 |
I thought you had written the board-game! ![]() Still very impressive though - some hardcore mathematics going on in that code. Smoke makes things work. When the smoke gets out, it stops! |
||||
twofingers Guru ![]() Joined: 02/06/2014 Location: GermanyPosts: 1520 |
I have again taken a critical look at the code and a unnecessary subroutine found. This Sub plotworld
can be deleted. Its a relict of a earlier version.
For x = 2 To C_x1 For y = 2 To C_y1 println (x,y,w$(x,y,t)) Next Next End Sub Download: 2014-08-30_101326_TBS_Life_v1...01.zip /////////////////////////////////////////////////////////// @Geoff I thought you were much younger! ![]() ![]() edit: And of course, THANK YOU! that you gave us MMBasic. ![]() @Grogster not really, simple counting. Just looks confusing. ![]() Michael ![]() ![]() ![]() ![]() ![]() ![]() ![]() causality ≠ correlation ≠ coincidence |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6212 |
I programmed Game of Life for my Microbee Z80 30+ years ago. While playing around with a RaspberryPi a week ago, I saw an example program of Game of Life with the display a LED matrix. Your 'mite version brings back many memories. Jim VK7JH MMedit |
||||
halldave![]() Senior Member ![]() Joined: 04/05/2014 Location: AustraliaPosts: 121 |
if anyone's interested I have a collection of untested uncovered GWBasic programs of some of the classic games for you to tinker and play with http://mmreference.com/unconverted-gwbasic-programs/ |
||||
Gizmo![]() Admin Group ![]() Joined: 05/06/2004 Location: AustraliaPosts: 5107 |
I remember typing in a Life clone into a TRS80 back at high school, it was one of the first programs I entered. It was only on a 128 X 64 grid, and pretty clunky. Glenn The best time to plant a tree was twenty years ago, the second best time is right now. JAQ |
||||
Oldbitcollector![]() Senior Member ![]() Joined: 16/05/2014 Location: United StatesPosts: 172 |
twofingers, Thanks for this.. I did a quick conversion of your code this evening to our project as well. Very cool! Jeff My Propeller/Micromite mini-computer project. |
||||
twofingers Guru ![]() Joined: 02/06/2014 Location: GermanyPosts: 1520 |
@halldave Yesterday I looked at your website. I like them, good job! I would be glad if you place my code on your site. Maybe you could expand one day your site to a technical reference? A (comparison) table of technical data, connectors and limits of all MITES (including known bugs)? That could enhance the benefits. If Geoff does not mind. Maybe something like that exist already, but I did not find it. Michael causality ≠ correlation ≠ coincidence |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |