Challenge of the Month - June 2012

# Challenge of the Month - June 2012

• Friday, June 01, 2012 6:34 PM
Moderator

Welcome to the monthly SmallBasic Challenge!

These challenges are intended for people who are learning to program or for more experienced programmers who want to start using SmallBasic after using a different language.  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.

Text Challenge - Repeat (no answers last month)

Write a program to read in and spell-check a sentence of text from a user.

Hint: Use the Dictionary method as a spell checker to find any miss-spelt words and highlight these for the user.

Graphical Challenge

Make a picture of a rainbow by drawing shapes on the GraphicsWindow.

Physics Challenge

Calculate the level that a boat will sit in the water as you add cargo to it.

Hint: Assume the boat has a length of 50m and width of 5m and has vertical sides, and its unloaded mass is 100 tonnes.

If cargo crates are 2x2x5 m and weigh 5 tonnes each, how many crates can it hold to not exceed a total draft of 4m.

If you want, also do a graphical display of the boat and crates to scale as it is loaded.

Maths Challenge

Write a program to accurately calculate the cube root of an input number (between 1 and 1000) without using any of the Math methods.

I am running out of original ideas so if anyone wants to do some future 'Challenge of the Month' and give a new perspective or variety then please make a post.

### All Replies

• Saturday, June 02, 2012 8:47 AM

Maybe an idea for a challenge would be to retrieve mail from an online account and save it to a text dcument r something.

Also make a DOS that is very basic, or a text based game, maybe like zork.

“Computers are like Old Testament gods: lots of rules and no mercy.” – Joseph Campbell

• Saturday, June 02, 2012 1:25 PM

Challenge Idea: Create a program in Small Basic that implements a One-Time-Pad cipher.
• Sunday, June 03, 2012 5:07 PM

Here is my solution of the math problem.

As is explained in the program, the last digit is rounded down.

It is possible that an increase of the last digit by 1 is closer to the real answer

'' calculation of the cuberoot of a number from 1 to 1000
'' 2012-06-03  cuberoot.sb

TextWindow.WriteLine("   The number must lie between 1 and 1000, doesn't need to be integer")
TextWindow.WriteLine("")
TextWindow.Write("Give the number to calculate the cuberoot of: ")
TextWindow.WriteLine("")

answ=0
mult=1
For i=1 To 13  ''number of digits; the last digit is rounded down!
For j= 9 To 0 Step -1
jj=answ+j*mult
j3=jj*jj*jj
if j3<=number then
answ=answ+j*mult
j=-1
EndIf
EndFor  ''j
mult=0.1*mult
EndFor  ''i
TextWindow.Write("  The cube root of  "+number+" is: ")
TextWindow.WriteLine(answ)
TextWindow.WriteLine("")
TextWindow.Write("The third power of this answer is :")
answ=answ*answ*answ
TextWindow.WriteLine(answ)
TextWindow.WriteLine("")

• Sunday, June 03, 2012 5:53 PM
Moderator

WhTurner33,

An inventive method and it works well.

• Wednesday, June 06, 2012 6:55 AM

My Graphical Challenge: DZC358

Nonki Takahashi

• Wednesday, June 06, 2012 12:17 PM

Physics Challenge    RTL937

• Wednesday, June 06, 2012 5:55 PM
Moderator

Nonki & NaochanON,

• Wednesday, June 06, 2012 8:50 PM

Text Challenge

LPX256

It's done but i'm having the weirdest problem, i can seem to put it to work if i change the window size, please feel free to give it a shot ;)

Also, it take's a bit to check the sentence if you have a slow IC like me

```  GraphicsWindow.Width = 415
GraphicsWindow.Height = 460```

Problem Solved by litdev

final version here

XGJ413

• Edited by Wednesday, June 06, 2012 10:04 PM updated the code
•
• Wednesday, June 06, 2012 9:36 PM
Moderator

Mike,

The ButtonClicked event is fired when a button is clicked, there is no need to check the mouse position, since the program already knows it was clicked - it couldn't be clicked if the mouse wasn't over the button at the time it was clicked!

There are methods like Controls.LastClickedButton to identify which of several buttons were pressed - in your case only one.

So we just need:

```Sub CheckMousePosition
ButtonSpellCheckAction()
EndSub
```

The reason the effect changed with the window size is probably that Mouse.MouseX is the coordinate on the screen and you would want GraphicsWindow.MouseX which is the coordinate in the GraphicsWindow.

• Wednesday, June 06, 2012 9:57 PM

thank you litdev, initially i had 2 buttons so i used the x and y mouse position to tell witch button i was pressing but i had forgotten about the LastClickedButton method witch would have made things so much more easy!

btw about new challenges i got a neat idea, draw your name using the turtle methods only! Draw your name in 3D for a full challenge ^^

