none
Challenge of the Month - July 2013

    General discussion

  • These challenges are intended for people who are learning to program for the first time or for those returning to programming who want to start using SmallBasic.  Some will be easy, some will be hard - but they will all make you think, and more importantly be GREAT FUN!

    Please post your solutions / partial solutions / questions / feedback etc. into this thread that will remain 'sticky' for the month.  The only rule is that your solution must use standard SmallBasic methods (no extensions).

    It would be good if people could post their problems with these challenges so that a discussion can start so that everyone can learn from each other.

    Also post feedback on the kind of challenges that you want to see more of in the future.

    Curriculum Challenge 2.1

    (1) Write a program that draws 5 randomly positioned and sized red rectangles on a blue background.

    (2) Draw a picture of a car using rectangles, ellipses and lines.

    (3) Draw a map of your local area.

    Basic Challenge 1

    Write a program where the user enters 5 numbers and you calculate the sum (all added), product (all multiplied) and average.

    Basic Challenge 2

    Write a program with 4 buttons, one in each corner or the GraphicsWindow.  They will:

    • Change the background colour
    • Make a sound
    • Show an image in the middle of the GraphicsWindow
    • Clear the GraphicsWindow, but not the buttons!

    Intermediate Challenge 1

    Write a program using the Controls methods to create a booking form for a hotel and store the results to a file to be printed.

    Data fields include:

    • Name
    • Arrival date
    • Departure date
    • Number of adults and children
    • Breakfast included or not
    • Special requests

    Intermediate Challenge 2

    Open a text file, search it for some keywords and output any sentence containing the keywords.

    The keywords should be set in another file, read by the program.

    Physics Game Challenge

    Make a simple table air hockey game for 2 players.

    Maths Challenge

    Write a program to calculate the sum of the first 100 prime numbers.

    Graphics Challenge

    Draw an animation of a walking man.

    Community Suggestion 1 (By Timo So)

    Write a program that creates SmallBasic programs.

    The user can input a set of commands like:

    file C:\test.txt open
    file C:\test.txt delete
    image C:\test.jpg show

    Your program will check for keywords and create a new SmallBasic program file that performs the operation and can be opened an run by SmallBasic.

    Community Suggestion 2 (By Nonki)

    (1) Make a head (or tail) command which the UNIX system has.

    (2) Draw (or fill) a round-rectangle with parameters x, y, width, height and border-radius.

    Do you have an idea for a future challenge? Please post it here!


    Monday, July 01, 2013 9:14 PM
    Moderator

