Tuesday, April 23, 2013

Binary Operations for Combining Multiple Selections

My latest realization: using XOR on a binary number can be used for combining menu selections.  Explanation below!

1:       int *coords;  
2:       int linecount=0;  
3:       short int missionTemp=1, missionChoice=0;  
4:       while(missionTemp!=0)  
5:       {  
6:            cout << "Enter mission desired, or 0 to continue: " << endl;  
7:            cout << "1. Fondor Escort (Lvl 10ish)" << endl;  
8:            cout << "2. Javaal Fleet Action (Lvl 10ish)" << endl;  
9:            cout << "3. Balosar Outpost (Lvl 10ish)" << endl;  
10:            cout << "4. Makem Te Assault (Lvl 20)" << endl;  
11:            cout << "5. Archenar Interception (Lvl 20)" << endl;  
12:            cout << "6. Syvris Evacuation (Lvl 28)" << endl;  
13:            cout << "7. Llanic Station Strike (Lvl 28)" << endl;  
14:            cout << "8. Paakuni Defense (Lvl 34)" << endl;  
15:            cin >> missionTemp;  
16:            if(missionTemp==1)  
17:                 missionChoice=missionChoice^1;  
18:            else if (missionTemp==2)  
19:                 missionChoice=missionChoice^2;  
20:            else if (missionTemp==3)  
21:                 missionChoice=missionChoice^4;  
22:            else if (missionTemp==4)  
23:                 missionChoice=missionChoice^8;  
24:            else if (missionTemp==5)  
25:                 missionChoice=missionChoice^16;  
26:            else if (missionTemp==6)  
27:                 missionChoice=missionChoice^32;  
28:            else if (missionTemp==7)  
29:                 missionChoice=missionChoice^64;  
30:            else if (missionTemp==8)  
31:                 missionChoice=missionChoice^128;  
32:            else if (missionTemp==0)  
33:                 break;  
34:            else  
35:                 break;  
36:       }  
37:       cout << "MissionChoice=" << missionChoice << endl;  
38:       cout << "Please switch to SWTOR and position properly." << endl;  
39:       Sleep(2000);  
40:       while(1)  
41:       {  
42:            if((missionChoice&1)==1)  
43:            {  
44:                 coords=readFile(fondor.inFile, linecount);  
45:                 while(1)  
46:                 {  
47:                      doMission(fondor.submapX, fondor.submapY, fondor.missionX, fondor.missionY, fondor.greenValue, linecount, coords);  
48:                 }  
49:            }  
50:            if((missionChoice&2)!=0)  
51:            {  
52:                 coords=readFile(javaal.inFile, linecount);  
53:                 while(1)  
54:                 {  
55:                      doMission(javaal.submapX, javaal.submapY, javaal.missionX, javaal.missionY, javaal.greenValue, linecount, coords);  
56:                 }  
57:            }  
58:            if((missionChoice&4)!=0)  
59:            {  
60:                 coords=readFile(balosar.inFile, linecount);  
61:                 while(1)  
62:                 {  
63:                      doMission(balosar.submapX, balosar.submapY, balosar.missionX, balosar.missionY, balosar.greenValue, linecount, coords);  
64:                 }  
65:            }  
66:            if((missionChoice&8)!=0)  
67:            {  
68:                 coords=readFile(makem.inFile, linecount);  
69:                 while(1)  
70:                 {  
71:                      doMission(makem.submapX, makem.submapY, makem.missionX, makem.missionY, makem.greenValue, linecount, coords);  
72:                 }  
73:            }  
74:            if((missionChoice&16)!=0)  
75:            {  
76:                 coords=readFile(archenar.inFile, linecount);  
77:                 while(1)  
78:                 {  
79:                      doMission(archenar.submapX, archenar.submapY, archenar.missionX, archenar.missionY, archenar.greenValue, linecount, coords);  
80:                 }  
81:            }  
82:            if((missionChoice&32)!=0)  
83:            {  
84:                 coords=readFile(syvris.inFile, linecount);  
85:                 while(1)  
86:                 {  
87:                      doMission(syvris.submapX, syvris.submapY, syvris.missionX, syvris.missionY, syvris.greenValue, linecount, coords);  
88:                 }  
89:            }  
90:            if((missionChoice&64)!=0)  
91:            {  
92:                 coords=readFile(llanic.inFile, linecount);  
93:                 while(1)  
94:                 {  
95:                      doMission(llanic.submapX, llanic.submapY, llanic.missionX, llanic.missionY, llanic.greenValue, linecount, coords);  
96:                 }  
97:            }  
98:            if((missionChoice&128)!=0)  
99:            {  
100:                 coords=readFile(pakuuni.inFile, linecount);  
101:                 while(1)  
102:                 {  
103:                      doMission(pakuuni.submapX, pakuuni.submapY, pakuuni.missionX, pakuuni.missionY, pakuuni.greenValue, linecount, coords);  
104:                 }  
105:            }  
106:       }  
107:  }  

