Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 19:39 02 May 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : [MMBasic] Game of Life (GOL)

Author Message
twofingers
Guru

Joined: 02/06/2014
Location: Germany
Posts: 1133
Posted: 04:11am 29 Aug 2014
Copy link to clipboard 
Print this post

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!

MichaelEdited by twofingers 2014-08-30
 
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3165
Posted: 01:30pm 29 Aug 2014
Copy link to clipboard 
Print this post

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 Zealand
Posts: 9066
Posted: 03:02pm 29 Aug 2014
Copy link to clipboard 
Print this post

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: Germany
Posts: 1133
Posted: 12:20am 30 Aug 2014
Copy link to clipboard 
Print this post

I have again taken a critical look at the code and a unnecessary subroutine found.
This
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
can be deleted. Its a relict of a earlier version.

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

  Quote  some hardcore mathematics going on in that code

not really, simple counting. Just looks confusing.

Michael


Edited by twofingers 2014-08-31
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 5910
Posted: 11:48am 30 Aug 2014
Copy link to clipboard 
Print this post

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   MMBasic Help
 
halldave

Senior Member

Joined: 04/05/2014
Location: Australia
Posts: 121
Posted: 12:00pm 30 Aug 2014
Copy link to clipboard 
Print this post


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: Australia
Posts: 5019
Posted: 03:20pm 30 Aug 2014
Copy link to clipboard 
Print this post

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 States
Posts: 172
Posted: 04:48pm 30 Aug 2014
Copy link to clipboard 
Print this post

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: Germany
Posts: 1133
Posted: 03:12am 03 Sep 2014
Copy link to clipboard 
Print this post

@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
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024