Page 1 of 1

A good problem for you experts

Posted: Sat Dec 15, 2007 5:09 pm
by Sinuvoid
OK, this is from GWbasic. Recreate the code in Qbasic.
Any questions please ask. btw, != is doesnt equal to. Hahve fun and Good luck!
Here's da code!

Code: Select all

10 CLS
20 AButton# = 0
30 CDown# = 1
40 CRight# = 2
50 CLeft# = 3
60 CUp# = 4
63 Print "A-" AButton# "CD-" CDown# "CR-" CRight# "CL-" CLeft# "CU-" CUp#
67 Input Nothing$
70 If CUp# < 255 And != CUp# - 1 Or CLeft# - 1 Or CRight# -1 Or CDown# - 1 Or AButton# - 1 Then CUp# = CUp# + 1 And Goto 63
80 If CUp# < 254 And = CUp# - 1 Or CLeft# - 1 Or CRight# -1 Or CDown# - 1 Or AButton# - 1 Then CUp# = CUp# + 2 And Goto 63 Else CUp = 0
90 If CLeft# < 255 And != CUp# - 1 Or CLeft# - 1 Or CRight# -1 Or CDown# - 1 Or AButton# - 1 Then CLeft# = CLeft# + 1 And Goto 63
100 If CLeft# < 254 And = CUp# - 1 Or CLeft# - 1 Or CRight# -1 Or CDown# - 1 Or AButton# - 1 Then CLeft# = CLeft# + 2 And Goto 63 Else CLeft = 0
110 If CRight# < 255 And != CUp# - 1 Or CLeft# - 1 Or CRight# -1 Or CDown# - 1 Or AButton# - 1 Then CRight# = CRight# + 1 And Goto 63
120 If CRight# < 254 And = CUp# - 1 Or CLeft# - 1 Or CRight# -1 Or CDown# - 1 Or AButton# - 1 Then CRight# = CRight# + 2 And Goto 63 Else CRight = 0
130 If CDown# < 255 And != CUp# - 1 Or CLeft# - 1 Or CRight# -1 Or CDown# - 1 Or AButton# - 1 Then CDown# = CDown# + 1 And Goto 63
140 If CDown# < 254 And = CUp# - 1 Or CLeft# - 1 Or CRight# -1 Or CDown# - 1 Or AButton# - 1 Then CDown# = CDown# + 2 And Goto 63 Else CDown = 0
150 If AButton# < 255 And != CUp# - 1 Or CLeft# - 1 Or CRight# -1 Or CDown# - 1 Or AButton# - 1 Then AButton# = AButton# + 1 And Goto 63
160 If AButton# < 254 And = CUp# - 1 Or CLeft# - 1 Or CRight# -1 Or CDown# - 1 Or AButton# - 1 Then AButton# = AButton# + 2 And Goto 63 Else Goto 10

Posted: Sat Dec 15, 2007 8:00 pm
by Nodtveidt
I think this is what you need:

Code: Select all

startofprogram:
CLS
AButton# = 0
CDown# = 1
CRight# = 2
CLeft# = 3
CUp# = 4
mainloop:
Print "A-"; AButton#; "CD-"; CDown#; "CR-"; CRight#; "CL-"; CLeft#; "CU-"; CUp#
Input Nothing$
If CUp# < 255 And (CUp# <> CUp# - 1 Or CUp# = CLeft# - 1 Or CUp# = CRight# -1 Or CUp# = CDown# - 1 Or CUp# = AButton# - 1) Then
  CUp# = CUp# + 1
  Goto mainloop
End If
If CUp# < 254 And (CUp# =  CUp# - 1 Or CUp# = CLeft# - 1 Or CUp# = CRight# -1 Or CUp# = CDown# - 1 Or CUp# = AButton# - 1) Then
  CUp# = CUp# + 2
  Goto mainloop
Else
  CUp = 0
End If
If CLeft# < 255 And (CLeft# <> CUp# - 1 Or CLeft# = CLeft# - 1 Or CLeft# = CRight# -1 Or CLeft# = CDown# - 1 Or CLeft# = AButton# - 1) Then
  CLeft# = CLeft# + 1
  Goto mainloop
End If
If CLeft# < 254 And (CLeft# =  CUp# - 1 Or CLeft# = CLeft# - 1 Or CLeft# = CRight# -1 Or CLeft# = CDown# - 1 Or CLeft# = AButton# - 1) Then
  CLeft# = CLeft# + 2
  Goto mainloop
Else
  CLeft = 0
End If
If CRight# < 255 And (CRight# <> CUp# - 1 Or CRight# = CLeft# - 1 Or CRight# = CRight# -1 Or CRight# = CDown# - 1 Or CRight# = AButton# - 1 Then)
  CRight# = CRight# + 1
  Goto mainloop
End If
If CRight# < 254 And (CRight# =  CUp# - 1 Or CRight# = CLeft# - 1 Or CRight# = CRight# -1 Or CRight# = CDown# - 1 Or CRight# = AButton# - 1) Then
  CRight# = CRight# + 2
  Goto mainloop
Else
  CRight = 0
End If
If CDown# < 255 And (CDown# <> CUp# - 1 Or CDown# = CLeft# - 1 Or CDown# = CRight# -1 Or CDown# = CDown# - 1 Or CDown# = AButton# - 1) Then
  CDown# = CDown# + 1
  Goto mainloop
End If
If CDown# < 254 And (CDown# =  CUp# - 1 Or CDown# = CLeft# - 1 Or CDown# = CRight# -1 Or CDown# = CDown# - 1 Or CDown# = AButton# - 1) Then
  CDown# = CDown# + 2
  Goto mainloop
Else
  CDown = 0
