Showing posts with label programs. Show all posts
Showing posts with label programs. Show all posts
Tuesday, September 4, 2012
Lotto Number Picker
There is something to be said for a well written plan. It's easy to follow through. OTOH, there is more than one way to skin a cat, and there's more than one way to code a program.
This Petit Computer program was done while docked at King Sooper, waiting to be unloaded. It's very easy to do, and it is proof that computer programming can be convenient, fun, and with potential of great reward!
Look at the algorithm and see if you can follow the code! Feel free to modify to suit!
'LOTTO NUMBER PICKER
'THE ALGORITHM:
'1. TITLE AND INITIALIZATIONS
'2. FILL BALL ARRAY 1 TO MAXNUM
'3. RANDOMIZE BALL ARRAY
'4. FOR I=1 TO NUMBER OF BALLS
'4.1 IF BALL NUMBER < 10, ADD LEADING ZERO
'4.2 APPEND TO TICKET ARRAY STRING
'5. REPEAT STEPS 3-4 UNTIL NUMBER OF TICKETS
'6. FOR I=1 TO NUMBER OF TICKETS
'6.1 PRINT TICKET$ ARRAY. ONE PER LINE
'7. PRESS ANY KEY TO CONTINUE. BACK TO TOP
@STEP1
CLS:CLEAR:COLOR 2
?"LOTTO GENERATOR"
?"(C) 2012 Harry M. Hardjono"
MN=42:'MAXNUM 1-42
NB=6:'NUMBER OF BALLS
NT=20:'NUMBER OF TICKETS
@STEP2
@STEP3
@STEP4
@STEP5
REM THEN A MIRACLE OCCURS...
@STEP6
FOR I=0 TO NB*NT-1:C$=CHR$(RND(MN)+101)
IF !(I%NB) THEN LOCATE 0,I/NB+3:S$="":COLOR I/NB/3%2+4
IF 0>INSTR(S$,C$) THEN ?RIGHT$(STR$(ASC(C$)),2);" ";:S$=S$+C$ ELSE I=I-1
NEXT
@STEP7
FOR I=0 TO 1:I=TCHST OR BUTTON(0):NEXT
GOTO @STEP1
Tuesday, April 10, 2012
SmallBasic Turtle Logo Source Code 3
This is my polished version of Logo Turtle example code implementation in Small Basic. I hope you can easily understand the code. The code is optimized for simplicity, not execution speed. I will be bringing you more examples as time goes by. I don't really teach, but hopefully, by showing you the source codes, this can be a self-guided tutorial for you.
'Turtle Logo in Small Basic - NMC672
'by Harry Hardjono
'Turtle angle speed x1 y1 x0 y0
'Turtle move moveto penup pendown
'Turtle turn turnleft turnright
'Turtle show hide
'
' I hope this program is a good tutorial on how you can do your own programs!
GraphicsWindow.Show()
GraphicsWindow.Clear()
'----------------------------------------------
' Example Turtle Operation Code
'----------------------------------------------
cmd1="init"
Toitle()
cmd1="size"
cmd2=10
Toitle()
Program.Delay(1000)
cmd1="color"
cmd2="green"
Toitle()
Program.Delay(1000)
cmd1="color"
cmd2="red"
Toitle()
Program.Delay(1000)
cmd1="size"
cmd2=4
Toitle()
Program.Delay(1000)
cmd1="size"
cmd2=8
Toitle()
For loop=0 to 360 Step 20
cmd1="moveto"
cmd2=sx/2
cmd3=sy/2
Toitle()
cmd1="turn"
cmd2=loop
Toitle()
cmd1="move"
cmd2=200
Toitle()
Program.Delay(1000)
endfor
beginloop:
GraphicsWindow.Clear()
cmd1="init"
Toitle()
Program.Delay(2000)
cmd1="show"
Toitle()
For loop=1 to 50
cmd1="penup"
Toitle()
cmd1="moveto"
cmd2=Math.GetRandomNumber(sx)
cmd3=Math.GetRandomNumber(sy)
Toitle()
cmd1="pendown"
Toitle()
cmd1="color"
cmd2=GraphicsWindow.GetRandomColor()
Toitle()
cmd1="size"
cmd2=Math.GetRandomNumber(10)
Toitle()
For loop2=1 to Math.GetRandomNumber(6)
cmd1="turn"
cmd2=Math.GetRandomNumber(360)
Toitle()
cmd1="move"
cmd2=Math.GetRandomNumber(1000)
Toitle()
endfor
endfor
Program.Delay(15000)
Goto beginloop
'----------------------------------------------
' Main Turtle function.
'----------------------------------------------
Sub Toitle
'cmd1=command (input)
'cmd2=param1 (input)
'cmd3=param2 (input)
If (cmd1="init") then
sy=GraphicsWindow.Height
sx=GraphicsWindow.Width
x0=0
y0=0
x1=sx/2
y1=sy/2
px=x1
py=y1
p=1
angle=180
col="blue"
speed=100
siz=1
ts=1
DrawTurtle()
elseif (cmd1="penup") then
p=0
elseif (cmd1="pendown") then
p=1
elseif (cmd1="move") then
'forward
x0=x1
y0=y1
x1=x0+cmd2*Math.Sin(Math.GetRadians(angle))
y1=y0+cmd2*Math.Cos(Math.GetRadians(angle))
draw()
elseif (cmd1="moveto") then
x0=x1
y0=y1
x1=cmd2
y1=cmd3
draw()
elseif (cmd1="turn") then
angle=cmd2
DrawTurtle()
elseif (cmd1="turnleft") then
angle=angle+cmd2
DrawTurtle()
elseif (cmd1="turnright") then
angle=angle-cmd2
DrawTurtle()
elseif (cmd1="color") then
col=cmd2
DrawTurtle()
elseif (cmd1="size") then
siz=math.Max(1,cmd2)
siz=math.Min(10,siz)
DrawTurtle()
elseif (cmd1="speed") then
speed=cmd2
speed=Math.Max(0,speed)
speed=Math.Min(100,speed)
elseif (cmd1="show") then
ts=1
elseif (cmd1="hide") then
ts=0
endif
If (ts=1) then
MoveTurtle()
ShowTurtle()
Else
HideTurtle()
endif
endsub
'Draw
Sub Draw
If (p>0) then
If (x0<>x1 or y0<>y1) then
m=math.Max(math.Abs(x1-x0),math.Abs(y1-y0))
map_var["mx1"]=0
map_var["mx3"]=m
For i=0 To m
map_var["my1"]=x0
map_var["my3"]=x1
map_var["mx2"]=i
map()
px=map_var["my2"]
map_var["my1"]=y0
map_var["my3"]=y1
map_var["mx2"]=i
map()
py=map_var["my2"]
RemBugFix()
px=Math.Remainder(px,sx)
py=Math.Remainder(py,sy)
GraphicsWindow.BrushColor=col
GraphicsWindow.FillEllipse(px,py,siz,siz)
MoveTurtle()
If (speed<100) then
Program.Delay(100-speed)
endif
EndFor
endif
endif
'These really need to be Mod
'Seems buggy - doesn't work with negative number?
RemBugFix2()
x1=Math.Remainder(x1,sx)
y1=Math.Remainder(y1,sy)
angle=Math.Remainder(angle,360)
endsub
'----------------------------------------------
'map function
'----------------------------------------------
Sub map
'x1-x2-x3 y1-y2-y3
'(x2-x1)/(x3-x1)=(y2-y1)/(y3-y1)
'y1+(y3-y1)*(x2-x1)/(x3-x1)=y2
map_var["my2"]=((map_var["my3"]-map_var["my1"])*(map_var["mx2"]-map_var["mx1"])/(map_var["mx3"]-map_var["mx1"]))+map_var["my1"]
EndSub
'----------------------------------------------
'Turtle Operations - Could've been Shape Operation had Rotate Triangle not buggy
'----------------------------------------------
Sub DrawTurtle
'Sets size, color, and rotation of Turtle
Shapes.Remove(TT) 'Nothing happens if you remove empty shape. Good!
DrawTurtle_local["tx1"]=(10+siz)*Math.Sin(Math.GetRadians(angle))
DrawTurtle_local["ty1"]=(10+siz)*Math.Cos(Math.GetRadians(angle))
DrawTurtle_local["tx2"]=(10+siz)*Math.Sin(Math.GetRadians(angle+140))
DrawTurtle_local["ty2"]=(10+siz)*Math.Cos(Math.GetRadians(angle+140))
DrawTurtle_local["tx3"]=(10+siz)*Math.Sin(Math.GetRadians(angle+220))
DrawTurtle_local["ty3"]=(10+siz)*Math.Cos(Math.GetRadians(angle+220))
GraphicsWindow.BrushColor=col
TT=Shapes.AddTriangle(DrawTurtle_local["tx1"],DrawTurtle_local["ty1"],DrawTurtle_local["tx2"],DrawTurtle_local["ty2"],DrawTurtle_local["tx3"],DrawTurtle_local["ty3"])
endsub
Sub MoveTurtle
'Move the turtle
Shapes.Move(TT,px+(siz/2),py+(siz/2))
endsub
Sub ShowTurtle
Shapes.ShowShape(TT)
endsub
Sub HideTurtle
Shapes.HideShape(TT)
endsub
'----------------------------------------------
' Various bug fix
'----------------------------------------------
Sub RemBugFix
While (px<0)
px=px+sx
EndWhile
While (py<0)
py=py+sy
EndWhile
endsub
Sub RemBugFix2
While (x1<0)
x1=x1+sx
EndWhile
While (y1<0)
y1=y1+sy
EndWhile
While (angle<0)
angle=angle+360
EndWhile
endsub
Subscribe to:
Posts (Atom)