none
Challenge of the Month - April 2015 RRS feed

  • 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 Small Basic. 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 Small Basic 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.

    Small Challenges

    • Write a TextWindow program to read in 2 times of the day (24 hour clock) and calculate the number of minutes between the 2 times, eg 10:25 and 14:12 is 227 minutes.
    • Write a GraphicsWindow program to mimic a touch-tone telephone.

    Musical Challenge

    Write a graphical interface program to play the tuning notes for some instruments, for example:

    • Guitar
    • Violin
    • Trumpet

    Graphics Challenge

    Using the Flickr object now working in version 1.1, white a simple slide show in the GraphicsWindow. 

    As an extra challenge try adding some fancy transitions like fade in/out, swipe, sprial in etc.

    Game Challenge

    Write a slot machine game - pull the handle, spin the wheels and win win win.

    Community Suggeston (by Pappa Lapub)

    • Stegano Image

    Below there a 2 images (a black and a white one, but could be any monochrome picture) that look rather boring. But both of them contain a very, very, .. OK & very important and secret message.

    Simply write a SB program to reveal and view the hidden contents.
    Hint: The red component in some parts of the image(s) is NOT as it should be on a pure black (or white/monochrome) image! Maybe even try to show the solution(s) in colors (eg. red, green; blue, yellow like on a MS flag)
    PS: To impede cheating there's also included a simple 'flood protection'.
    To create a similar image yourself, you would need LitDev Ext and ~ 10-20 lines of code.

    • NoSleep

    Imagine you are running a huge download or another unattended process and you want to avoid, that your computer gets to sleep or hibernation, while you're away. Write a simple SB program, that keeps your system awake in the meantime.

    Communit Suggestion (by Nonki)

    Write a simple Logo interpreter which has following 6 commands:

    • FORWARD n (to move the turtle n pixels)
    • RIGHT n (to rotate the turtle right n degrees)
    • LEFT n (to rotate the turtle left n degrees)
    • PENUP (to lift the pen up)
    • PENDOWN (to set the pen down)
    • REPEAT n [commands] (to repeat commands n times)


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

    Wednesday, April 1, 2015 6:15 PM
    Moderator