• Thursday, June 07, 2012 3:19 AM

My Maths Challenge : FNK878

Nonki Takahashi

• Thursday, June 07, 2012 3:34 AM

This is my idea of future Challenge:

(1) Generate a maze

(2) Solve the maze with Turtle

Nonki Takahashi

• Thursday, June 07, 2012 11:38 AM

My Physics Challenge : TZK617

Nonki Takahashi

• Thursday, June 07, 2012 12:45 PM

My Physics Challenge : TZK617

Nonki Takahashi

This is a front view of the boat!

Nonki Takahashi

• Friday, June 08, 2012 12:27 AM

This is my idea of future Challenge:

(1) Generate a maze

(2) Solve the maze with Turtle

Nonki Takahashi

This is a sample shot.  But the maze is not complete.

Nonki Takahashi

• Friday, June 08, 2012 12:33 AM

NaochanON,

Animation! A lot of fun!

Nonki Takahashi

• Friday, June 08, 2012 5:09 AM

My ideas of future Challenge:

4),5),7),12),15)..... maybe necessary to get mouse moving speed.

• Edited by Friday, June 08, 2012 5:14 AM
•
• Friday, June 08, 2012 5:58 PM
Moderator

NaochanON,

Would you like to do next month's challenge.  If you fancy it, then create a new discussion thread with the same sort of format as the current one on the start of July and I will make it sticky.  Try to add a couple basic programming challenges as well, i.e. something suitable for children around 12-14 or those just starting out.

If you are going to do it, then let me know so we don't both create something.

Cheers.

• Saturday, June 09, 2012 7:25 AM

NaochanON,

Would you like to do next month's challenge.  If you fancy it, then create a new discussion thread with the same sort of format as the current one on the start of July and I will make it sticky.  Try to add a couple basic programming challenges as well, i.e. something suitable for children around 12-14 or those just starting out.

OK, I'll try it.  Underline points,   I will consider some proposals by the end of June.

• Edited by Saturday, June 09, 2012 7:26 AM
•
• Wednesday, June 13, 2012 5:27 PM

Graphics challange.

Angle2 = 180
dx = 200
r = 1
color[1] = "Red"
color[2] = "Orange"
color[3] = "Yellow"
color[4] = "Green"
color[5] = "Blue"
color[6] = "indigo"
color[7] = "Violet"
Restart:
dan = .1
While r < 8
Angle2 = Angle2 + dan
If Angle2 > 360 - r Then
Angle2 = 180 + R
dan = 0
dx = dx + 1
r = r + 1
Goto Restart
EndIf
x = Radius * Math.Cos(Angle) + 200
y = Radius * Math.Sin(Angle) + dx
GraphicsWindow.SetPixel(x,y,color[r])
EndWhile

Zock77

• Wednesday, June 13, 2012 5:37 PM
Moderator

Nice method, here's mine:

```color[1] = "Red"
color[2] = "Orange"
color[3] = "Yellow"
color[4] = "Green"
color[5] = "Blue"
color[6] = "indigo"
color[7] = "Violet"

gw = 600
gh = 600
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
GraphicsWindow.BackgroundColor = "LightBlue"
xCentre = gw/2
yCentre = gh/2
width = 10
size = gw/3

For i = 1 To 7
GraphicsWindow.BrushColor = color[i]
EndFor

GraphicsWindow.BrushColor = GraphicsWindow.BackgroundColor
GraphicsWindow.FillRectangle(0,yCentre,gw,gh-yCentre)
```

• Wednesday, June 13, 2012 7:42 PM

Here is my source code for the graphical challenge. And when I was working on the Math Challenge I found another way to do it much more accurately. Basically, I told it to take the imput number(the number that you are trying to find the cubic root of) set it as variable x and If it was 1 return a value of 1. If it was < 1 to then start generating values for y from 0 to 1 with a step of .000001. Next, If it was > 1 I made it generate values for y between 1 and 10 with a step of .000001. Finally i said if y * y * y = x then writeline y. The only problem with this method is sometimes there was not a number with 6 decimal points that could be cubed to equal x. If more 0's were added before the 1 in the decimal it would increase the accuracy but it would also take much longer to generate. If anyone has a solution to this it would be most appreciated. Also, I don't know if small basic can do exponets because this is what i tried first. I said the input number is x and then i tried to multiply x to the 1/3 power. I know small basic can do this by using Math.Power, but when i tried x * ^1/3 I couldnt make it work. If anybody knows how to do this it would be another soultion to the math challenge. I look forward to somebodys reply... So finally here is the code for my rainbow. :)