I wanted my bot to run a customized selection of missions repeatedly, instead of having to compile a separate executable for every mission, or only allowing one choice.

Using binary, it's easier to see the operation in action.

If the user selects 1, 2, and 3, these are the binary operations that take place:

 missionChoice=00000000 (initial value)  
           XOR 00000001 (selecting 1 XORs with 1)
 missionChoice=00000001 (end result)  

missionChoice=00000001 (initial value)  
          XOR 00000010 (selecting 2 XORs with 2)
missionChoice=00000011 (end result) 

missionChoice=00000011 (initial value) 
         XOR  00000100 (selecting 3 XORs with 4)
missionChoice=00000111 (end result)  

After using the XOR process to combine the selections into a single variable, an AND operation is used with the same numbers to check if that value was previously selected:

 
missionChoice=00000111 (initial value)  
          AND 00000001 (was "1" selected?)  
              00000001 (anything other than 0 means it was selected)
  
missionChoice=00000111 (initial value)  
          AND 00000010 (was "2" selected)  
              00000010 (once again, non-0 value means it was selected)
  
missionChoice=00000111 (initial value)  
          AND 00000100 (was "3" selected)  
              00000100 (etc etc)  

Cool stuff!

Sunday, April 21, 2013

Programming - Passing Dynamic Memory to Functions by Reference

Needed to get the concept down for a much larger program.  Here is an example of how I used a dynamic array, (size dependent on user input... DANGER), passed it to a function, and had the function print the values back.

1:  void somefunc(int *nums, int howmuch)  
2:  {  
3:       for(int i=0;i<howmuch;i++)  
4:       {  
5:            cout << "Number 1: " << nums[i] << endl;  
6:       }  
7:  }  
8:    
9:    
10:  int main()  
11:  {  
12:       int *numbers;  
13:       int howmany;  
14:       cout << "How many: ";  
15:       cin >> howmany;  
16:       numbers = new int[howmany];  
17:       for(int i=0;i<howmany;i++)  
18:       {  
19:            cout << "Enter Number: ";  
20:            cin >> numbers[i];  
21:       }  
22:       somefunc(numbers, howmany);  
23:       Sleep(10000);  
24:  }  
25:    

The thing that caught me up was remembering not to PASS a *value, because that'd be a pointer to a pointer.  In the function declaration, you state that it's RECEIVING a pointer, so that's where you put the *.  Tricksy little functions.

Saturday, April 20, 2013

Programming Woes

Before today, I considered myself a decent C++ programmer.  I've written file parsers, encoders/decoders, automated bots for games, etc...

My latest endeavor was taking my polished executable and adding a GUI to it.  I've never been so confused!  I figured on an hour max for basically: taking my console program, adding a GUI with a  drop-down box with a few selections, and a button to "go".

Nope.  It's like a completely different language.

Screw this, I'm watching Star Trek.