Showing posts with label example. Show all posts
Showing posts with label example. Show all posts
Tuesday, June 26, 2012
Small Basic Spell Checker - 25 lines challenge
'Spell checker example - wxv722-2
'Uses Dictionary.GetDefinition(word) - Need internet access
'Harry Hardjono
'June 2012 - 25 line small basic challenge version
'
Str="abcdefghijklmnopqrstuvwxyz'ABCDEFGHIJKLMNOPQRSTUVWXYZ"
While (inputtext<>" ")
TextWindow.WriteLine(" ")
TextWindow.WriteLine("Enter your sentence: ")
inputtext=text.Append(TextWindow.Read()," ")
For i=1 To Text.GetLength(inputtext)
If (Text.GetIndexOf(Str,Text.GetSubText(inputtext,i,1))=0) Then 'Not word
If (Word<>"") Then
If (Dictionary.GetDefinition(Word)="") Then
TextWindow.BackgroundColor="white"
TextWindow.ForegroundColor="black"
TextWindow.Write(Word)
TextWindow.BackgroundColor="black"
TextWindow.ForegroundColor="white"
Else
TextWindow.Write(Word)
EndIf
Word=""
EndIf
TextWindow.Write(Text.GetSubText(inputtext,i,1))
Else 'Word
Word=Text.Append(Word,Text.GetSubText(inputtext,i,1))
EndIf
endfor
EndWhile
Tuesday, June 12, 2012
Small Basic Spell Checker
'Spell checker example - wxv722-1
'Uses Dictionary.GetDefinition(word) - Need internet access
'Harry Hardjono
'June 2012
'
Init:
Str="abcdefghijklmnopqrstuvwxyz'ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Word=""
Loop:
TextWindow.BackgroundColor="black"
TextWindow.ForegroundColor="white"
TextWindow.WriteLine(" ")
TextWindow.WriteLine("Enter your sentence: ")
inputtext=text.Append(TextWindow.Read()," ") 'bug fix. ^_^;
If (inputtext=" ") Then
Goto End
EndIf
For i=1 To Text.GetLength(inputtext)
curchar=Text.GetSubText(inputtext,i,1)
If (Text.GetIndexOf(Str,curchar)=0) Then
'Not word
If (Word<>"") Then
WordDef=Dictionary.GetDefinition(Word)
If (WordDef="") Then
TextWindow.BackgroundColor="white"
TextWindow.ForegroundColor="black"
TextWindow.Write(Word)
TextWindow.BackgroundColor="black"
TextWindow.ForegroundColor="white"
Else
TextWindow.Write(Word)
EndIf
Word=""
EndIf
TextWindow.Write(curchar)
Else 'Word
Word=Text.Append(Word,curchar)
EndIf
endfor
Goto Loop
End:
Program.End()
Tuesday, May 8, 2012
Small Basic Morse Code
I got bored one day, and hey, remember that Rock/Paper/Scissors game? That was quick. So I was hunting for a quick coding project, quicker than my usual one hour session. The Morse code is it. It took about 10 minutes, including typing all those entries. Of course, the original design was phonetic alphabet project, but I decided that typing Morse code entries would be faster than typing phonetic alphabet entries.
BTW, you don't have to limit yourself. You can do your own project like this, and substitute Klingon alphabet, for example. Do you know that Small Basic uses Unicode? Try looping some big numbers through Text.GetCharacter and you'll get the idea.
Of course, as luck would have it, I still have some time, so I improved it with tones. I think there is something wrong here because the sound is so soft! Oh, well.
'Extending the code, as well as decoding is left as an exercise for the reader!
Morse="a=+-;b=-+++;c=-+-+;d=-++;e=+;f=++-+;g=--+;h=++++;i=++;j=+---;k=-+-;l=+-++;m=--;n=-+;o=---;p=+--+;q=--+-;r=+-+;s=+++;t=-;u=++-;v=+++-;w=+--;x=-++-;y=-+--;z=--++;"
Loop:
TextWindow.Write("Enter Text:")
t=TextWindow.Read()
For i=1 To Text.GetLength(t)
TextWindow.Write((Morse[Text.ConvertToLowerCase(Text.GetSubText(t,i,1))])+" ")
EndFor
TextWindow.WriteLine(" ")
Goto Loop
'Extending the code, as well as decoding is left as an exercise for the reader!
Morse="a=+-;b=-+++;c=-+-+;d=-++;e=+;f=++-+;g=--+;h=++++;i=++;j=+---;k=-+-;l=+-++;m=--;n=-+;o=---;p=+--+;q=--+-;r=+-+;s=+++;t=-;u=++-;v=+++-;w=+--;x=-++-;y=-+--;z=--++;"
TextWindow.WriteLine("Morse Code Encoder by Harry Hardjono")
Loop:
TextWindow.Write(Text.GetCharacter(13)+Text.GetCharacter(10)+"Enter Text:")
t=TextWindow.Read()
For i=1 To Text.GetLength(t)
MC=Text.Append(MC,(Morse[Text.ConvertToLowerCase(Text.GetSubText(t,i,1))]+" ")) 'Comment this out for no sound
TextWindow.Write((Morse[Text.ConvertToLowerCase(Text.GetSubText(t,i,1))])+" ")
EndFor
PlayMorse() 'Comment this out for no sound
Goto Loop
Sub PlayMorse
TT="+=C12;-=C4; =P4;"
For j=1 To Text.GetLength(MC)
MT=Text.Append(MT,TT[Text.GetSubText(MC,j,1)])
EndFor
Sound.PlayMusic(MT)
MC=""
MT=""
EndSub
Tuesday, April 24, 2012
Small Basic Analog Clock
I wrote a Small Basic Analog Clock. Since I really want to have background pictures, I use Shapes. The rotation is a mess. Lots of trial and error. I also put in time offset, although you have to muddle with the source code to do it. Maybe an update will allow you to do it without modifying the source code. The sample picture has random picture from Flickr. I do wish I know who took it, so it can be properly credited.
'Analog Clock - MHR140
'A Shape example
'by Harry Hardjono
' April 2012
'
'Init
GraphicsWindow.Width=400
GraphicsWindow.Height=400
GraphicsWindow.Show()
GraphicsWindow.Clear()
screen_x=GraphicsWindow.Width
screen_y=GraphicsWindow.Height
'Commented out because it hangs on my SilverLight
'BPic=Flickr.GetRandomPicture("clock")
'BImg=ImageList.LoadImage(BPic)
'BGP=Shapes.AddImage(BImg)
For i=1 to 12
Digits[i]=Shapes.AddRectangle(10,40)
mx1=1
mx2=i
mx3=12
my1=30
my3=360
map()
DAngle=my2
Dx=(screen_x/2)+150*Math.Sin(Math.GetRadians(DAngle))
Dy=(screen_y/2)-150*Math.Cos(Math.GetRadians(DAngle))
Shapes.Rotate(Digits[i],DAngle)
Shapes.Move(Digits[i],Dx,Dy)
Program.Delay(100)
endfor
GraphicsWindow.BrushColor="White"
Hand[1]=Shapes.AddRectangle(20,90) 'Hour hand
Hand[2]=Shapes.AddRectangle(12,150) 'Minute hand
Hand[3]=Shapes.AddRectangle(2,180) 'Second hand
GraphicsWindow.FontSize=15
GraphicsWindow.BrushColor="Black"
TT=Shapes.AddText(Clock.Time)
Shapes.Move(TT,150,5)
OffsetHour=0
OffsetMin=0
OffsetSec=0
Loop:
OnTick()
Program.Delay(1000)
Goto Loop
Sub OnTick
Shapes.SetText(TT,Clock.Time)
THour=Clock.Hour
mx1=0
mx2=Math.Remainder(THour+OffsetHour,12)
mx3=12
my1=0
my3=360
map()
DAngle=my2
Dx=((screen_x/2)+10)+40*Math.Sin(Math.GetRadians(DAngle))
Dy=((screen_y/2)-22.5)-40*Math.Cos(Math.GetRadians(DAngle))
Shapes.Rotate(Hand[1],DAngle)
Shapes.Move(Hand[1],Dx-15,Dy)
TMin=Clock.Minute
mx1=0
mx2=Math.Remainder(TMin+OffsetMin,60)
mx3=60
my1=0
my3=360
map()
DAngle=my2
Dx=((screen_x/2)+6)+60*Math.Sin(Math.GetRadians(DAngle))
Dy=((screen_y/2)-75)-60*Math.Cos(Math.GetRadians(DAngle))
Shapes.Rotate(Hand[2],DAngle)
Shapes.Move(Hand[2],Dx-8,Dy+20)
TSec=Clock.Second
mx1=0
mx2=Math.Remainder(TSec+OffsetSec,60)
mx3=60
my1=0
my3=360
map()
DAngle=my2
Dx=((screen_x/2)+1)+70*Math.Sin(Math.GetRadians(DAngle))
Dy=((screen_y/2)-90)-70*Math.Cos(Math.GetRadians(DAngle))
Shapes.Rotate(Hand[3],DAngle)
Shapes.Move(Hand[3],Dx+3,Dy+20)
Endsub
Sub map 'map function
'x1-x2-x3 y1-y2-y3
my2=((my3-my1)*(mx2-mx1)/(mx3-mx1))+my1
EndSub
Tuesday, April 17, 2012
Small Basic Dice Roller
If it takes me less than one hour of programming, it is a good project for beginner! That in mind, this is an example of a dice roller. Obviously, it's just a lot of variable assignments. You can change the source code if you want, say D100. An example of using the GUI (Control) elements.
Also, I put in a picture loader. Push the "0" button to check it out.
'Dice Roller - MWH959
'A GUI example
'by Harry Hardjono
' April 2012
'
'Init
GraphicsWindow.Show()
GraphicsWindow.Clear()
backpic=""
screen_x=GraphicsWindow.Width
screen_y=GraphicsWindow.Height
NumDice=1
ValDice=6
Button[0]=Controls.AddButton("0",50,50)
Button[1]=Controls.AddButton("1",80,50)
Button[2]=Controls.AddButton("2",110,50)
Button[3]=Controls.AddButton("3",140,50)
Button[4]=Controls.AddButton("4",170,50)
Button[5]=Controls.AddButton("5",50,90)
Button[6]=Controls.AddButton("6",80,90)
Button[7]=Controls.AddButton("7",110,90)
Button[8]=Controls.AddButton("8",140,90)
Button[9]=Controls.AddButton("9",170,90)
Dice[2]=Controls.AddButton("D2",50,150)
Dice[3]=Controls.AddButton("D3",90,150)
Dice[4]=Controls.AddButton("D4",130,150)
Dice[5]=Controls.AddButton("D5",170,150)
Dice[6]=Controls.AddButton("D6",50,190)
Dice[7]=Controls.AddButton("D7",90,190)
Dice[8]=Controls.AddButton("D8",130,190)
Dice[9]=Controls.AddButton("D9",170,190)
Dice[10]=Controls.AddButton("D10",50,230)
Dice[11]=Controls.AddButton("D11",90,230)
Dice[12]=Controls.AddButton("D12",130,230)
Dice[13]=Controls.AddButton("D13",170,230)
Dice[14]=Controls.AddButton("D14",50,270)
Dice[15]=Controls.AddButton("D15",90,270)
Dice[16]=Controls.AddButton("D16",130,270)
Dice[17]=Controls.AddButton("D17",170,270)
Dice[18]=Controls.AddButton("D18",50,310)
Dice[19]=Controls.AddButton("D19",90,310)
Dice[20]=Controls.AddButton("D20",130,310)
Dice[21]=Controls.AddButton("D21",170,310)
Controls.ButtonClicked=OnButtonClicked
'Event
Sub OnButtonClicked
LastButton=Controls.LastClickedButton
GraphicsWindow.BrushColor="white"
GraphicsWindow.FillRectangle(240,50,400,400)
If (backpic<>"") Then
GraphicsWindow.DrawResizedImage(backpic,0,0,screen_x,screen_y)
EndIf
GraphicsWindow.BrushColor="black"
GraphicsWindow.FontSize=30
If (LastButton=Button[0]) Then 'Use sparingly. Picture is loaded every screen redraw!
backpic=Flickr.GetRandomPicture("dice")
GraphicsWindow.DrawResizedImage(backpic,0,0,screen_x,screen_y)
NumDice=1
ElseIf (LastButton=Button[1]) then
NumDice=1
ElseIf (LastButton=Button[2]) then
NumDice=2
ElseIf (LastButton=Button[3]) then
NumDice=3
ElseIf (LastButton=Button[4]) then
NumDice=4
ElseIf (LastButton=Button[5]) then
NumDice=5
ElseIf (LastButton=Button[6]) then
NumDice=6
ElseIf (LastButton=Button[7]) then
NumDice=7
ElseIf (LastButton=Button[8]) then
NumDice=8
ElseIf (LastButton=Button[9]) then
NumDice=9
Else
'Do nothing
endif
If (LastButton=Dice[2]) Then
ValDice=2
ElseIf (LastButton=Dice[3]) then
ValDice=3
ElseIf (LastButton=Dice[4]) then
ValDice=4
ElseIf (LastButton=Dice[5]) then
ValDice=5
ElseIf (LastButton=Dice[6]) then
ValDice=6
ElseIf (LastButton=Dice[7]) then
ValDice=7
ElseIf (LastButton=Dice[8]) then
ValDice=8
ElseIf (LastButton=Dice[9]) then
ValDice=9
ElseIf (LastButton=Dice[10]) then
ValDice=10
ElseIf (LastButton=Dice[11]) then
ValDice=11
ElseIf (LastButton=Dice[12]) then
ValDice=12
ElseIf (LastButton=Dice[13]) then
ValDice=13
ElseIf (LastButton=Dice[14]) then
ValDice=14
ElseIf (LastButton=Dice[15]) then
ValDice=15
ElseIf (LastButton=Dice[16]) then
ValDice=16
ElseIf (LastButton=Dice[17]) then
ValDice=17
ElseIf (LastButton=Dice[18]) then
ValDice=18
ElseIf (LastButton=Dice[19]) then
ValDice=19
ElseIf (LastButton=Dice[20]) then
ValDice=20
ElseIf (LastButton=Dice[21]) then
ValDice=21
Else
EndIf
GraphicsWindow.DrawText(260,70,NumDice)
GraphicsWindow.DrawText(290,70,("D"+ValDice))
Result=0
For i=1 To NumDice
Result=Result+Math.Floor(Math.GetRandomNumber(ValDice))
EndFor
GraphicsWindow.FontSize=70
GraphicsWindow.DrawText(290,120,Result)
EndSub
Subscribe to:
Posts (Atom)