A good problem for you experts

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
Sinuvoid
Veteran
Posts: 155
Joined: Wed Jul 25, 2007 8:20 am

A good problem for you experts

Post 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
Nodtveidt
Veteran
Posts: 826
Joined: Sun Jul 25, 2004 4:24 am
Location: Quebradillas, PR
Contact:

Post 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.
Sinuvoid
Veteran
Posts: 155
Joined: Wed Jul 25, 2007 8:20 am

Post by Sinuvoid »

THanks, this isnt my code. Its a friend of mines who wanted it converted :p Thanks alot man!
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post 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
Last edited by burger2227 on Sun Dec 16, 2007 7:26 pm, edited 1 time in total.
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
Sinuvoid
Veteran
Posts: 155
Joined: Wed Jul 25, 2007 8:20 am

Post by Sinuvoid »

Me? Like I said before, this IS NOT my code. I dont code in GWbasic, ONLY Qbasic :P
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

I thought GW was dead! OOPS, he is our president boo hoo.

But that rule is true in QB too.

:roll:
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
User avatar
coma8coma1
Veteran
Posts: 100
Joined: Sat Dec 08, 2007 5:29 pm
Location: Maryland, USA

Post by coma8coma1 »

i did not know you could use parenthesis in IF..THEN statements. This could simplify some of my sloppy code!
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post 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
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
Mac
Veteran
Posts: 151
Joined: Mon Aug 06, 2007 2:00 pm

Post 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
User avatar
coma8coma1
Veteran
Posts: 100
Joined: Sat Dec 08, 2007 5:29 pm
Location: Maryland, USA

parentheses

Post by coma8coma1 »

does this apply to QuickBASIC, or to GW BASIC only
Mac
Veteran
Posts: 151
Joined: Mon Aug 06, 2007 2:00 pm

Re: parentheses

Post 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
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

I dunno Mac

Post 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
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
Mac
Veteran
Posts: 151
Joined: Mon Aug 06, 2007 2:00 pm

Re: I dunno Mac

Post 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
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post 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
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
Post Reply