QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: Dimster on March 16, 2021, 03:41:25 pm

Title: Not Sure What has happened here in my use of IMP logical operator
Post by: Dimster on March 16, 2021, 03:41:25 pm
I'm trying to use IMP in my program and coming up with some odd results. Not sure why the inconsistent results. I do know I can do a work around with NOT, AND and OR . So here is some code to give you an idea of the math I'm using in my program

Code: QB64: [Select]
  1. a = 3: Print "value of a = "; a
  2. b = 1: Print "value of b = "; b
  3. c$ = ""
  4. Print "NOTE: A is Greater than B.....so if number A = 3 and next number B is less than A (at 1 in this case), then this is a TRENDING DOWN situation"
  5. Print " Equation is 'If a < b And b > 1 Then c = a Imp b'    "
  6. If a < b And b > 1 Then c = a Imp b
  7. If c = -1 Then c$ = "Trending UP"
  8. If c = 0 Then c$ = "Trending Down"
  9. Print "Result is "; c$;
  10. If c$ = "Trending Down" Then Print " ... Correct Result" Else Print " ... Incorrect Result"
  11.  
  12.  
  13.  
  14.  
  15.  
  16. a = 1: Print "value of a = "; a
  17. b = 8: Print "value of b = "; b
  18. c$ = ""
  19. Print "NOTE: A is Less than B.....so if number A = 1 and next number B is greater than 1 (at 8 in this case), then this is a TRENDING UP situation"
  20. Print " Equation is 'If a < b And b > 1 Then c = a Imp b'    "
  21.  
  22. If a < b And b > 1 Then c = a Imp b
  23. If c = -1 Then c$ = "Trending UP"
  24. If c = 0 Then c$ = "Trending Down"
  25. Print "Result is "; c$;
  26. If c$ = "Trending UP" Then Print " ... Correct Result" Else Print " ... Incorrect Result"
  27.  
  28.  
  29. 'Sleep
  30.  
  31.  
  32. a = 1: Print a; " = value of a"
  33. b = 3: Print b; " = value of b"
  34. Print "NOTE: A is Less than B.....so if number of A = 1 and next number of success is greater than 1 (at 3 in this case), then this is a TRENDING UP situation"
  35. Print " Equation is 'If a < b And b > 1 Then c = a Imp b'    "
  36.  
  37. If a < b And b > 1 Then c = a Imp b
  38. If c = -1 Then c$ = "Trending UP"
  39. If c = 0 Then c$ = "Trending Down"
  40. Print "Result is "; c$;
  41.  
  42. If c$ = "Trending UP" Then Print " ... Correct Result" Else Print " ... Incorrect Result"
  43.  
  44.  

The equation is the same in each of the 3 cases, only the value of "8" is different. I'm not sure why equation 1 and 3 are producing the correct result and equation 2 is not.
Title: Re: Not Sure What has happened here in my use of IMP logical operator
Post by: TempodiBasic on March 16, 2021, 08:36:35 pm
Hi  Dimster
I think that you have loosen the point because you types
Code: QB64: [Select]
  1. THEN c = a IMP b
  buuutttt you mean
Code: QB64: [Select]
  1. THEN  c = (a<b) IMP (b>1)
.
Give a try because for now you have calculated   True IMP True as any positive number is true.
Title: Re: Not Sure What has happened here in my use of IMP logical operator
Post by: Dimster on March 17, 2021, 08:22:22 am
@TempodiBasic  Thank You - that is indeed what I was missing.
Title: Re: Not Sure What has happened here in my use of IMP logical operator
Post by: Dimster on March 17, 2021, 09:03:46 am
@TempodiBasic  - so I thought the matter was solved but here is that same routine with the changes in the IMP format. The result were ok until I added the last one where a=8 and b =1. This is virtually the identical calculation as the 1st one where a = 3 and b =1 but for some reason the math formula is failing.

What appears to be happening is... the previous value for c$ is simply being repeated and the evaluation of the formula is either being skipped or producing a sum with is not a -1 or 0. The logic of the equation seems to be correct. I must be missing something in the formula again.

