Newcomers Corner or Code Snippets?
I decided to post here because all my previous postings have been here.
As I wrote in one of my postings,
Quote: "
My DBClassic programming skills are on a continual learner level so I'd like to hear from you if you can offer a different (not necessarily better) way of writing any part of any of the programmes.
"
In light of that, and looking at the following snippets (all from the same programme)in which there are 32 goto's in total, could the code have been written differently, thereby avoiding the so-called dreaded goto?
None in this snippet but this part of the code, together with the subroutine checkselection:, in the next snippet, forms a loop until a selection has been made.
` Come here if no selection is made
noselection:
` --------------------------------------------------
` Choose any domino
` --------------------------------------------------
` Define the string (a space is needed at the end
` to print the last word)
message$=""
message$=message$+"Choose any domino, remember it or write it down "
message$=message$+"and click on it "
` Determine text width of longest message line
` and where to write text to screen.
tw=text width("Choose any domino, remember it ")/2
across=320-tw:down=420:wide=across+0
wide=wide+text width("Choose any domino, remember it ")
` Call the function to write words to screen
writewords(across,down,message$,tw,wide)
` A small pause
wait 1000
` This command will show the mouse pointer
show mouse
` This command will set the mouse pointer
position mouse 320,441
` Go to subroutine to check selection
gosub checkselection
In this snippet, there's 28 goto's. 27 go to a label re_turn: within the subroutine and 1 to a label noselection: as in the snippet above
` -------------------------------------------------
` Subroutine to check for selection
` -------------------------------------------------
checkselection:
`Wait for mouse click
suspend for mouse
` If mouse button is pressed
x=mousex()
y=mousey()
` This command will hide the mouse pointer
hide mouse
`-------------------------
` First row of dominoes
`-------------------------
if (x>=99 and x<=141) and (y>=21 and y<=139)
selection=66
goto re_turn
endif
if (x>=149 and x<=191) and (y>=21 and y<=139)
selection=65
goto re_turn
endif
if (x>=199 and x<=241) and (y>=21 and y<=139)
selection=64
goto re_turn
endif
if (x>=249 and x<=291) and (y>=21 and y<=139)
selection=63
goto re_turn
endif
if (x>=299 and x<=341) and (y>=21 and y<=139)
selection=62
goto re_turn
endif
if (x>=349 and x<=391) and (y>=21 and y<=139)
selection=61
goto re_turn
endif
if (x>=399 and x<=441) and (y>=21 and y<=139)
selection=60
goto re_turn
endif
if (x>=449 and x<=491) and (y>=21 and y<=139)
selection=55
goto re_turn
endif
if (x>=499 and x<=541) and (y>=21 and y<=139)
selection=54
goto re_turn
endif
`-------------------------
` Second row of dominoes
`-------------------------
if (x>=99 and x<=141) and (y>=155 and y<=273)
selection=53
goto re_turn
endif
if (x>=149 and x<=191) and (y>=155 and y<=273)
selection=52
goto re_turn
endif
if (x>=199 and x<=241) and (y>=155 and y<=273)
selection=51
goto re_turn
endif
if (x>=249 and x<=291) and (y>=155 and y<=273)
selection=50
goto re_turn
endif
if (x>=299 and x<=341) and (y>=155 and y<=273)
selection=44
goto re_turn
endif
if (x>=349 and x<=391) and (y>=155 and y<=273)
selection=43
goto re_turn
endif
if (x>=399 and x<=441) and (y>=155 and y<=273)
selection=42
goto re_turn
endif
if (x>=449 and x<=491) and (y>=155 and y<=273)
selection=41
goto re_turn
endif
if (x>=499 and x<=541) and (y>=155 and y<=273)
selection=40
goto re_turn
endif
`-------------------------
` Third row of dominoes
`-------------------------
if (x>=99 and x<=141) and (y>=289 and y<=407)
selection=33
goto re_turn
endif
if (x>=149 and x<=191) and (y>=289 and y<=407)
selection=32
goto re_turn
endif
if (x>=199 and x<=241) and (y>=289 and y<=407)
selection=31
goto re_turn
endif
if (x>=249 and x<=291) and (y>=289 and y<=407)
selection=30
goto re_turn
endif
if (x>=299 and x<=341) and (y>=289 and y<=407)
selection=22
goto re_turn
endif
if (x>=349 and x<=391) and (y>=289 and y<=407)
selection=21
goto re_turn
endif
if (x>=399 and x<=441) and (y>=289 and y<=407)
selection=20
goto re_turn
endif
if (x>=449 and x<=491) and (y>=289 and y<=407)
selection=11
goto re_turn
endif
if (x>=499 and x<=541) and (y>=289 and y<=407)
selection=10
goto re_turn
endif
goto noselection
re_turn:
selection=selection
return
Here we have 2 goto's, each of which go to a label here: within the function
function writewords(across,down,message$,tw,wide)
` Go through all the letters in the message
for t=1 to len(message$)
` Add one letter from message$ to nextword$
nextword$=nextword$+mid$(message$,t)
` Check if that letter is a space or |
if mid$(message$,t)=" " or mid$(message$,t)="|" or mid$(message$,t)="*"
` Check if the current across coordinate + the size of the
` word will go beyond the designated width
if across+text width(nextword$)>wide
` and if it does, reset across and increase down coordinates
across=320-tw
inc down,text height(nextword$)
endif
if mid$(message$,t)="|"
across=320-tw
inc down,text height(nextword$)
` Go to this point to avoid writing this |
goto here
endif
if mid$(message$,t)="*"
wait 1000
` Go to this point to avoid writing this *
goto here
endif
` Print the text
text across,down,nextword$
` Increase the across coordinate by the size of the word
inc across,text width(nextword$)
` Come to this point to avoid writing this | or this *
here:
` Clear the current word to make a new word next loop
nextword$=""
` A small pause so you can see it working
wait 200
endif
next t
endfunction
and finally there's 2 here, one going to the start of the programme and one to the end of the programme. There's also two exit statements in this one. What's the view on these?
This is part of a subroutine as can be seen from the return statement
` Until mouse is clicked
do
if mouseclick()=1
goto start
exit
endif
if mouseclick()=2
goto the_end
exit
endif
` End loop
Loop
return
Would be interested to hear
gearce
LANG MAY YER LUM REEK