locked
SOME HELP IN THIS PROGRAM I'VE STARTED PLEASE RRS feed

  • Question

  • I am trying to make a program for SMALL BASIC which will automatically make a labyrinth on the graphics window. Until now , I 've made it draw 2 difrent lines on a certain height and If the x position of the drawn line is more or less than half , the right line is made bigger than the other so at the next stage we can draw new lines leading at the other side. The problem is that sometimes I run it , the one line is getting much bigger than the other. If someone can help me , read my program and find a solution or an easier way to write it. I am a begginer.

    Here is my program  FOR NOW:

    GraphicsWindow

    .BackgroundColor = "LightGreen"

    GraphicsWindow

    .Width = 900

    GraphicsWindow

    .Height = 600

    GraphicsWindow

    .FontSize = 20

    start = Controls.AddButton("BEGIN",380,120)

    Controls

    .SetSize(start,150,150)

    Controls

    .ButtonClicked = lab

    Sub lab

     

    Controls.HideControl(start)

    GraphicsWindow.PenWidth = 3

    GraphicsWindow.PenColor="Red"

    x1 = Math.GetRandomNumber(890)

    nx1=x1

    x2 = x1-10

    nx2=x2

    y1 = Math.GetRandomNumber(5)*100

    y2 = y1

    If y1=100 Or y1=200 Then

    y1 = y1+250

    EndIf

    If x2>=450 Then

    y1 = y1 -30

    EndIf

     

    GraphicsWindow.DrawLine(x1,600,x1,y1)

    y1 = y2

    If x2<450 Then

    y2 = y2 -30

    EndIf

     

    GraphicsWindow.DrawLine(x2,600,x2,y2)

    EndSub

     

     

     

     

     

     

    Tuesday, December 28, 2010 12:09 PM

Answers

  • I had a look at the code and in order to work out what was happening, added comments - hopefully they shed light on why sometimes the lines are long.

    Sub lab
      'Controls.HideControl(start)
      GraphicsWindow.PenWidth = 3
      GraphicsWindow.PenColor="Red"
     
      'This bit gives 2 vertical lines 10 pixels apart ('1' on the right, '2' on the left)
      x1 = Math.GetRandomNumber(890)
      nx1=x1 'Not used
      x2 = x1-10
      nx2=x2 'Not used
      y1 = Math.GetRandomNumber(5)*100
      y2 = y1
     
      'This bit decreases the length of long vertical segments on the right - THIS IS THE CAUSE OF THE OCCASIONAL SHORTER SEGMENTS ON THE RIGHT
      If y1=100 Or y1=200 Then
        y1 = y1+250
      EndIf
     
      'This bit lengthens the right segment if the left segment is over half the window width
      If x2>=450 Then
        y1 = y1 -30
      EndIf
     
      'Draw segment 1 - the one on the right
      GraphicsWindow.DrawLine(x1,600,x1,y1)
      y1 = y2 'This does nothing since y1 is not used again
     
      'This bit lengthens the left segment if the left segment is less than half the window width
      If x2<450 Then
        y2 = y2 -30
      EndIf
     
      'Draw segment 2 - the one on the left
      GraphicsWindow.DrawLine(x2,600,x2,y2)
    EndSub


    On a more general point of how to create a labyrinth, the first thing I would do is draw a few with pencil and paper and try to work out what you did to create them.  Are there any rules about how you did it - this is not an easy starting problem.

    An easier approach would be to recreate some example labyrinths in SmallBasic that you have drawn on paper, rather than using a general method to create random mazes.  If you create a few, you may find some shortcuts to describing them (some sort of shorthand notation), which will help to find a general way to create them.

    • Marked as answer by -Geo- Wednesday, December 29, 2010 3:46 PM
    Tuesday, December 28, 2010 10:02 PM

All replies

  • I had a look at the code and in order to work out what was happening, added comments - hopefully they shed light on why sometimes the lines are long.

    Sub lab
      'Controls.HideControl(start)
      GraphicsWindow.PenWidth = 3
      GraphicsWindow.PenColor="Red"
     
      'This bit gives 2 vertical lines 10 pixels apart ('1' on the right, '2' on the left)
      x1 = Math.GetRandomNumber(890)
      nx1=x1 'Not used
      x2 = x1-10
      nx2=x2 'Not used
      y1 = Math.GetRandomNumber(5)*100
      y2 = y1
     
      'This bit decreases the length of long vertical segments on the right - THIS IS THE CAUSE OF THE OCCASIONAL SHORTER SEGMENTS ON THE RIGHT
      If y1=100 Or y1=200 Then
        y1 = y1+250
      EndIf
     
      'This bit lengthens the right segment if the left segment is over half the window width
      If x2>=450 Then
        y1 = y1 -30
      EndIf
     
      'Draw segment 1 - the one on the right
      GraphicsWindow.DrawLine(x1,600,x1,y1)
      y1 = y2 'This does nothing since y1 is not used again
     
      'This bit lengthens the left segment if the left segment is less than half the window width
      If x2<450 Then
        y2 = y2 -30
      EndIf
     
      'Draw segment 2 - the one on the left
      GraphicsWindow.DrawLine(x2,600,x2,y2)
    EndSub


    On a more general point of how to create a labyrinth, the first thing I would do is draw a few with pencil and paper and try to work out what you did to create them.  Are there any rules about how you did it - this is not an easy starting problem.

    An easier approach would be to recreate some example labyrinths in SmallBasic that you have drawn on paper, rather than using a general method to create random mazes.  If you create a few, you may find some shortcuts to describing them (some sort of shorthand notation), which will help to find a general way to create them.

    • Marked as answer by -Geo- Wednesday, December 29, 2010 3:46 PM
    Tuesday, December 28, 2010 10:02 PM
  • Thank you , litdev

    Maybe you are right , making random labyrinth is not so simple but I won't ignore your help , I'll try to improve it a bit . Before that I'll make a few in a paper to examine all the possibilities while drawing it.

    Wednesday, December 29, 2010 3:45 PM
  • Geo,

    While obviously trickier to create a random maze it is not at all an unrealistic objective and I wouldn't want to put you off trying.  Playing with pen and paper will probably help to get some ideas going.  Post again if you want some feedback on your ideas and maybe some suggestions.

    Good-luck
    Wednesday, December 29, 2010 7:00 PM
  • Geo,

    I got interested in this, and here is an example of a maze generation algorithm with a simple (not very good) game on top.  With more imagination the maze method could be used for more interseting games.  Import SHV994.

    Tuesday, January 11, 2011 10:34 PM