Code: QB64: [Select]
  1. a = 3: Print "value of a = "; a
  2. b = 1: Print "value of b = "; b
  3. Print "NOTE: A is Greater than B.....so if number A = 3 and next number B is less than A (at 1 in this case), then this is a TRENDING DOWN situation"
  4. Print " Equation is 'If a < b And b > 1 Then c = (a<b) Imp (b>1)'    "
  5. If a < b And b > 1 Then c = (a < b) Imp (b > 1)
  6. If c = -1 Then c$ = "Trending UP"
  7. If c = 0 Then c$ = "Trending Down"
  8. Print "Result is "; c$;
  9. If c$ = "Trending Down" Then Print " ... Correct Result" Else Print " ... Incorrect Result"
  10.  
  11.  
  12.  
  13.  
  14.  
  15. a = 1: Print "value of a = "; a
  16. b = 8: Print "value of b = "; b
  17. Print "NOTE: A is Less than B.....so if number A = 1 and next number B is greater than 1 (at 8 in this case), then this is a TRENDING UP situation"
  18. Print " Equation is 'If a < b And b > 1 Then c = (a<b) Imp (b>1)'    "
  19.  
  20. If a < b And b > 1 Then c = (a < b) Imp (b > 1)
  21. If c = -1 Then c$ = "Trending UP"
  22. If c = 0 Then c$ = "Trending Down"
  23. Print "Result is "; c$;
  24. If c$ = "Trending UP" Then Print " ... Correct Result" Else Print " ... Incorrect Result"
  25.  
  26.  
  27. 'Sleep
  28.  
  29.  
  30. a = 1: Print a; " = value of a"
  31. b = 1: Print b; " = value of b"
  32. Print "NOTE: A is Equal B.....so if number of A = 1 and next number of B is equal to  1 (as in this case), then this is TRENDING Steady but as Steady is not a choice and is NOT Down then TRENDING UP is the answer"
  33. Print " Equation is 'If a < b And b > 1 Then c = (a<b) Imp (b>1)'    "
  34. If a < b And b > 1 Then c = (a < b) Imp (b > 1)
  35. If c = -1 Then c$ = "Trending UP"
  36. If c = 0 Then c$ = "Trending Down"
  37. Print "Result is "; c$;
  38. If c$ = "Trending UP" Then Print " ... Correct Result" Else Print " ... Incorrect Result"
  39.  
  40.  
  41. a = 8: Print "value of a = "; a
  42. b = 1: Print "value of b = "; b
  43. Print "NOTE: A is Greater than B.....so if number A = 8 and next number B is less than A (at 1 in this case), then this is a TRENDING Down situation"
  44. Print " Equation is 'If a < b And b > 1 Then c = (a<b) Imp (b>1)'    "
  45.  
  46. If a < b And b > 1 Then c = (a < b) Imp (b > 1)
  47. If c = -1 Then c$ = "Trending UP"
  48. If c = 0 Then c$ = "Trending Down"
  49. Print "Result is "; c$;
  50. If c$ = "Trending Down" Then Print " ... Correct Result" Else Print " ... Incorrect Result"
  51.  
  52.  
Title: Re: Not Sure What has happened here in my use of IMP logical operator
Post by: TempodiBasic on March 17, 2021, 08:16:25 pm
Hi Dimster
Let:s debug
Using the table True False

a.  b   (a<b)   (b>1)      IMP.    Trending 
3   1    0         0         0 imp 0 =-1    UP
1   8    - 1     - 1      - 1 imp - 1 =- 1  UP
1   1     0        0        0 imp 0 =-1      UP
8   1     0        0        0  imp 0=-1    UP

Well it seems that we always got UP  and so you must correct the lines 14 and 72 that are wrong.



Title: Re: Not Sure What has happened here in my use of IMP logical operator
Post by: TempodiBasic on March 18, 2021, 06:45:20 pm
Hi Dimster
this evening I can use the QB64 Ide so I can test and MOD your code to verify the goal

Well in the first algorythm there is a bug...
when in the code we test
Code: QB64: [Select]
  1.  If a < b And b > 1 Then c = (a < b) Imp (b > 1)
we have not thought that the part
Code: QB64: [Select]
  1.  c = (a < b) Imp (b > 1)