All replies

  • litdev~

    Here's an option you wrote, and I quote verbatim, "(2) Draw a picture of a car using lines, rectangles, ellipses and lines." You wrote "lines" twice.


    I can write 1000 lines of code in roughly 10 seconds. Here's how:
    i = 1

    For i = 1 to 1000
    TextWindow.WriteLine(i+ ". Code")
    Program.Delay(10)
    EndFor

    Make Sense?

    Tuesday, July 02, 2013 4:14 AM
  • fixed typo - thanks
    Tuesday, July 02, 2013 1:06 PM
    Moderator
  • My Entry for Basic Challenge 2. Import it it doesn't work very well on browser.                                                            SCW898


    I am a 12 year old learning how to code using small basic.


    • Edited by 8Bit Pony Wednesday, July 03, 2013 2:38 AM nothing
    Wednesday, July 03, 2013 12:27 AM
  • My Entry to Basic Challenge 1: MMS129

     :3

     


    I am a 12 year old learning how to code using small basic.


    • Edited by 8Bit Pony Wednesday, July 03, 2013 2:55 AM Wrong Link
    Wednesday, July 03, 2013 2:53 AM
  • This is my basic challenge 1: XJH677.

    Nonki Takahashi

    Wednesday, July 03, 2013 9:41 AM
    Moderator
  • This is my sample of head command for community suggestion 2 (1).

    Usage:

    1. Save following source code as head.sb .
    2. Run once.
    3. Open command prompt.
    4. Change directory (with cd command) to the directory you save the head.sb .
    5. Type head -? for help, head [-n line] filename to run.

    ' head
    ' usage: head [-n lines] filename
    GetOption()
    GetFilename()
    If help Or filename = "" Then
      TextWindow.WriteLine("usage: head [-n lines] filename")
    Else
      ReadFile()
      WriteHead()
    EndIf
    Sub GetOption
      nArg = Program.ArgumentCount
      For i = 1 To nArg
        arg[i] = Program.GetArgument(i)
      EndFor
      i = 1
      head = 10
      help = "False"
      While Text.StartsWith(arg[i], "-")
        If arg[i] = "-n" Then
          head = arg[2]
          i = i + 2
        Else
          help = "True"
          i = i + 1
        EndIf
      EndWhile
    EndSub
    Sub GetFilename
      filename = arg[nArg]
    EndSub
    Sub ReadFile
      NL = Text.GetCharacter(13) + Text.GetCharacter(10)
      buf = File.ReadContents(filename)
      nLine = 0
      p = 1
      len = Text.GetLength(buf)
      While p <= len
        eol = Text.GetIndexOf(Text.GetSubTextToEnd(buf, p), NL)
        nLine = nLine + 1
        If eol > 0 Then ' eol found
          line[nLine] = Text.GetSubText(buf, p, eol - 1)
          p = p + eol + 1
        Else  ' eol not found
          line[nLine] = Text.GetSubTextToEnd(buf, p)
          p = len + 1
        EndIf
      EndWhile
    EndSub
    Sub WriteHead
      For i = 1 To head
        TextWindow.WriteLine(line[i])
      EndFor
    EndSub


    Nonki Takahashi


    Wednesday, July 03, 2013 11:31 AM
    Moderator
  • Good solution 8bit.

    That's a good challenge. Demonstrates how Controls (& Shapes) sit on top of the GraphicsWindow.

    1st layer is GraphicsWindow, any statement starting GraphicsWindow.method(...) is drawn on the GW layer

    2nd layer are things you place on top of the GW, like Controls and Shapes.

    When you clear the GW it clears everything on top of the GW and everything on the GW.

    Wednesday, July 03, 2013 7:27 PM
    Answerer
  • Thanks for the feed back!

    I finished The Intermediate challenge (1) just now. I might keep working on it so you don't have to change the path in the code. LSG226

    I'd appreciate any feed back, also. :3


    I am a 12 year old learning how to code using small basic.

    Wednesday, July 03, 2013 11:51 PM
  • Anyone?

    Intermediate Challenge 2

    Open a text file, search it for some keywords and output any sentence containing the keywords. The keywords should be set in another file, read by the program.

    Is this an example of an Input/output based application -a type of structured programming?

    If anyones interested in this topic here's a link: http://norvig.com/adapaper-pcai.html

    This is how i'm writing a POS program from an earlier challenge. Easy for me to add/change the program to suit the users needs.

    Thursday, July 04, 2013 12:28 AM
    Answerer
  •  I might keep working on it so you don't have to change the path in the code. LSG226

    I'd appreciate any feed back, also. :3



    You can use the Program.Directory variable. When you type in a SB-program and use Program.Directory it points to a temporary directory in your account, but once you save the program, Program.Directory points to the directory where you saved the program.

    You can better change File.WriteContents into File.AddContents, as the first overwrites the old contents, while the second add the new booking at the end of the file and keeps the old information.


    Jan [ WhTurner ] The Netherlands



    Thursday, July 04, 2013 10:42 AM
    Answerer
  • HSJ417

    Thanks for the feedback. c: i fixed it. now,you only have to create a txt file named "BookingForum".


    I am a 12 year old learning how to code using small basic.

    Thursday, July 04, 2013 3:19 PM
  •  Basic Challenge 2

    Learnt a lot with this challenge. Could add some more features like, Preview next pic and BG colour.

    Also wondering if I can animate the opening message while image is loading?

    And might try out rescale window using get GW width/height in a loop with a flag? i.e. a pseudo event picked up by get width.

    This is why I like SB, gives me a chance to learn some good techniques and also test what it can do.

    LRM180

    Thursday, July 04, 2013 9:41 PM
    Answerer
  • Intermediate Challenge 2

    Open a text file, search it for some keywords and output any sentence containing the keywords.

    The keywords should be set in another file, read by the program.

    BSK175-0

    This program makes a source file and a keyword file at first.

    Friday, July 05, 2013 12:35 PM
  • This is a sample code of filling a round-rectangle for community suggestion 2 (2):

    param="x=10;y=10;width=200;height=100;border-radius=20;"
    FillRoundRectangle()
    Sub FillRoundRectangle
      x = param["x"]
      y = param["y"]
      width = param["width"]
      height = param["height"]
      r = param["border-radius"]
      If (width / 2 < r) Or (height / 2 < r) Then
        r = Math.Min(width / 2, height / 2)
      EndIf
      GraphicsWindow.FillEllipse(x, y, r * 2, r * 2)
      GraphicsWindow.FillRectangle(x + r, y, width - r * 2, height)
      GraphicsWindow.FillEllipse(x + width - r * 2, y, r * 2, r * 2)
      GraphicsWindow.FillRectangle(x, y + r, width, height - r * 2)
      GraphicsWindow.FillEllipse(x, y + height - r * 2, r * 2, r * 2)
      GraphicsWindow.FillEllipse(x + width - r * 2, y + height - r * 2, r * 2, r * 2)
    EndSub

    To fill is easier than to draw!

    Screen shot of a Small Basic program which draws a round-rectangle.


    Nonki Takahashi


    Friday, July 05, 2013 1:20 PM
    Moderator
  • This is very early version of graphics challenge: TFN794.

    Screen shot of a program Walkman 0.1a


    Nonki Takahashi

    Friday, July 05, 2013 1:38 PM
    Moderator
  • Nonki. Amazing. Are you going to get this guy to move as well?
    Friday, July 05, 2013 10:09 PM
    Answerer
  • Sure.  Firstly, I'd like to try to let this guy can be correctly shown in remote with Silverlight environment. 

    Nonki Takahashi

    Saturday, July 06, 2013 2:35 AM
    Moderator
  • litdev~

    I found another typo!!

    "...Basic Challenge 2

    Write a program with 4 buttons, one in each corner or the GraphicsWindow.  They will:..."

    I think it should be "...one in each corner of the GraphicsWindow. ..." not "or."


    I can write 1000 lines of code in roughly 10 seconds. Here's how:
    i = 1

    For i = 1 to 1000
    TextWindow.WriteLine(i+ ". Code")
    Program.Delay(10)
    EndFor

    Make Sense?

    Saturday, July 06, 2013 8:40 PM
  • Graphics Challenge

    Draw an animation of a walking man.    GWV844    

    Sunday, July 07, 2013 6:59 AM
  • NaochanON, walking man is amazing.
    Sunday, July 07, 2013 7:10 AM
    Answerer
  • Very nice walking man.
    Sunday, July 07, 2013 7:56 PM
    Moderator
  • Graphics Challenge

    Draw an animation of a walking man.    GWV844    

    Featured it: http://blogs.msdn.com/b/smallbasic/archive/2013/07/08/small-basic-featured-program-a-walking-man-animated-in-code.aspx

    Great job!


    Ed Price (a.k.a User Ed), SQL Server Customer Program Manager (Blog, Small Basic, Wiki Ninjas, Wiki)

    Answer an interesting question? Create a wiki article about it!

    Tuesday, July 09, 2013 12:42 AM
    Owner
  • Beta version of community challenge 1

    TBZ268

    Save this program and create a .txt in the same directory called commands.txt

    Here are how to use the commands:

    console.writeline: This Is a Test
    delay: 1000
    image.addimage: C:\Test\Imagething
    Program.endoftext

    One more thing. you always have to end the commands.txt with "Program.endoftext"

    Please tell me what you think!


    It is written: "'As surely as I live,' says the Lord, 'every knee will bow before me; every tongue will acknowledge God.'" Romans 14:11

    Tuesday, July 09, 2013 4:14 PM
    Answerer
  • I expanded Nonki's FilledRoundedRectangle (see topic of July 5th) with a subroutine for DrawRoundedRectangle. Insert the following codeblock after the second line of Nonki's program.

    I just found another possibility:

    Draw a slightly smaller filled rounded rectangle with background brushcolor  over the blue one .

    param["y"]=y+150
    DrawRoundRectangle()
    Sub DrawRoundRectangle
      x = param["x"]
      y = param["y"]
      width = param["width"]
      height = param["height"]
      r = param["border-radius"]
      If (width / 2 < r) Or (height / 2 < r) Then
        r = Math.Min(width / 2, height / 2)
      EndIf
      GraphicsWindow.DrawLine(x+r,y,x+width-r,y)  
      GraphicsWindow.DrawLine(x+r,y+height,x+width-r,y+height)  
      GraphicsWindow.DrawLine(x,y+r,x,y+height-r)  
      GraphicsWindow.DrawLine(x+width,y+r,x+width,y+height-r)  
      lastx=x+width-r
      lasty=y+height
      For ang=0 To 90 Step 5
        angr= Math.GetRadians(ang)
        xang=Math.Sin(angr)*r+x+width-r
        yang=Math.Cos(angr)*r+y+height-r
        GraphicsWindow.DrawLine(lastx,lasty,xang,yang)
        lastx=xang
        lasty=yang
      endfor
      lastx=x+width
      lasty=y+r
      For ang=90 To 180 Step 5
        angr= Math.GetRadians(ang)
        xang=Math.Sin(angr)*r+x+width-r
        yang=Math.Cos(angr)*r+y+r
        GraphicsWindow.DrawLine(lastx,lasty,xang,yang)
        lastx=xang
        lasty=yang
      endfor
      lastx=x+r
      lasty=y
      For ang=180 To 270 Step 5
        angr= Math.GetRadians(ang)
        xang=Math.Sin(angr)*r+x+r
        yang=Math.Cos(angr)*r+y+r
        GraphicsWindow.DrawLine(lastx,lasty,xang,yang)
        lastx=xang
        lasty=yang
      endfor
      lastx=x
      lasty=y+height-r
      For ang=270 To 360 Step 5
        angr= Math.GetRadians(ang)
        xang=Math.Sin(angr)*r+x+r
        yang=Math.Cos(angr)*r+y+height-r
        GraphicsWindow.DrawLine(lastx,lasty,xang,yang)
        lastx=xang
        lasty=yang
      endfor
    EndSub
    


    Jan [ WhTurner ] The Netherlands

    Tuesday, July 09, 2013 5:29 PM
    Answerer
  • Draw a white rounded rectangle over the blue one:

    Put the following three lines after the second line: 

    param="x=54;y=54;width=192;height=092;border-radius=16;"
    GraphicsWindow.BrushColor="White"
    FillRoundRectangle()
    


    Jan [ WhTurner ] The Netherlands

    Tuesday, July 09, 2013 5:50 PM
    Answerer
  •  I just added some features to my  Basic Challenge 2 solution. It now shows a preview of the next BG colour and next Pic. It does it by running some event routines while the main event loop is running something else.  Seems to be working fine.

    http://smallbasic.com/program/?PXQ334

    I now want to try and animate the "wait screen" at the start of the program while the pic is downloading & loading. So as an event loop nOOb i'm thinking I will need to load the 1st pic in an event (timer) and flag the animation to run while the pic is loading.

    Have no idea if you can do this, any fb and tips would be appreciated.

    Tuesday, July 09, 2013 8:36 PM
    Answerer
  • WhTurner33, good job!
    You can write DrawArc subroutine so DrawRoundRectangle will become more compact.

    Nonki Takahashi

    Wednesday, July 10, 2013 2:07 AM
    Moderator
  • Jibba Jabba, nice program.

    This is just an idea.  I haven't tried yet.

    circle = Shapes.AddEllipse(10, 10) angle = 0 cx = GraphicsWindow.Width / 2 cy = GraphicsWindow.Height / 2 r = 100 Timer.Interval = 1000 Timer.Tick = OnTick LoadPic() Timer.Pause()

    Shapes.HideShape(circle) : : Sub OnTick rad = Math.GetRadians(angle) x = cx + r * Math.Cos(rad) y = cy + r * Math.Sin(rad) Shapes.Move(circle, x, y) angle = angle + 45 If 360 <= angle Then angle = 0 EndIf EndSub


    And GraphicsWindow.Clear() clears controls also.  So other approach should be better.


    Nonki Takahashi


    Wednesday, July 10, 2013 2:33 AM
    Moderator
  • Nonki, thanks for the tip.


    Wednesday, July 10, 2013 6:18 AM
    Answerer
  • This is very early version of graphics challenge: TFN794.

    Screen shot of a program Walkman 0.1a


    Nonki Takahashi

    I rewrote this program as TFN794-0 to workaround for remote environment.

    Nonki Takahashi


    Thursday, July 11, 2013 3:51 AM
    Moderator
  • This is my beta version of graphics challenge: TFN794-1.

    Screen shot of a program Walkman 0.3b


    Nonki Takahashi

    Thursday, July 11, 2013 5:15 AM
    Moderator
  • My spin on Community Suggestion 1 (By Timo So).

    Program ID: SRT592

    This program reads in a smallbasic file, and searches the file for certain keywords. Currently I have it set to search for "//include" and "//sub"

    if, for example, you have the following line in your program:

    '//include c:\temp\myprogram.sb

    SRT592 will append myprogram.sb to the smallbasic program you used as input.

    the //sub keyword will allow you to quickly create code that mimics a function call with multiple arguments and a single output.

    For example, if you have the following line in your program:

    '//sub x = mysub(a, b)

    SRT592 will replace that line with the following code:

    arg1 = a
    arg2 = b
    mysub()
    x = output

    You can have as many or as few arguments as you like and it will work with or without an output value. you can even use the //sub keyword to create a call like this:

    '//sub txt = mytext(Text.getSubtext(a, 1, 5), "My text", b)

    which creates:

    arg1 = Text.getsubtext(a, 1, 5)
    arg2 = "My text"
    arg3 = b
    mytext()
    txt = output

    I'm pretty sure I have all the bugs worked out, but I'm always happy to know if you guys can break it! :D Dowload it, Give it a go, and let me know what you think!

    Saturday, July 13, 2013 3:39 AM
  • My graphic solution to Basic Challenge 1.

    Desktop version: WZK146

    Browser version: http://smallbasic.com/program/?KNQ722-0

    Saturday, July 13, 2013 5:51 AM
    Answerer
  • my entry for challenge is for bassic challenge 1 .

    intermediate challenge 1.

    math challenge


    Want to Learn for Help !!

    Saturday, July 13, 2013 6:30 AM
  • Nonki, amazing walking stick man.
    Saturday, July 13, 2013 9:34 AM
    Answerer
  •  Basic Challenge 2

    Added an animated opening screen with speed bar. Runs locally only. PJJ742-2.

    Will continuously load up to 50 pics then clear each one from memory after it's viewed while still loading.

    The opening screen shows till 10 pics are preloaded. And has a preview of what's next.




    Friday, July 19, 2013 3:15 PM
    Answerer
  • These are ideas for a future challenge.
    (1) Create a simple command prompt with dir (list directory) command and cd (change directory) command.
    (2) Draw a picture of color cube which has 216 web safe colors.


    Nonki Takahashi

    Saturday, July 27, 2013 3:25 PM
    Moderator
  • This another version of my challenge for community suggestion 2 (2): JMN410.

    Screen shot of a program Draw and Fill Round Rectangle


    Nonki Takahashi

    Monday, July 29, 2013 3:49 AM
    Moderator
  • This is an alpha version of physics game challenge: KLB414-0.

    Screen shot of a program Air Hockey 0.1a


    Nonki Takahashi


    Monday, July 29, 2013 8:04 AM
    Moderator
  • Ideas for next months Community Challenge:

    1. A calculator with some neat features. Using either Control Buttons or an Array Table to set out the buttons.
    2. A visually appealing stop watch with Start, Stop, Reset, Lap and memory to file.
    3. An improved clone of Curriculum 4.1's See Saw exercise. A sample I did MPK879-0



    Tuesday, July 30, 2013 3:44 AM
    Answerer
  • This is still alpha version for physics game challenge: KLB414-1.

    Screen shot of a program Air Hockey 0.2a


    Nonki Takahashi

    Tuesday, July 30, 2013 12:06 PM
    Moderator