End If
If AButton# < 255 And (AButton# <> CUp# - 1 Or AButton# = CLeft# - 1 Or AButton# = CRight# -1 Or AButton# = CDown# - 1 Or AButton# = AButton# - 1) Then
  AButton# = AButton# + 1
  Goto mainloop
End If
If AButton# < 254 And (AButton# = CUp# - 1 Or AButton# = CLeft# - 1 Or AButton# = CRight# -1 Or AButton# = CDown# - 1 Or AButton# = AButton# - 1) Then
  AButton# = AButton# + 2
  Goto mainloop
Else
  Goto startofprogram
End If
Not exactly sure what you're trying to accomplish here but that looks like some mighty inefficient code. I'm pretty sure I interpreted the logic flow correctly though, but you're dealing with some archaic stuff there.

Posted: Sun Dec 16, 2007 8:54 am
by Sinuvoid
THanks, this isnt my code. Its a friend of mines who wanted it converted :p Thanks alot man!

Posted: Sun Dec 16, 2007 7:18 pm
by burger2227
Great! Now, do you understand what he actually did?

You can use parenthesis in IF statements when you have many things to compare. Without them, any OR could make the statement true without the other operations.

Ted

PS: Do you really know how to code? You ask an awful lot of questions LOL

Posted: Sun Dec 16, 2007 7:25 pm
by Sinuvoid
Me? Like I said before, this IS NOT my code. I dont code in GWbasic, ONLY Qbasic :P

Posted: Sun Dec 16, 2007 7:28 pm
by burger2227
I thought GW was dead! OOPS, he is our president boo hoo.

But that rule is true in QB too.

:roll:

Posted: Sun Dec 16, 2007 7:33 pm
by coma8coma1
i did not know you could use parenthesis in IF..THEN statements. This could simplify some of my sloppy code!

Posted: Sun Dec 16, 2007 11:33 pm
by burger2227
You MUST use parenthesis when using the AND, OR and XOR numerical operators in an IF statement as below. These operators are not the same thing as normal IF - AND - OR statements:

Code: Select all

IF (INP(889) AND 128) THEN PRINT "Valid data"
The evaluation will be wrong if you do not use it! The code looks for a bit to be high in a byte value returned from a LPT status port.

Ted

Posted: Mon Dec 17, 2007 3:28 pm
by Mac
burger2227 wrote:IF (INP(889) AND 128) THEN PRINT "Valid data"
While I agree that one should always use parentheses in order to avoid bugs caused by one's faulty application of the order of operations, I don't follow your example.

Code: Select all

IF 321 AND 128 THEN PRINT 1 ELSE PRINT 2
IF (321) AND (128) THEN PRINT 1 ELSE PRINT 2
IF (321 AND 128) THEN PRINT 1 ELSE PRINT 2
All statements give the same result, so parentheses were optional.

Mac

parentheses

Posted: Mon Dec 17, 2007 5:38 pm
by coma8coma1
does this apply to QuickBASIC, or to GW BASIC only

Re: parentheses

Posted: Mon Dec 17, 2007 9:11 pm
by Mac
coma8coma1 wrote:does this apply to QuickBASIC, or to GW BASIC only
Suggestion: take something which you wonder works on the same.

1) Try in on QuickBASIC
2) Try it on GW BASIC

I use neither. I use QBasic. My guess is they are the same.

Mac

I dunno Mac

Posted: Tue Dec 18, 2007 12:38 am
by burger2227
You start playing with fire and your example is not the best either, but neither was mine looking back.

Code: Select all

IF INP(889) AND 128 > 0 THEN
What will happen there? It will run every time the port has a reading, because 128 is always more than 0. Also it could be confused with a conditional operation instead of a number one. QB will be confused too! That is why I use brackets for all numerical operations in IF statements. It also makes the code easier to understand.

By the way, the highest number can only be 248 LOL.

Ted

Re: I dunno Mac

Posted: Tue Dec 18, 2007 7:57 am
by Mac
burger2227 wrote:your example is not the best either, but neither was mine looking back.
True. Let's clarify further

These two always give the same result:
?????IF (INP(889) AND 128) THEN
?????IF INP(889) AND 128 THEN
so you retract the statement that the outer parentheses are useful in that case and agree that the outer parentheses are wasted, i.e.
?????IF (anything you test) THEN
?????IF anything you test THEN

On the other hand, these can give different results:
?????IF (INP(889) AND 128) > 0 THEN
?????IF INP(889) AND 128 > 0 THEN

Because, unknown to the programmer, the order of operations makes the latter test become
?????IF INP(889) AND (128 > 0) THEN

So we both always agreed that programmers should always use parentheses to make the order of operations explicit rather then rely on possible faulty memory of the QBasic order of operations. I just replied in this thread that the recommendation to put superfluous parentheses around the
?????IF ( ) THEN
was not useful.

Here is a program the OP can run on GW BASIC and QuickBasic:

Code: Select all

CLS
a = 0: GOSUB Tests
a = 1: GOSUB Tests
SYSTEM
Tests:
IF (a AND 128) THEN PRINT "A";??ELSE PRINT "B";
IF a AND 128 THEN PRINT "A";??ELSE PRINT "B";
IF (a AND 128) > 0 THEN PRINT "A";??ELSE PRINT "B";
IF a AND 128 > 0 THEN PRINT "A";??ELSE PRINT "B";
IF a AND (128 > 0) THEN PRINT "A";??ELSE PRINT "B";
PRINT
RETURN

Posted: Wed Dec 19, 2007 8:42 pm
by burger2227
True Mac. I just don't see a great need for not using the brackets. I don't want to have to troubleshoot those kind of problems. The brackets just keep anything odd from happening.

:roll:
Ted