will be executed only when the 2 conditions are true, so we can get only the Trending UP or a null action (using the flag security tip, otherwise we don't reset at each pass the c variable).

I found it when I wanted to write in modular way your code using a main and a SUB.
Here mod that shows the differences
Code: QB64: [Select]
  1. 'Print
  2. 'Print
  3. 'a = 3: Print "value of a = "; a
  4. 'b = 1: Print "value of b = "; b
  5. 'Print "NOTE: A is Greater than B.....so if number A = 3 and next number B is less than A (at 1 in this case), then this is a TRENDING DOWN situation"
  6. 'Print " Equation is 'If a < b And b > 1 Then c = (a<b) Imp (b>1)'    "
  7. 'Print
  8. 'If a < b And b > 1 Then c = (a < b) Imp (b > 1)
  9. 'If c = -1 Then c$ = "Trending UP"
  10. 'If c = 0 Then c$ = "Trending Down"
  11. 'Print
  12. 'Print "Result is "; c$;
  13. 'Sleep
  14. 'If c$ = "Trending Down" Then Print " ... Correct Result" Else Print " ... Incorrect Result"
  15.  
  16.  
  17.  
  18. 'Sleep
  19.  
  20.  
  21. 'Print
  22. 'Print
  23. 'a = 1: Print "value of a = "; a
  24. 'b = 8: Print "value of b = "; b
  25. 'Print
  26. 'Print "NOTE: A is Less than B.....so if number A = 1 and next number B is greater than 1 (at 8 in this case), then this is a TRENDING UP situation"
  27. 'Print " Equation is 'If a < b And b > 1 Then c = (a<b) Imp (b>1)'    "
  28.  
  29. 'If a < b And b > 1 Then c = (a < b) Imp (b > 1)
  30. 'If c = -1 Then c$ = "Trending UP"
  31. 'If c = 0 Then c$ = "Trending Down"
  32. 'Print
  33. 'Print "Result is "; c$;
  34. 'Sleep
  35. 'If c$ = "Trending UP" Then Print " ... Correct Result" Else Print " ... Incorrect Result"
  36.  
  37.  
  38. ''Sleep
  39.  
  40.  
  41. 'Sleep
  42. 'Print
  43. 'Print
  44. 'a = 1: Print a; " = value of a"
  45. 'b = 1: Print b; " = value of b"
  46. 'Print "NOTE: A is Equal B.....so if number of A = 1 and next number of B is equal to  1 (as in this case), then this is TRENDING Steady but as Steady is not a choice and is NOT Down then TRENDING UP is the answer"
  47. 'Print " Equation is 'If a < b And b > 1 Then c = (a<b) Imp (b>1)'    "
  48. 'If a < b And b > 1 Then c = (a < b) Imp (b > 1)
  49. 'If c = -1 Then c$ = "Trending UP"
  50. 'If c = 0 Then c$ = "Trending Down"
  51. 'Print
  52. 'Print "Result is "; c$;
  53. 'Sleep
  54. 'If c$ = "Trending UP" Then Print " ... Correct Result" Else Print " ... Incorrect Result"
  55.  
  56. 'Sleep
  57.  
  58. 'Print
  59. 'Print
  60. 'a = 8: Print "value of a = "; a
  61. 'b = 1: Print "value of b = "; b
  62. 'Print
  63. 'Print "NOTE: A is Greater than B.....so if number A = 8 and next number B is less than A (at 1 in this case), then this is a TRENDING Down situation"
  64. 'Print " Equation is 'If a < b And b > 1 Then c = (a<b) Imp (b>1)'    "
  65.  
  66. 'If a < b And b > 1 Then c = (a < b) Imp (b > 1)
  67. 'If c = -1 Then c$ = "Trending UP"
  68. 'If c = 0 Then c$ = "Trending Down"
  69. 'Print
  70. 'Print "Result is "; c$;
  71. 'Sleep
  72. 'If c$ = "Trending Down" Then Print " ... Correct Result" Else Print " ... Incorrect Result"
  73. For d = 1 To 2 Step 1
  74.     If d = 1 Then m$ = "Dimster" Else If d = 2 Then m$ = "Tempo"
  75.     Cls
  76.     Print " New IMP test  it has result False and Trending down"; " mode "; m$
  77.     ImpAB 0, 1, m$ ' True Imp False  --> False
  78.     Print " New IMP test  it has result True and Trending up"; " mode "; m$
  79.     ImpAB 3, 1, m$ ' False Imp False --> True
  80.     Print " New IMP test  it has result True and Trending up"; " mode "; m$
  81.     ImpAB 3, 4, m$ ' True Imp True --> True
  82.     Print " New IMP test  it has result True and Trending up"; " mode "; m$
  83.     ImpAB 3, 2, m$ ' False Imp True --> True
  84.  
  85.  
  86. Sub ImpAB (a As Integer, b As Integer, Mode As String)
  87.     Print
  88.     Print
  89.     Print "value of a = "; a
  90.     Print "value of b = "; b
  91.     If a < b Then
  92.         Print "NOTE: A is Less than B....."
  93.         If Mode = "Dimster" And (b <= 1) Then
  94.             Print "  and if b<=1 "
  95.             Print " the Equation 'If a < b And b > 1 Then c = (a<b) Imp (b>1)' is never executed   "
  96.         End If
  97.     ElseIf a > b Then
  98.         Print "NOTE: A is Greater than B.....and with both if B>1 both B<1 IMP gives True "
  99.         If b > 1 Then
  100.             Print " the Equation 'If a < b And b > 1 Then c = (a<b) Imp (b>1)' is True "
  101.         Else
  102.             Print " the Equation 'If a < b And b > 1 Then c = (a<b) Imp (b>1)' is never executed   "
  103.         End If
  104.     End If
  105.     If Mode = "Dimster" Then Print " so the condition Trending Down cannot be reached!"
  106.     c = 99 ' flag of initialization
  107.     If Mode = "Dimster" Then
  108.         If a < b And b > 1 Then c = (a < b) Imp (b > 1)
  109.     ElseIf Mode = "Tempo" Then
  110.         c = (a < b) Imp (b > 1)
  111.     End If
  112.     If c = -1 Then c$ = "Trending UP"
  113.     If c = 0 Then c$ = "Trending Down"
  114.     Print
  115.     Print "Result is "; c$;
  116.     Print
  117.     Sleep

I hope that you find interesting the bug of no reset of variable c and the bug of no calculate c when b>1 is false.
Good Coding
Title: Re: Not Sure What has happened here in my use of IMP logical operator
Post by: Dimster on March 19, 2021, 09:09:09 am
@TempodiBasic - Thank You very much for your examples - I am studying them closely. In my main program I am using the very simple, tried & true, formula of " If A => B then "Trending UP" else "Trending Down".

When there are only the two variables, perhaps IMP is not the best choice (ie in terms of accuracy of results or flexibility of use Logically). And I do see your point where that very first Logical decision ( ie A > B AND B > 1) leads to a failure to engage the IMP condition in the  C = (A < B) IMP (B > 1) in a number of scenarios where the values of A & B vary. Steve gave me a really good example using only two variables ( Your alive or Not alive ) a little while ago. Since then I have come across a number of occasions where I can see one result produced in my program would imply a Trend. Which has me exploring IMP again.

So its back to the drawing board. Thanks again Tempodi
Title: Re: Not Sure What has happened here in my use of IMP logical operator
Post by: luke on March 19, 2021, 11:00:59 am
For reference, a IMP b is equivalent to (NOT a) OR b.
Title: Re: Not Sure What has happened here in my use of IMP logical operator
Post by: Dimster on March 19, 2021, 01:05:51 pm
@luke - Thanks Luke. I was aware of that, and that IMP returns a value of -1 meaning all bits are set, or 0 meaning no bits are set (according to the wiki). Steve gave me an example and he was using -1 and 0 in the results. But when I research IMP beyond our wiki, I'm getting info that the bit wise comparison for IMP results in 1 and 0, not -1 and 0. Which is subtly confusing. Plus in terms of Logical Operator (AND, OR, XOR), the results are True = 1 and False = 0. I am working on the Eureka Moment - making IMP work if the goal.
Title: Re: Not Sure What has happened here in my use of IMP logical operator
Post by: bplus on March 19, 2021, 05:15:58 pm
With Boolean True or False (only), anything not = 0 is True. So -1 or 1 or 1,000,000 no difference, all True!
Title: Re: Not Sure What has happened here in my use of IMP logical operator
Post by: TempodiBasic on March 25, 2021, 06:24:07 pm
Hi guys and gals

Hi Dimster

Here I post a demonstration to show how to use IMP in the logical managing of the move of a character on the screen and the alternative more known OR.  In the top left corner you can see the result of detector FUNCTION, in the Top Right corner you can see the type of detector FUNCTION at work, in the bottom of the window you can read the help for user, the title of window remembers you that it is a Demo.

Code: QB64: [Select]
  1. ' Demo in two languages  / Dimostrazione in due linguaggi
  2. Screen 0 'screen text mode / modalit… schermo solo testo
  3. _Title "Demo OR and IMP to move a character on screen" ' setting title of window / impostare il titolo della finestra
  4. ' defining useful constants  / definire costanti utili
  5. Const X = 1, Y = 2, Life = 3, True = -1, False = 0, ModeOr = 10, ModeImp = 20
  6. ' defining variables and arrays  / definire variabili ed array
  7. Dim Hero(1 To 3) As Integer, Limit(1 To 2, 1 To 2) As Integer, ModeEvaluation As Integer
  8.  
  9. 'initialization of variables  / inizializzazione delle variabili
  10. Hero(X) = (Rnd * 79) + 1
  11. Hero(Y) = (Rnd * 22) + 1
  12. Hero(Life) = True
  13. Limit(1, X) = 10
  14. Limit(1, Y) = 2
  15. Limit(2, X) = 70
  16. Limit(2, Y) = 19
  17. ModeEvaluation = ModeImp
  18.  
  19. ' main loop  /ciclo principale
  20.     Cls ' refresh screen  / ripulisce lo schermo
  21.  
  22.     ' manage evaluation mode and its results / gestisce la modalit… di valutazione e i suoi risultati
  23.     Locate 1, 1
  24.     If ModeEvaluation = ModeImp Then
  25.         If outheRange(Limit(1, X), Limit(2, X), Hero(X)) Or outheRange(Limit(1, Y), Limit(2, Y), Hero(Y)) Then Print "Out of game" Else Print " In game"
  26.  
  27.     ElseIf ModeEvaluation = ModeOr Then
  28.         If IntheRange(Limit(1, X), Limit(2, X), Hero(X)) And IntheRange(Limit(1, Y), Limit(2, Y), Hero(Y)) Then Print " In Game" Else Print "Out Of Game"
  29.     End If
  30.  
  31.     ' border of game field  / bordo del campo di gioco
  32.     Locate Limit(1, Y), Limit(1, X)
  33.     Print String$(Abs(Limit(1, X) - Limit(2, X)), Chr$(178));
  34.     For a = Limit(1, Y) To Limit(2, Y) Step 1
  35.         Locate a, Limit(1, X): Print Chr$(178)
  36.         Locate a, Limit(2, X): Print Chr$(178)
  37.     Next
  38.     Locate Limit(2, Y), Limit(1, X)
  39.     Print String$(Abs(Limit(1, X) - Limit(2, X)), Chr$(178))
  40.  
  41.     'hero / l'eroe
  42.     Locate Hero(Y), Hero(X)
  43.     Print "";
  44.  
  45.     'Helpscreen   / la guida a schermo
  46.     Locate 1, 60: Print "ModeEvaluation: ";
  47.     If ModeEvaluation = ModeImp Then Print "IMP"; Else Print " OR";
  48.     Locate 24, 1: Print " cursors to  move Hero, Enter key to switch ModeEvaluation, Space to quit";
  49.  
  50.     ' input and command manager     / gestore dell'input e dei comandi
  51.     a$ = InKey$
  52.     Select Case a$
  53.         Case Chr$(0) + "M"
  54.             If Hero(X) < 80 Then Hero(X) = Hero(X) + 1
  55.         Case Chr$(0) + "H"
  56.             If Hero(Y) > 1 Then Hero(Y) = Hero(Y) - 1
  57.         Case Chr$(0) + "P"
  58.             If Hero(Y) < 25 Then Hero(Y) = Hero(Y) + 1
  59.         Case Chr$(0) + "K"
  60.             If Hero(X) > 1 Then Hero(X) = Hero(X) - 1
  61.         Case "K"
  62.             Hero(Life) = False
  63.         Case Chr$(32)
  64.             End
  65.         Case Chr$(13)
  66.             If ModeEvaluation = ModeImp Then ModeEvaluation = ModeOr Else ModeEvaluation = ModeImp
  67.     End Select
  68.     _Limit 20 ' save CPU  / risparmia la CPU
  69. End ' Endo of program  /fine del programma
  70.  
  71. ' SUB and FUNCTION area  / area delle SUB e delle FUNCTION
  72. Function outheRange (min As Integer, max As Integer, value As Integer)
  73.     outheRange = (min < value Imp value > max)
  74.  
  75. Function IntheRange (min As Integer, max As Integer, Value As Integer)
  76.     If min > Value Or max < Value Then IntheRange = 0 Else IntheRange = -1
  77.  

  [ This attachment cannot be displayed inline in 'Print Page' view ]  
Title: Re: Not Sure What has happened here in my use of IMP logical operator
Post by: Dimster on March 26, 2021, 09:06:58 am
I am impressed and confused @TempodiBasic

Here is my equation :  "If a < b And b > 1 Then c = (a<b) Imp (b>1)'    " If you remove the IF statement the equation becomes "c = (a<b) Imp (b>1)"

And here is your equation : "outheRange = (min < value Imp value > max)".

I do see a lot of difference between them. In your coding min, max and value are all calculate, whereas in mine I have fixed the values of a,b and fixed the value of 1. If I substitute a = min and b= max, then my equation becomes (min < max IMP max > 1) whereas yours converts to (a <1 IMP 1 > b)

In my mind, my equation is much more logical than yours in as much as min being less than max ( ie my a's & b's convert to your equation becomes ..(min < max IMP max > 1)... is very logical and max great than 1 pretty well a "must be obvious statement" but my equation doesn't work whereas your equation does works.

In your equation which I converted to reflect my a's and b's ...(a <1 IMP 1 > b)... i'm having difficult seeing how a < 1 , which indicates a value of a as zero or negative, can IMPLY 1 will be greater than b.

I will get it - one day - I  hope.
Title: Re: Not Sure What has happened here in my use of IMP logical operator
Post by: Pete on March 26, 2021, 01:35:26 pm
@TempodiBasic

Well, it has SCREEN 0 and Hero in it, so I had to run it! Hey, this takes me back. We had a challenge the QB64 forum 20 years ago to write this type of movement app in the fewest lines possible. I won! Michael Calkins hates me to this day, and he didn't even enter the contest, go figure. Anyway, I can't recall if I used the IMP function in that or not. I think it took just a little over 20 lines to code, no colons.

Well back to work, but thanks for a walk down QB memory lane.

Pete
Title: Re: Not Sure What has happened here in my use of IMP logical operator
Post by: bplus on March 26, 2021, 05:33:01 pm
@TempodiBasic

Well, it has SCREEN 0 and Hero in it, so I had to run it! Hey, this takes me back. We had a challenge the QB64 forum 20 years ago to write this type of movement app in the fewest lines possible. I won! Michael Calkins hates me to this day, and he didn't even enter the contest, go figure. Anyway, I can't recall if I used the IMP function in that or not. I think it took just a little over 20 lines to code, no colons.

Well back to work, but thanks for a walk down QB memory lane.

Pete

Nice challenge Pete, since I wasn't there then here is my hat in the ring:
Code: QB64: [Select]
  1. hero = Int(Rnd * 25 * 80) + 1
  2.     Cls
  3.     For y = 2 To 19
  4.         For x = 10 To 70
  5.             If y = 2 Or y = 19 Or x = 10 Or x = 70 Then
  6.                 Locate y, x
  7.                 Print Chr$(178)
  8.             End If
  9.             If (y - 1) * 80 + x - 1 = hero Then Locate 25, 32: Print "Hero is inbounds.";
  10.         Next
  11.     Next
  12.     Locate hero \ 80 + 1, (hero Mod 80) + 1
  13.     Print Chr$(1);
  14.     a$ = InKey$
  15.     If a$ = Chr$(0) + "M" Then hero = hero - (((hero Mod 80) + 1) <> 80)
  16.     If a$ = Chr$(0) + "K" Then hero = hero + (((hero Mod 80)) <> 0)
  17.     If a$ = Chr$(0) + "H" Then hero = hero + 80 * ((hero \ 80) > 0)
  18.     If a$ = Chr$(0) + "P" Then hero = hero - 80 * ((hero \ 80) < 24)
  19.     _Limit 10
  20.  

Sorry one colon. :(

Edit: 4 less lines swapping variables for literals.
Title: Re: Not Sure What has happened here in my use of IMP logical operator
Post by: bplus on March 27, 2021, 12:34:29 pm
@TempodiBasic

Seems this is slightly better way to write InRange:
Code: QB64: [Select]
  1. Function InRange% (min As Integer, max As Integer, Value As Integer)
  2.     If Value >= min And Value <= max Then InRange% = -1
  3.  
exactly to the point, though doing everything in Long Type covers more ground.

What do you think?
Title: Re: Not Sure What has happened here in my use of IMP logical operator
Post by: TempodiBasic on March 27, 2021, 07:21:28 pm
@Pete  And @bplus
here the srhinked version of my demo into 17 lines

Code: QB64: [Select]
  1. Hero(1) = Int((Rnd * 79) + 1)
  2. Hero(2) = Int((Rnd * 22) + 1)
  3.     If a = 1 Then Cls ' refresh screen  / ripulisce lo schermo
  4.     If a = 1 Then If (10 < Hero(1) Imp Hero(1) >= 70) Or (2 < Hero(2) Imp Hero(2) >= 19) Then Print "Out of game" Else Print " In game"
  5.     If a < 20 And a > 1 Then If (a = 2 Or a = 19) Then Print Space$(10) + String$(60, Chr$(178)) Else Print Space$(10) + Chr$(178) + Space$(58) + Chr$(178)
  6.     If a = 24 Then Locate 24, 30
  7.     If a = 24 Then Print " cursors to  move Hero";
  8.     If a = Hero(2) Then Locate Hero(2), Hero(1)
  9.     If a = Hero(2) Then Print ""
  10.     If InKey$ = Chr$(0) + "M" Then If Hero(1) < 80 Then Hero(1) = Hero(1) + 1
  11.     If InKey$ = Chr$(0) + "H" Then If Hero(2) > 1 Then Hero(2) = Hero(2) - 1
  12.     If InKey$ = Chr$(0) + "P" Then If Hero(2) < 23 Then Hero(2) = Hero(2) + 1
  13.     If InKey$ = Chr$(0) + "K" Then If Hero(1) > 1 Then Hero(1) = Hero(1) - 1
  14.     If a = 25 Then _Limit 5
  15.     If a = 25 Then a = 1 Else a = a + 1

Please try it. I think it would be smoother with  _Keyhit
Title: Re: Not Sure What has happened here in my use of IMP logical operator
Post by: bplus on March 27, 2021, 07:31:16 pm
@TempodiBasic

Very nice but needs one more line because hero refuses to budge specially with down keypress:
Code: QB64: [Select]
  1. Hero(1) = Int((Rnd * 79) + 1)
  2. Hero(2) = Int((Rnd * 22) + 1)
  3.     If a = 1 Then Cls ' refresh screen  / ripulisce lo schermo
  4.     If a = 1 Then If (10 < Hero(1) Imp Hero(1) >= 70) Or (2 < Hero(2) Imp Hero(2) >= 19) Then Print "Out of game" Else Print " In game"
  5.     If a < 20 And a > 1 Then If (a = 2 Or a = 19) Then Print Space$(10) + String$(60, Chr$(178)) Else Print Space$(10) + Chr$(178) + Space$(58) + Chr$(178)
  6.     If a = 24 Then Locate 24, 30
  7.     If a = 24 Then Print " cursors to  move Hero";
  8.     If a = Hero(2) Then Locate Hero(2), Hero(1)
  9.     If a = Hero(2) Then Print ""
  10.     k$ = InKey$
  11.     If k$ = Chr$(0) + "M" Then If Hero(1) < 80 Then Hero(1) = Hero(1) + 1
  12.     If k$ = Chr$(0) + "H" Then If Hero(2) > 1 Then Hero(2) = Hero(2) - 1
  13.     If k$ = Chr$(0) + "P" Then If Hero(2) < 23 Then Hero(2) = Hero(2) + 1
  14.     If k$ = Chr$(0) + "K" Then If Hero(1) > 1 Then Hero(1) = Hero(1) - 1
  15.     If a = 25 Then _Limit 25
  16.     If a = 25 Then a = 1 Else a = a + 1
  17.  
  18.  

Did Steve help you with this? ;-))


Also Hero inbound on left border but out bounds on the other borders
  [ This attachment cannot be displayed inline in 'Print Page' view ]  

I think you just need another = sign.

Also hero refuses to go below row 23 I think, wont run down along bottom of screen.

Still very clever!
Title: Re: Not Sure What has happened here in my use of IMP logical operator
Post by: bplus on March 27, 2021, 09:41:06 pm
@TempodiBasic

I have yours down to 16 with out the message to use cursor keys, still can't go past 23 but the in/out is consistent:
Code: QB64: [Select]
  1. Hero(1) = Int((Rnd * 79) + 1) ' I fixed up
  2. Hero(2) = Int((Rnd * 22) + 1)
  3.     If a = 1 Then Cls ' refresh screen  / ripulisce lo schermo
  4.     If a = 1 Then If (11 < Hero(1) Imp Hero(1) >= 70) Or (2 < Hero(2) Imp Hero(2) >= 19) Then Print "Out of game" Else Print " In game"
  5.     If a < 20 And a > 1 Then If (a = 2 Or a = 19) Then Print Space$(10) + String$(60, Chr$(178)) Else Print Space$(10) + Chr$(178) + Space$(58) + Chr$(178)
  6.     If a = Hero(2) Then Locate Hero(2), Hero(1)
  7.     If a = Hero(2) Then Print ""
  8.     k$ = InKey$
  9.     If k$ = Chr$(0) + "M" Then If Hero(1) < 80 Then Hero(1) = Hero(1) + 1
  10.     If k$ = Chr$(0) + "H" Then If Hero(2) > 1 Then Hero(2) = Hero(2) - 1
  11.     If k$ = Chr$(0) + "P" Then If Hero(2) < 23 Then Hero(2) = Hero(2) + 1
  12.     If k$ = Chr$(0) + "K" Then If Hero(1) > 1 Then Hero(1) = Hero(1) - 1
  13.     If a = 25 Then _Limit 5
  14.     If a = 25 Then a = 1 Else a = a + 1
  15.  
Title: Re: Not Sure What has happened here in my use of IMP logical operator
Post by: bplus on March 27, 2021, 09:54:21 pm
This gets full range of screen but a little noisey:
Code: QB64: [Select]
  1. hero = Int(Rnd * 25 * 80) + 1
  2. For a = 1 To 25
  3.     Locate a, 1
  4.     If a >= 2 And a <= 19 Then If a = 2 Or a = 19 Then Print Space$(9) + String$(62, 178) + Space$(9); Else Print Space$(9) + Chr$(178) + Space$(60) + Chr$(178) + Space$(9);
  5.     If a = 1 Or a > 19 Then Print Space$(80);
  6.     Locate 25, 33
  7.     If (hero \ 80 + 1) > 1 And (hero \ 80 + 1) < 20 And ((hero Mod 80) + 1 > 9) And ((hero Mod 80) + 1 < 72) Then Print "Hero is in/on bounds."; Else Print "  Hero is outside.";
  8.     Locate hero \ 80 + 1, (hero Mod 80) + 1
  9.     Print Chr$(1);
  10.     k$ = InKey$
  11.     If k$ = Chr$(0) + "M" Then hero = hero - (((hero Mod 80) + 1) <> 80)
  12.     If k$ = Chr$(0) + "K" Then hero = hero + (((hero Mod 80)) <> 0)
  13.     If k$ = Chr$(0) + "H" Then hero = hero + 80 * ((hero \ 80) > 0)
  14.     If k$ = Chr$(0) + "P" Then hero = hero - 80 * ((hero \ 80) < 24)
  15.     If a = 25 Then a = 0
  16.  
Title: Re: Not Sure What has happened here in my use of IMP logical operator
Post by: bplus on March 27, 2021, 11:40:37 pm
2 more lines removed:
Code: QB64: [Select]
  1. hero = Int(Rnd * 25 * 80) + 1
  2. For a = 1 To 25
  3.     Locate a, 1
  4.     If a >= 2 And a <= 19 Then If a = 2 Or a = 19 Then Print Space$(9) + String$(62, 178) + Space$(9); Else Print Space$(9) + Chr$(178) + Space$(60) + Chr$(178) + Space$(9);
  5.     If a = 1 Or a > 19 Then Print Space$(80);
  6.     Locate 25, 33
  7.     If (hero \ 80 + 1) > 1 And (hero \ 80 + 1) < 20 And ((hero Mod 80) + 1 > 9) And ((hero Mod 80) + 1 < 72) Then Print "Hero is in/on bounds."; Else Print "  Hero is outside.";
  8.     Locate hero \ 80 + 1, (hero Mod 80) + 1
  9.     Print Chr$(1);
  10.     k$ = InKey$
  11.     If (k$ = Chr$(0) + "M") Or (k$ = Chr$(0) + "K") Then If k$ = Chr$(0) + "M" Then hero = hero - (((hero Mod 80) + 1) <> 80) Else hero = hero + (((hero Mod 80)) <> 0)
  12.     If (k$ = Chr$(0) + "H") Or (k$ = Chr$(0) + "P") Then If (k$ = Chr$(0) + "H") Then hero = hero + 80 * ((hero \ 80) > 0) Else hero = hero - 80 * ((hero \ 80) < 24)
  13.     If a = 25 Then a = 0
  14.  
Title: Re: Not Sure What has happened here in my use of IMP logical operator
Post by: TempodiBasic on March 28, 2021, 12:12:08 pm
Hi Bplus
Thank you for take a look at my shrinked version of IMP demo.

Thank you for debug it.
Hero refuses to reach the bottom border of the screen to avoid meeting  help text on the 24 row.

Cool your improvements and the reducing of  number of lines.
And I see that you like my massive use of IF


Title: Re: Not Sure What has happened here in my use of IMP logical operator
Post by: bplus on March 28, 2021, 12:54:21 pm
Quote
Cool your improvements and the reducing of  number of lines.
And I see that you like my massive use of IF

Yes, you can make 2 statements effectively, at least 2 variable assignments, with "massive use of IF".