All replies

  • Stegano Challenge - great challenge

    Wednesday, April 1, 2015 9:37 PM
    Moderator
  • Haha, you were curious! Here's my black one

    Wednesday, April 1, 2015 10:33 PM
    Answerer
  • This is my first step for community suggestion by Nonki: XVK119-1.
    Implemented commands except REPEAT.

    Screen shot of a program Turtle Graphics 0.3a


    Nonki Takahashi

    Monday, April 6, 2015 12:50 AM
    Moderator
  • Here's my Slot Machine solution: Slot Machine. It uses some .au sound files so these may not run on some pc's.



    • Edited by Jibba jModerator Sunday, November 15, 2015 9:00 AM replaced id with gallery
    Wednesday, April 8, 2015 12:15 AM
    Moderator
  • Brilliantly done Jibba Jabba.
    Wednesday, April 8, 2015 9:33 PM
    Answerer
  • Wow! How could you get JPT105 likes Jackpot?

    Screen shot of a program Jackpot! (Slot Machine)


    Nonki Takahashi

    Thursday, April 9, 2015 4:55 AM
    Moderator
  • This is my second step for community suggestion by Nonki: XVK119-2.

    Screen shot of a program Turtle Graphics 0.4b


    Nonki Takahashi

    Thursday, April 9, 2015 5:28 AM
    Moderator
  • Pappa Lapub,

    I like your challenge NoSleep.  This is great challenge.

    This is my first trial.  But doesn't work...  I thought user input should prevent sleep.  But this wasn't user input.

    count = 100
    GraphicsWindow.BrushColor = "Black"
    GraphicsWindow.DrawText(10, 10, "Hit Alt+F4 to close.")
    While "True"
      sx = Mouse.MouseX
      sy = Mouse.MouseY
      dx = Math.GetRandomNumber(Desktop.Width)
      dy = Desktop.Height - Math.GetRandomNumber(40)
      For i = 1 To count
        k = i / count
        x = k * dx + (1 - k) * sx
        y = k * dy + (1 - k) * sy
        Mouse.MouseX = x
        Mouse.MouseY = y
        Program.Delay(50)
      EndFor
    EndWhile


    Nonki Takahashi

    Thursday, April 9, 2015 7:38 AM
    Moderator
  • Thanks Coding Cat and Nonki.

    I used paint.net to crop and cut an image posted on the net for educational purposes.

    With a bit of graphic s/ware practice I should be able to make original designs from generic outlines. I'm also interested in using digital scanning and more powerful graphic s/ware down the track.

    The code is unstructured (just hacked out a design) so I have to structure it so I can add some more features.

    Thursday, April 9, 2015 9:30 AM
    Moderator
  • re: slot machine challenge.

    Would be good to know how to animate the barrels spinning.

    Thursday, April 9, 2015 9:35 AM
    Moderator
  • Jibba Jabba,

    Here is an example that will help you with the spinning effect: BZN148

    'Spin/Roll Effect Example - by Matthew L. Parets aka codingCat
    'The effect suggests the spinning of a slot machine wheel
    'This effect takes advantage of the fact that small basic layers 
    'shapes as they are created. New shapes are created on top
    'of previous created shapes.
    
    GraphicsWindow.Show()
    GraphicsWindow.PenColor = "Black"  ' Change to white to see the full effect
    circ = Shapes.AddEllipse(25,25)    'A shape on the wheel
    GraphicsWindow.BrushColor = "black" ' Change to white to see the full effect
    bottom = Shapes.AddRectangle(30,30) 'The rectangles will be created on top of the circle
    top = Shapes.AddRectangle(30,30)
    
    Shapes.Move(top, 300,100)     'Move the rectangles to the middle of the screen
    Shapes.Move(bottom, 300,160)
    Shapes.Move(circ, 303,100)    'hide the circle behind the top rectangle
    
    circY = 100                   'Starting place for circle
    
    While "True"
      circY = circY + 1           'Move the circle down a dot
      If circY > 160 Then         'Are we fully behind the bottom rectangle?
        circY = 100               'Move up behind the top rectangle
      EndIf
      Shapes.Move(circ, 303,circY)  'Move the circle
      Program.Delay(10)             'wait for a second
    EndWhile
      

    As the comments describe, the effect is possible because Small Basic creates shapes on top of each other. New shapes sit on top of older shapes.

    For your slot machine game I would suggest chopping up your picture into top, middle and bottom pieces. Add the middle first. Then add the top and bottom shapes AFTER you have added the squares, circles and triangles. This would give you the obscuring effect that would suggest spinning.

    Enjoy!

    Thursday, April 9, 2015 5:20 PM
    Answerer
  • Hi Nonki,

    Looks as you're on the same way with Mouse moves. Wrote it quickly last yr before had to leave. But had to use Piccolino Extension to perform a left click in the GW every now and then, like 'do a little rocky horror jump to the left (+click) and another jump to the right (+click)', ...etc). And this worked/s fine.

    Before posting the challenge i tried it again (with extension line(s) commented out, but the small GW in the right bottom of the screen and cursor jumping up and down (clock area of tray) to cause a system reaction in the hope, this would wake it up.

    And that seemed to work (without extension). But after another test this afternoon, screen got dark and hib. mode was entered. Changed power settings since then or anyone bumped the mouse, when the test ran successfully(?). Just recompiled Piccolino ext and running some more tests. I hope to get it working until ultimo (w/o ext), else i'll have to shame for incomplete testing :-(( You sure wanna make it working for yourself, though CKC093 is my WiP at the moment (line MouseControl.LeftClick() needs a 'commentout !)


    Thursday, April 9, 2015 7:08 PM
    Answerer
  • PS: Could'nt believe my eyes, when LitDev already finished the SteganoImage, while i was thinking about adding another more hint to make it a bit easier (to understand). So here's another one with the same principle.

    Friday, April 10, 2015 10:31 PM
    Answerer
  • Thanks for the sample CC.

    I reckon too that shapes are the way to go for the barrel roll. The best way to move a graphic. If you use Zoom to enhance any 3d effect then the code gets a bit harder since shapes zoom around the centre.

    I checked out some pro slot machines. Those guys seem to go hard at using the path of least resistance.

    e.g. it seems they use sprites but don't scale them for roll effect but instead move them over a fixed b/ground that has roll effect shading. It creates a convincing effect of rolling.

    Friday, April 10, 2015 11:37 PM
    Moderator
  • Actually, thinking about it right now, you could get a lot of what your thinking with the Zoom effect, but only zooming in one direction. For example, compressing the shape as it approaches the top. If you did this over a black background you could also ramp down the Opacity at the same time. This would give a fade effect as well. Together I think they would give you about 80% of what you are looking for.
    Saturday, April 11, 2015 12:59 AM
    Answerer
  • @JibbaJabba - Great slot machine.

    And my code for the first Stegano Image challenge, JJG240

    Saturday, April 11, 2015 8:26 PM
    Moderator
  • Pappa Lapub,

    This is my test procedure and updated program.

    1. Open Control Panel > Hardware and Sound > Power Options > Create a power plan > Select [Power saver] > Enter "Sleep test" to Plan name > [Next] > Select [1 minute] for both [Turn off the display] and [Put the computer to sleep] > [Create] > Close Control Panel
    2. Wait about 4 minutes for sleep.
    3. Push power button for wake up.
    4. Run following program.
    5. Wait about 4 minutes for sleep.

    count = 100
    GraphicsWindow.BrushColor = "Black"
    GraphicsWindow.DrawText(10, 10, "Hit Alt+F4 to close.")
    GraphicsWindow.FontSize = 30
    time = Shapes.AddText("0")
    Shapes.Move(time, 10, 30) 
    t0 = Clock.ElapsedMilliseconds
    While "True"
      sx = Mouse.MouseX
      sy = Mouse.MouseY
      dx = Math.GetRandomNumber(Desktop.Width)
      dy = Desktop.Height - Math.GetRandomNumber(40)
      For i = 1 To count
        t = (Clock.ElapsedMilliseconds - t0) / 1000
        Shapes.SetText(time, t)
        k = i / count
        x = k * dx + (1 - k) * sx
        y = k * dy + (1 - k) * sy
        Mouse.MouseX = x
        Mouse.MouseY = y
        Program.Delay(50)
      EndFor
    EndWhile


    Nonki Takahashi


    Sunday, April 12, 2015 3:52 AM
    Moderator
  • This is my third step for community suggestion by Nonki: XVK119-3.

    • Supported nested REPEAT.
    • Supported to skip opening with space key.

    Screen shot of a program Turtle Graphics 0.5b


    Nonki Takahashi


    Sunday, April 12, 2015 4:19 PM
    Moderator
  • @Nonki, thanks for procedure description. I'll test this till tomorrow (to keep the sleeper awake :-).

    About SteganoImage,

    Instead of a solution i'll first post the program it's been made with, which also shows the principle and makes it easier to unhide the message. ID: LDS658 (needs LitDev extension, a little bit extended with infos and a Sub to avoid overwriting of an already existing file)

    My solution for Small Challenge (TimeDiff in TW): WDZ371

    Monday, April 13, 2015 7:31 PM
    Answerer
  • This is my second try for the NoSleep challenge:

    GraphicsWindow.BrushColor = "Black"
    GraphicsWindow.FontSize = 30
    time = Shapes.AddText("0")
    Shapes.Move(time, 10, 10) 
    t0 = Clock.ElapsedMilliseconds
    fname = File.GetTemporaryFilePath()
    GraphicsWindow.Title = fname
    While "True"
      t = (Clock.ElapsedMilliseconds - t0) / 1000
      Shapes.SetText(time, t)
      File.WriteContents(fname, t)
      Program.Delay(100)
    EndWhile
    I tried to use File I/O to prevent sleep.  But it didn't work well...


    Nonki Takahashi

    Saturday, April 18, 2015 1:50 AM
    Moderator
  • ... just like the 'Show Desktop' area right from the clock (on W7, donno if still there on W8), which does'nt work as well. Visited every 2 minutes (for a min) by the cursor did'nt darken the screen, but finally went to sleep.
    Saturday, April 18, 2015 6:18 PM
    Answerer
  • This is my solution for musical challenge:

    gw = 598
    gh = 428
    GraphicsWindow.Width = gw
    GraphicsWindow.Height = gh
    GraphicsWindow.Title = "Ukulele Tuner"
    GraphicsWindow.BackgroundColor = "DimGray"
    GraphicsWindow.BrushColor = "Black"
    GraphicsWindow.FontName = "Trebuchet MS"
    GraphicsWindow.FontSize = 40
    button[4] = Controls.AddButton("G", 10, 10)
    button[3] = Controls.AddButton("C", 50, 10)
    button[2] = Controls.AddButton("E", 90, 10)
    button[1] = Controls.AddButton("A", 130, 10)
    Controls.ButtonClicked = OnButtonClicked
    Sub OnButtonClicked
      tone = Controls.GetButtonCaption(Controls.LastClickedButton)
      Sound.PlayMusic("O5" + tone + "4")
    EndSub

    Screen shot of a program Ukulele Tuner


    Nonki Takahashi




    Sunday, April 19, 2015 12:49 AM
    Moderator
  • Community Suggestion:

    • Write a Dragon Game. It can be any game where you try to slay a dragon, whether it is a text-based game, graphical, or a combination. You could also make the dragon the hero.

    Ed Price, Azure & Power BI Customer Program Manager (Blog, Small Basic, Wiki Ninjas, Wiki)

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

    Monday, April 20, 2015 9:07 AM
    Owner
  • Thanks litdev.
    Wednesday, April 22, 2015 8:28 PM
    Moderator
  • Dragon Game - good idea for a challenge!

    There's some good free dragon games on the net to have a look at.

    Wednesday, April 22, 2015 8:32 PM
    Moderator
  • I updated litdev's code and solve the Stegano image above.  Program ID is JJG240-0.

    Screen shot of a program Stegano Image Viewer


    Nonki Takahashi


    Friday, April 24, 2015 3:23 AM
    Moderator
  • I blogged about Jibba Jabba's slot machine:

    http://blogs.msdn.com/b/smallbasic/archive/2015/04/24/slot-machines-small-basic-featured-program.aspx

     

    Jibba Jabba, I sent you a Skype message. Thanks!

     


    Ed Price, Azure & Power BI Customer Program Manager (Blog, Small Basic, Wiki Ninjas, Wiki)

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

    Friday, April 24, 2015 7:05 PM
    Owner
  • My solution for SteganoImage ID: XRN627

    Gets pixel(0,0) as a supposed reference for the base color of the whole image and only reveals the pixels, whose red channel differs from the red channel of this base image color.
    Also added some other viewing variants (random, stripes, rainbow).

    Suggestion for a future challenge:

    Collage: Write a program to create a collage of images (ev. TextShape(s)) in the GraphicsWindow. Lots of viewing samples on the web about 'collage or gallery software'.


    Friday, April 24, 2015 9:20 PM
    Answerer
  • Thanks Ed. I just replied to your Skype: Rick Murphy aka Jibba Jabba.
    Friday, April 24, 2015 11:04 PM
    Moderator
  • Game Challenge

    Import ID: VLN842

    I read about the 1.1 version and I wanted to come back and check it out. Small Basic is even more awesome now!


    • Edited by Athasak Monday, April 27, 2015 6:10 PM
    Sunday, April 26, 2015 10:10 AM
  • After some experimenting, I made a Steganoimage with the contents of a textfile. Three consecutive characters are placed into the three colors of a pixel. The following program can read the text back. It works for me locally, and I am curious if it works if I place the image here in the forum.

    '////  read back \\\\\
    HX="123456789ABCDEF"
    image=Program.Directory+"\declar.png"  'here the name you give the image
    im=ImageList.LoadImage(image)
    hgt= ImageList.GetHeightOfImage(im)
    GraphicsWindow.DrawImage(im,0,0)
    For v=0 To hgt
      For i=0 To 99
        px=GraphicsWindow.GetPixel(i,v)
        For c=1 To 5 step 2
          val=0
          For cc=1 To 2
            col=text.GetSubText(px,c+cc,1)
            val=16*val+Text.GetIndexOf(HX,col)
          EndFor
          TextWindow.Write(Text.GetCharacter(val))
        EndFor
      EndFor
    EndFor
    TextWindow.WriteLine("")


    Jan [ WhTurner ] The Netherlands


    Sunday, April 26, 2015 5:23 PM
    Answerer
  • Hi Jan,  hey cool, and works great (i used

    image = https://social.msdn.microsoft.com/Forums/getfile/649879

    , but also if saved as local file) and i'm dissecting it at the moment:

    Char    pxlCol          c       cc      col     val
    T;      #546865;        1;      2;      4;      84
    h;      #546865;        3;      2;      8;      104
    e;      #546865;        5;      2;      5;      101
     ;      #20756E;        1;      2;      0;      32
    u;      #20756E;        3;      2;      5;      117
    n;      #20756E;        5;      2;      E;      110
    a;      #616E69;        1;      2;      1;      97
    n;      #616E69;        3;      2;      E;      110
    i;      #616E69;        5;      2;      9;      105
    m;      #6D6F75;        1;      2;      D;      109
    o;      #6D6F75;        3;      2;      F;      111
    u;      #6D6F75;        5;      2;      5;      117
    s;      #732044;        1;      2;      3;      115
     ;      #732044;        3;      2;      0;      32
    D;      #732044;        5;      2;      4;      68
    e;      #65636C;        1;      2;      5;      101
    c;      #65636C;        3;      2;      3;      99
    l;      #65636C;        5;      2;      C;      108
    a;      #617261;        1;      2;      1;      97
    r;      #617261;        3;      2;      2;      114
    a;      #617261;        5;      2;      1;      97
    t;      #74696F;        1;      2;      4;      116
    i;      #74696F;        3;      2;      9;      105
    o;      #74696F;        5;      2;      F;      111
    n;      #6E206F;        1;      2;      E;      110
     ;      #6E206F;        3;      2;      0;      32
    o;      #6E206F;        5;      2;      F;      111
    f;      #662074;        1;      2;      6;      102

    etc....

    Total 8700 characters (incl. the #000000 's at the end).

    Do you have an SB code to write/draw the encoded hex pixels ?? (eg. TextFileToHexPixels.sb)

    Sunday, April 26, 2015 7:53 PM
    Answerer
  • Hi Athasak

    Great opening screen then I seemed to get stuck at this screen:

    Any idea why?

    Sunday, April 26, 2015 9:17 PM
    Moderator
  • Hi Jan, worked for me using the images "src" path.

    Sunday, April 26, 2015 10:00 PM
    Moderator
  • Hi Jibba,

    I don't really know why this happened. But this is what it should look like :)


    • Edited by Athasak Sunday, April 26, 2015 10:19 PM
    Sunday, April 26, 2015 10:09 PM
  • Very Nice.

    Maybe one of the images is not d/loading to my PC.

    I ran it locally. I'll check it out.

    Sunday, April 26, 2015 11:08 PM
    Moderator
  • Athasak

    I found 1 image that can't load:

    ImgHandle = "C:\Users\Θανάσης\Documents\Small Basic\Images\Random\RedSphere.png"

    If you want just post that image here on the forum and i'll post the link to it.

    Monday, April 27, 2015 3:54 AM
    Moderator
  • Jibba

    Thank you. Here is the image:

    Monday, April 27, 2015 8:12 AM
  • Hi Jan,  hey cool, and works great

    Do you have an SB code to write/draw the encoded hex pixels ?? (eg. TextFileToHexPixels.sb)

    Hello Pappa Lepub,

    This is the coding proram:

    FI= Program.Directory+"\#tst\declar.txt"   '<< file with text
    CNTS=File.ReadContents(FI)
    Ltxt=Text.GetLength(CNTS)
    GraphicsWindow.DrawText(120,10,Ltxt+" characters in file")
    saveTo=Program.Directory+"\#tst\declar.png"    '<<imagefile
    
    '/// convert 3 characters to one color \\\\
    For v=0 To Math.Ceiling(Ltxt/300)   '300 chars in one pixelline of 100 pixels
      For h=0 To 99
        ch=100*v+h
        For j=1 To 3
          char=3*ch+j
          c[j]=Text.GetCharacterCode(Text.GetSubText(CNTS,char,1))
        EndFor
        col= GraphicsWindow.GetColorFromRGB(c[1],c[2],c[3])
        GraphicsWindow.SetPixel(h,v,col)
      EndFor
    EndFor
    Program.Delay(3000)
    '//// save the image \\\\\\
    gwShot= LDGraphicsWindow.Capture("","")
    LDImage.Crop(gwShot,0,0,100,Math.Ceiling(Ltxt/300))
    LDImage.SaveAs(gwShot,saveTo)
    GraphicsWindow.Clear()
    Program.Delay(3000)
    im=ImageList.LoadImage(saveTo)
    GraphicsWindow.DrawText(120,10,"reloaded image")
    GraphicsWindow.DrawImage(im,0,0)


    Jan [ WhTurner ] The Netherlands

    Monday, April 27, 2015 5:14 PM
    Answerer
  • Athasak

    Here's the path to the Handle: https://social.msdn.microsoft.com/Forums/getfile/650084

    Great Slot Machine and Top Graphics - love it!

    I added a tiny bit of (rough) code:

    • added the above path to: ImgHandle
    • used ImageList.LoadImage because I've got slow internet
    • and short "How to Play" message at the start because it took me a second to work out how the handle worked

    Very Nice.

    Monday, April 27, 2015 5:31 PM
    Moderator
  • Jibba

    Thank you! I edited a previous post with the correct link for the image.

    Could you publish the program and give me the code, I would love to see and study your improvements.

    Also your Slot Machine solution is absolutely fantastic! - great fun :)

    Monday, April 27, 2015 6:23 PM
  • Hi Jan, see this and take that :-)
    Monday, April 27, 2015 6:59 PM
    Answerer
  • I just preloaded the Images and added "how to" in a Textwindow.   GPZ097

    You've got to Pull the Handle Down Quickly.

    Monday, April 27, 2015 7:01 PM
    Moderator
  • Thanks Jibba :)
    Monday, April 27, 2015 7:44 PM
  • Hi Pappa Lapub, Thank you for the nice "message".

    Jan [ WhTurner ] The Netherlands

    Tuesday, April 28, 2015 10:42 AM
    Answerer
  • @ Jan, because it's cooool man :-) Lots of info on minimal space.



    • Edited by Pappa LapubEditor Friday, May 1, 2015 1:13 PM Mistakenly removed during upgrade
    Tuesday, April 28, 2015 7:24 PM
    Answerer
  • My solution for NoSleep: ----

    Unfortunately this does NOT work with cursor moves alone (not between 2 different app windows, when window is activated by cursor entering or with other native SB commands). At least i could not find a solution. and guess there has to be an active physical mouse movement or key press on the keyboard. Simulated key strokes (KeyboardBuffer, SendKeys), GW resizing/moving around aso did'nt work either. Only simulated mouse clicks worked (except the cat played with the mouse in the meantime), like this offTopic ID: QVM401 (WITH extension and no challenge solution) worked. But if using, it's as simple as launch and forget.  Sorry Nonki and if anyone else tried it.

    Thursday, April 30, 2015 10:43 PM
    Answerer
  • Hi PL

    I haven't been following what you're doing with this, but maybe you wish to force an SB window back on top.

    Some thoughts, maybe:

    TextWindow.Show()
    TextWindow.Hide()
    GraphicsWindow.Hide()
    GraphicsWindow.Show()

    Also sometimes you can hide a window by pushing it off the desktop:

    TextWindow.Show()
    TextWindow.Hide()
    GraphicsWindow.Hide()
    GraphicsWindow.Show()

    Program.Delay(1000)
    GraphicsWindow.Top = Desktop.Height

    Friday, May 1, 2015 12:17 AM
    Moderator
  • Thanks JJ,

    It's simply about, to keep the system busy and so prevent it from falling into hibernation mode, while anything important is going on. But does'nt work without extension and above is the only way if could find (but only with ext!). So i put ashes on my head and tear my clothes (sometime, maybe :-))

    Friday, May 1, 2015 1:22 PM
    Answerer