Author Topic: C++ Logic need to understand for QB64  (Read 1788 times)

0 Members and 1 Guest are viewing this topic.

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
C++ Logic need to understand for QB64
« on: February 08, 2019, 02:26:32 pm »
I run a WoW server (usually classic or WotLK). In the source and DB they have an amazing formula for determining various things a player has.  So for this example, I'll use "Race" - I think this has something to do with OR, or XOR logic

NOTE: these are not correct values, I do not have the source open at the time of this writing

0 All races
1 Human
2 elves
4 Orcs
8 Draenea
16 Trolls
32 blood elves

etc...

So to determine a selected race(s)
0 = all races of course
but lets say we want just the humans and draenea - then the value would be 10 (2 + 8)

My question, is how would your reverse that in a program?

How can we deduce that 36 = blood elves and orcs

I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
Re: C++ Logic need to understand for QB64
« Reply #1 on: February 08, 2019, 03:11:12 pm »
My question, is how would your reverse that in a program?

How can we deduce that 36 = blood elves and orcs

Simple answer, you CAN'T  -  Would be the same as 6 + 7 = 13, if you only have 13 and even if you know also its a sum, you can't say for sure its made out of 6 and 7, it could also be 10 and 3, 8 and 5 or whatever. You need to know at least one of the original values to determine the other, doesn't matter if it's simple arithmetic or logic operators.

However, as all your single values are powers of two, you could check the respective bits if set or not,

eg: v& = 36 (bin=100100)

IF (v& AND 32) <> 0 THEN blood elves
IF (v& AND 4) <> 0 THEN orcs

guess that's what you want...
« Last Edit: February 08, 2019, 03:13:23 pm by RhoSigma »
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
Re: C++ Logic need to understand for QB64
« Reply #2 on: February 08, 2019, 03:38:21 pm »
How about two trolls and an orc? 16+ 16 +4

Set rules and use conditional statements. Maybe that's just old school.

Pete

- Two trolls and an orc walk into a bar...
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: C++ Logic need to understand for QB64
« Reply #3 on: February 08, 2019, 04:00:25 pm »
I run a WoW server (usually classic or WotLK). In the source and DB they have an amazing formula for determining various things a player has.  So for this example, I'll use "Race" - I think this has something to do with OR, or XOR logic

NOTE: these are not correct values, I do not have the source open at the time of this writing

0 All races
1 Human
2 elves
4 Orcs
8 Draenea
16 Trolls
32 blood elves

etc...

So to determine a selected race(s)
0 = all races of course
but lets say we want just the humans and draenea - then the value would be 10 (2 + 8)

My question, is how would your reverse that in a program?

How can we deduce that 36 = blood elves and orcs