GraphicsWindow.Show()
GraphicsWindow.BackgroundColor = "LightBlue"
GraphicsWindow.Width = Desktop.Width
GraphicsWindow.Height = Desktop.Height
GraphicsWindow.Top = 0
GraphicsWindow.Left = -5
GraphicsWindow.CanResize = "True"
GraphicsWindow.BrushColor = "Red"
GraphicsWindow.FillEllipse(0,Desktop.Height - 350,Desktop.Width,Desktop.Width)
GraphicsWindow.BrushColor = "Orange"
GraphicsWindow.FillEllipse(40,Desktop.Height - 310,Desktop.Width - 80,Desktop.Width - 80)
GraphicsWindow.BrushColor = "Yellow"
GraphicsWindow.FillEllipse(80,Desktop.Height - 270,Desktop.Width - 160,Desktop.Width - 160)
GraphicsWindow.BrushColor = "LimeGreen"
GraphicsWindow.FillEllipse(120,Desktop.Height - 230,Desktop.Width - 240,Desktop.Width - 240)
GraphicsWindow.BrushColor = "Blue"
GraphicsWindow.FillEllipse(160,Desktop.Height - 190,Desktop.Width - 320,Desktop.Width - 320)
GraphicsWindow.BrushColor = "Indigo"
GraphicsWindow.FillEllipse(200,Desktop.Height - 150,Desktop.Width - 400,Desktop.Width - 400)
GraphicsWindow.BrushColor = "Violet"
GraphicsWindow.FillEllipse(240,Desktop.Height - 110,Desktop.Width - 480,Desktop.Width - 480)
GraphicsWindow.BrushColor = "White"
GraphicsWindow.FillEllipse(280,Desktop.Height - 70,Desktop.Width - 560,Desktop.Width - 560)
GraphicsWindow.BrushColor = "White"
NumGenerator:
Num = Math.GetRandomNumber(240)
If Num > 30 Then
Goto Clouds
Elseif Num < 30 Then
Goto NumGenerator
Clouds:
GraphicsWindow.FillEllipse(10,Num - 40,60,60)
GraphicsWindow.FillEllipse(40,Num - 15,60,60)
GraphicsWindow.FillEllipse(60,Num - 35,60,60)
GraphicsWindow.FillEllipse(80,Num - 20,60,60)
GraphicsWindow.FillEllipse(38,Num,60,60)
GraphicsWindow.FillEllipse(95,Num - 40,60,60)
GraphicsWindow.FillEllipse(95,Num - 10,60,60)
EndIf

I added a cloud that appears in a random location above the rainbow and a blue sky background because I felt it was too simple without it.

• Wednesday, June 13, 2012 8:34 PM
Moderator

Good rainbow and I like the cloud.

This calculates the cube root to the full accuracy of SmallBasic numbers (16 bytes - or about 28 significant figures):

```'Simple Newton method calculation of cube root
cube = 300
accuracy = 0
cubeRoot()
TextWindow.WriteLine("The cube root of "+cube+" is "+ x)
TextWindow.WriteLine("Error = "+F+" in "+iter+" iterations")

Sub cubeRoot
F = 1
x = 1
iter = 0
While ((F < -accuracy Or F > accuracy) And iter < 100)
F = x*x*x-cube
dFdx = 3*x*x
dx = -F/dFdx
x = x+dx
iter = iter+1
EndWhile
EndSub```
• Friday, June 15, 2012 4:22 AM

Graphical Challenge    NJJ207

It rains,   makes puddles, and finally rainbow appears.

• Edited by Friday, June 15, 2012 4:26 AM
•
• Friday, June 15, 2012 10:58 PM

I've solved the graphics challenge: HNH634

Should adjust itself to different screen resolutions.

I'm enjoying this...

Any fun changes anyone can think of?

Robin.

• Friday, June 15, 2012 11:46 PM

I tried playing around with the code and came up with the following: HNH634-0

This demonstrates what to me seems like a problem with the way shapes and images are handled in MSB. Why does a shape have to be added to the graphics window when it is assigned to a variable? Wouldn't it make more sense either to have position parameters for shapes.add, or to not display until shapes.move was used?

Does anyone know a way around this?

I've looked at code for the game Stargates, where images are moved off screen immediately until needed, but I think they appear for a brief moment before being moved.

Robin.

• Saturday, June 16, 2012 1:35 AM

graphicswindow hide

make shapes -->  hide shapes  -->

grapwindow show

repeat    show shapes -->  hide shapes

• Saturday, June 16, 2012 1:54 AM

Hi Robin!

When a shape is created ( or a control ), SB generates a unique type name for it.
But there are many myths about on what really happens!  :P

Let's take this example bellow:

It draws an ellipse and returns a name.
In this case we haven't assigned a variable to record its name, so it gets lost!
But its name still exists, we just need to figure out what it was!

The truth is: the 1st ellipse shape name generated is always "Ellipse1".
You can check this fact this way:

So, we are not "obliged" to store their names in variables, as long as we count the order on which each one is generated!
It's just for convenience! :D

Here's another code which controls the ellipse shapes w/o storing their generated names:

For i = 1 To 10
Shapes.Move( "Ellipse" + i  50, i*40 )
EndFor

Now, for the issue of shapes showing up while they are being created, the most we can do is move them ASAP to off-screen coordinates:

Shapes.Move( shape,  -100,-100 )

Or then use GraphicsWindow.Hide() until they're ready.

Well, that's all I wanted to say! ^_^

Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

• Saturday, June 16, 2012 12:12 PM

I like this idea! It could be very challenging though, I reckon, especially if the theory of mazes is a new subject!

I found the following webpage that I think will give me a starting point: http://www.mazeworks.com/mazegen/mazetut/index.htm

Robin.

• Monday, June 18, 2012 5:58 PM

I have another idea for a challenge: a French translator using dictionary command?
• Wednesday, June 20, 2012 12:08 AM

In anticipation of next month's challenge(?), I've written this: NRW874.
Any feedback welcome, including improvements, alternative solutions, words of encouragement, or explantions of occasional random blue squares!
• Wednesday, June 20, 2012 6:20 AM

1)using blue-rectangle-shapes?

2)If left mousebutton -->  Erase( setopacity=0) & OnOFF[r][c]=0

if right mousebutton -->  draw ( setopacity=100) & OnOFF[r][c]=1

*you can remake a maze easily.

3)save  OnOff [r][c] data    as   txt file

* you can reload past maze data

• Thursday, June 21, 2012 4:35 AM

In anticipation of next month's challenge(?), I've written this: NRW874.
Any feedback welcome, including improvements, alternative solutions, words of encouragement, or explantions of occasional random blue squares!

Feedback:

(1) Interesting!

(2) I think better that rows and columns will be odd numbers.

(3) I think better that mouseDownEvent doesn't have "For" loops.

(4) Following picture is my sample initial grids.  How about?

Nonki Takahashi

(4) is an idea only for manual maze making, not good for auto maze building.
• Thursday, June 21, 2012 6:02 AM

I like this idea! It could be very challenging though, I reckon, especially if the theory of mazes is a new subject!

I found the following webpage that I think will give me a starting point: http://www.mazeworks.com/mazegen/mazetut/index.htm

Robin.

Robin, good hint!

I could confirm that DFS algorithm easily to make maze automatically!

Nonki Takahashi

• Friday, June 22, 2012 4:48 PM

Hi Nonki,

Thanks for your input. I'm quite new to this, and I struggled to find a way to detect that the mouse was being clicked within a certain cell. Any suggestions about how  to do this without the For loops? Also, could I see your code for the image above?

Robin.

• Sunday, June 24, 2012 2:13 AM

Hi Nonki,

Thanks for your input. I'm quite new to this, and I struggled to find a way to detect that the mouse was being clicked within a certain cell. Any suggestions about how  to do this without the For loops? Also, could I see your code for the image above?

Robin.

I modified your code and published as NRW874-0.

I wrote automated maze generation also, but you can change the maze by mouse after generation.

If you need the image above, please comment in lines 27 to 31.

Nonki Takahashi

• Tuesday, June 26, 2012 12:42 AM

I have an another idea for future challenge:

Show graphical abacus with digital display and beads will be moved with mouse click.

---

Following picture is a sample image created with Excel.

Nonki Takahashi

• Tuesday, June 26, 2012 2:31 AM

Nice!  (いいね！）
• Wednesday, July 11, 2012 2:44 PM

Future Text Challenge Idea : Indent program for Small Basic.

Example:

Input source:

For i = 1 To 10

TextWindow.Write(i + " ")

EndFor

TextWindow.WriteLine("")

Output:

For i = 1 To 10

TextWindow.Write(i + " ")

EndFor

TextWindow.WriteLine("")

Nonki Takahashi

• Wednesday, July 11, 2012 5:51 PM
Moderator

Nonki,

Do you want to do next month's challenges.  If so, please let me know and try to think up some easy challenges as well as harder ones.

• Thursday, July 12, 2012 11:29 AM

litdev,

OK, I'd like to do so.

Nonki Takahashi

• Thursday, July 19, 2012 5:44 PM
Moderator

Nonki,

Can you set it a little early (on or before Tuesday 24 July) since I won't be around for a couple of weeks to make it sticky - if not I will create an empty sticky thread and you can add your challenges as the first reply to the thread.

Thanks.

• Monday, July 23, 2012 2:22 PM

litdev,

OK, I'll create new thread as soon as possible.

Nonki Takahashi

• Wednesday, February 13, 2013 3:03 AM

I sought a further reality: DZC358-1.

Original photo is by Evan Leeson