Code: QB64: [Select]
  1. 'if you can put the data in a string with a predictable format, then you can do math on it
  2.  
  3. dat$ = "0 All races, 1 Human, 2 elves, 4 Orcs, 8 Draenea, 16 Trolls, 32 blood elves"
  4.  
  5. REDIM races$(0)
  6. Split dat$, ", ", races$()
  7. DIM vals(UBOUND(races))
  8. 'what is the total of all the races?
  9. FOR i = 0 TO UBOUND(races$)
  10.     vals(i) = VAL(leftOf$(races$(i), " "))
  11.     total = total + vals(i)
  12. PRINT "Total Races should be "; total
  13.  
  14. GOSUB selectRace
  15. a = vals(race)
  16. ar$ = rightOf$(races$(race), " ")
  17. getop:
  18. INPUT "Enter + , -, * , / ", op$
  19. IF INSTR("+-*/", op$) < 1 THEN PRINT "Nope not an operation, try again.": GOTO getop
  20. GOSUB selectRace
  21. b = vals(race)
  22. br$ = rightOf$(races$(race), " ")
  23.     CASE "+": PRINT ar$; " + "; br$; " = "; a + b
  24.     CASE "-": PRINT ar$; " - "; br$; " = "; a - b
  25.     CASE "*": PRINT ar$; " * "; br$; " = "; a * b
  26.     CASE "/": PRINT ar$; " / "; br$; " = "; a / b
  27.  
  28. selectRace:
  29. PRINT "Enter Number of Race to add, subtract, mult, divide:"
  30. FOR i = 1 TO UBOUND(races$)
  31.     PRINT i; " "; rightOf$(races$(i), " ")
  32. INPUT ""; race
  33. IF race < 1 OR race > UBOUND(races$) THEN PRINT "Try again": GOTO selectRace
  34.  
  35. 'notes: REDIM the array(0) to be loaded before calling Split '<<<<<<<<<<<<<<<<<<<<<<< IMPORTANT!!!!
  36. SUB Split (mystr AS STRING, delim AS STRING, arr() AS STRING)
  37.     ' bplus modifications of Galleon fix of Bulrush Split reply #13
  38.     ' http://www.[abandoned, outdated and now likely malicious qb64 dot net website - don’t go there]/forum/index.php?topic=1612.0
  39.     ' this sub further developed and tested here: \test\Strings\Split test.bas
  40.     DIM copy AS STRING, p AS LONG, curpos AS LONG, arrpos AS LONG, lc AS LONG, dpos AS LONG
  41.     copy = mystr 'make copy since we are messing with mystr
  42.     'special case if delim is space, probably want to remove all excess space
  43.     IF delim = " " THEN
  44.         copy = RTRIM$(LTRIM$(copy))
  45.         p = INSTR(copy, "  ")
  46.         WHILE p > 0
  47.             copy = MID$(copy, 1, p - 1) + MID$(copy, p + 1)
  48.             p = INSTR(copy, "  ")
  49.         WEND
  50.     END IF
  51.     curpos = 1
  52.     arrpos = 0
  53.     lc = LEN(copy)
  54.     dpos = INSTR(curpos, copy, delim)
  55.     DO UNTIL dpos = 0
  56.         arr(arrpos) = MID$(copy, curpos, dpos - curpos)
  57.         arrpos = arrpos + 1
  58.         REDIM _PRESERVE arr(arrpos + 1) AS STRING
  59.         curpos = dpos + LEN(delim)
  60.         dpos = INSTR(curpos, copy, delim)
  61.     LOOP
  62.     arr(arrpos) = MID$(copy, curpos)
  63.     REDIM _PRESERVE arr(arrpos) AS STRING
  64.  
  65. FUNCTION leftOf$ (source$, of$)
  66.     posOf = INSTR(source$, of$)
  67.     IF posOf > 0 THEN leftOf$ = MID$(source$, 1, posOf - 1)
  68.  
  69. FUNCTION rightOf$ (source$, of$)
  70.     posOf = INSTR(source$, of$)
  71.     IF posOf > 0 THEN rightOf$ = MID$(source$, posOf + LEN(of$))
  72.  
  73.  
« Last Edit: February 08, 2019, 04:01:38 pm by bplus »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • Steve’s QB64 Archive Forum
Re: C++ Logic need to understand for QB64
« Reply #4 on: February 08, 2019, 04:21:12 pm »
It seems as if you’re looking for:

Race OR Value (to set the value)
Race AND NOT Value (to clear the value)

Human = 1
Elf = 2
Orc = 4

IF Human THEN Race = Race OR 1
IF Elf THEN Race = Race OR 2
IF Orc THEN Race = Race OR 4

To remove the value, use AND NOT.

IF Remove_Human THEN Race = Race AND NOT 1
IF Remove_Elf THEN Race = Race AND NOT 2
IF Remove_Orc THEN Race = Race AND NOT 4



One more point:

Zero should be NO races, not ALL races. 

All races would be 63 (&B111111 for the 6 flags all being set).



AND is used for comparisons:

IF Race AND 1 THEN ‘is human, do human stuff
IF Race AND 2 THEN ‘is elf, do elf stuff
IF Race AND 4 THEN ‘is orc, do orc stuff
« Last Edit: February 08, 2019, 04:28:14 pm by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • Steve’s QB64 Archive Forum
Re: C++ Logic need to understand for QB64
« Reply #5 on: February 08, 2019, 04:56:59 pm »
Give this little demo a quick run:

Code: QB64: [Select]
  1.     CLS
  2.     PRINT "Select Which Races can use the Sword Of Ultimate Doom:"
  3.     PRINT "0) None (monster only)"
  4.     PRINT "1) Human"
  5.     PRINT "2) Elf"
  6.     PRINT "3) Orc"
  7.     PRINT "4) Draenea"
  8.     PRINT "5) Troll"
  9.     PRINT "6) Blood Elf"
  10.     PRINT
  11.     PRINT "Currently usable by (Race = "; Race; ", which represents:"
  12.     'COMPARE ROUTINE
  13.     IF Race = 0 THEN PRINT "Monsters Only, No Player Races.";
  14.     IF Race AND 1 THEN PRINT "Human, ";
  15.     IF Race AND 2 THEN PRINT "Elf, ";
  16.     IF Race AND 4 THEN PRINT "Orc, ";
  17.     IF Race AND 8 THEN PRINT "Draenea, ";
  18.     IF Race AND 16 THEN PRINT "Troll, ";
  19.     IF Race AND 32 THEN PRINT "Blood Elf, ";
  20.     PRINT
  21.     PRINT
  22.     PRINT
  23.     PRINT "Enter Number from 0 to 6 to add a race, SHIFT + number from 1 to 6 to erase a race."
  24.     PRINT "Blank Entry to Quit"
  25.     a$ = INPUT$(1)
  26.     'ADD VALUE ROUTINE
  27.     SELECT CASE a$
  28.         CASE "0": Race = 0
  29.         CASE "1": Race = Race OR 1
  30.         CASE "2": Race = Race OR 2
  31.         CASE "3": Race = Race OR 4
  32.         CASE "4": Race = Race OR 8
  33.         CASE "5": Race = Race OR 16
  34.         CASE "6": Race = Race OR 32
  35.             'SUBTRACT VALUE ROUTINE
  36.         CASE ")": Race = 63
  37.         CASE "!": Race = Race AND NOT 1
  38.         CASE "@": Race = Race AND NOT 2
  39.         CASE "#": Race = Race AND NOT 4
  40.         CASE "$": Race = Race AND NOT 8
  41.         CASE "%": Race = Race AND NOT 16
  42.         CASE "^": Race = Race AND NOT 32
  43.         CASE "", " ": END
  44.     END SELECT
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Dimster

  • Forum Resident
  • Posts: 500
Re: C++ Logic need to understand for QB64
« Reply #6 on: February 08, 2019, 04:58:34 pm »
Humans and Draenea add up to 9. In fact, as Humans are the only odd number, any odd number you would be search for would have to include Humans. Seems a backward search might work best. Take the target number 36, find the nearest less than 36 that equals a race, then search half that value until you come up with a value matching that equals your target. ie Target Num = 36 , closest match less than 36 = 32. Hold as a match for one race. 1/2 of 32 = 16 but 16 + 32 not equal Target Num of 36, then 1/2 of 16, then 1/2 of 8 until a race is reached that meets the Target Num. I'm not sure how many races you have but I think your way of numbering them holds a lot of promise for a backward searching formula. If the Target Num was an odd number, they one of the races has to be human.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: C++ Logic need to understand for QB64
« Reply #7 on: February 08, 2019, 05:10:25 pm »
Quote
0 All races
1 Human
2 elves
4 Orcs
8 Draenea
16 Trolls
32 blood elves
Is this table a population listing of races or just names for powers of 2?

What if:
2 Human
3 elves
5 Orcs
7 Draenea
11 Trolls
13 blood elves

Ha! Humans and Draenea still = 9!
« Last Edit: February 08, 2019, 05:14:14 pm by bplus »

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
Re: C++ Logic need to understand for QB64
« Reply #8 on: February 08, 2019, 05:43:26 pm »
Hi
Just a curiosity..
why do you say
Quote
C++ Logic need to understand
?

it seems only binary logic... that you find every time you work with binary numbers showed as power of 2 in Basic, in QBasic, in QB64, in TurboBasic, in VisualBasic, in FreeBasic, in TurboPascal and bla bla bla bla in all languages that are built with binary number management and Logic operator....(AND OR XOR NOT...)
see here http://qb64.org/wiki/Keyword_Reference_-_By_usage#Logical_Bitwise_Operations:
 following math of tables of true/false there are some other operators... see here for more
info https://en.wikipedia.org/wiki/Truth_table
Programming isn't difficult, only it's  consuming time and coffee

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • Steve’s QB64 Archive Forum
Re: C++ Logic need to understand for QB64
« Reply #9 on: February 08, 2019, 06:36:03 pm »
Quote
0 All races
1 Human
2 elves
4 Orcs
8 Draenea
16 Trolls
32 blood elves
Is this table a population listing of races or just names for powers of 2?

What if:
2 Human
3 elves
5 Orcs
7 Draenea
11 Trolls
13 blood elves

Ha! Humans and Draenea still = 9!

It’s a Binary Listing of Races available in World of Warcraft.  Take a look at my simple demo here: https://www.qb64.org/forum/index.php?topic=1053.msg102389#msg102389
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: C++ Logic need to understand for QB64
« Reply #10 on: February 08, 2019, 06:53:19 pm »
Quote
It’s a Binary Listing of Races available in World of Warcraft.  Take a look at my simple demo here: https://www.qb64.org/forum/index.php?topic=1053.msg102389#msg102389

I did look the at the demo which gave rise to my question.

Just naming powers of 2 then :P

Code: QB64: [Select]
  1. _TITLE "Race naming" 'B+ 2019-02-08
  2.  
  3. DIM tnames$(7)
  4. tname$(0) = "Human"
  5. tname$(1) = "Elves"
  6. tname$(2) = "Orcs"
  7. tname$(3) = "Draenea"
  8. tname$(4) = "Trolls"
  9. tname$(5) = "Blood Elves"
  10.  
  11.     INPUT "Enter a number and I will say what races it includes "; raceNum
  12.     IF raceNum <= 63 AND raceNum >= 0 THEN
  13.         FOR power = 0 TO 7
  14.             IF raceNum AND 2 ^ power THEN PRINT tname$(power); " ";
  15.         NEXT
  16.         PRINT
  17.     END IF
  18. LOOP UNTIL raceNum < 0 OR raceNum > 63
  19.  
  20.  
« Last Edit: February 08, 2019, 07:42:40 pm by bplus »

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
Re: C++ Logic need to understand for QB64
« Reply #11 on: February 08, 2019, 09:58:14 pm »
I see I need to push my _SETBIT, _READBIT, _RESETBIT, _TOGGLEBIT, and _ROTATEBIT routines  to the repo.
Granted after becoming radioactive I only have a half-life!

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
Re: C++ Logic need to understand for QB64
« Reply #12 on: February 09, 2019, 01:31:08 am »
Can we get a few more constraints on the problem? I can imagine that it makes no sense to select the same race twice for instance, but then again, I can imagine you *can* for certain questions.

Some headway you can make without thinking whatsoever: if the total is odd, it definitely involves humans, because there is only one odd number in the list. EDIT: I see dimster noticed this above. Nice.

Also, if I add all numbers 1+2+4+...+32, does that imply we'd never deal with a total higher than that?

The guys are above are attempting to answer *some* question, but for reputation purposes, my two cents will only drop when we know a little more of what's being asked. Seems like something that can be understood purely by theory instead of hobby engineering.



« Last Edit: February 09, 2019, 02:02:36 am by STxAxTIC »
You're not done when it works, you're done when it's right.