[C/C++][Cheat] Facebook A-Z game

Questions about programming languages and debugging
Post Reply
User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

[C/C++][Cheat] Facebook A-Z game

Post by ayu »

A friend of mine created a facebook account for me during a lecture today. Never really been in to stuff like that, but I gave it a shot, and to be honest, it's rather fun to use it.

Anyway, so another friend (also Software Engineer), told me to try and break his record at this game, where you have to write A-Z 5 times as fast as possible. I tried it 2 times, then I got bored.

...But I still wanted to break his damn record (he was pretty damn good!)

So I thought, "come on? We are Software Engineers in training! We should be able to fight on a higher level!"

So I wrote this ...

Code: Select all

#include <windows.h>
#include <iostream>

using namespace std;

int main()
{
	char	charArr[]	= {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
	byte	hexArr[]	= {0x9e, 0xb0, 0xae, 0xa0, 0x92, 0xa1, 0xa2, 0xa3, 0x97, 0xa4, 0xa5, 0xa6, 0xb2, 0xb1, 0x98, 0x99, 0x90, 0x93, 0x9f, 0x94, 0x96, 0xaf, 0x91, 0xad, 0x95, 0xac};

	cout << "Starting in 5 seconds\n";
	Sleep(5000);

	for(int i = 0; i < 5; ++i)
	{
		//A-Z
		for(int j = 0; j < 26; ++j)
		{
			//Sleep(100);
			cout << "Round [" << i+1 << "] - Character [" << charArr[j] << "]\n";
			keybd_event(VkKeyScan(charArr[j]),hexArr[j],0 , 0);
			keybd_event(VkKeyScan(charArr[j]),hexArr[j], KEYEVENTF_KEYUP,0);
		}
	}

	system("PAUSE");
	return 0;
}
It's nothing much, but it was pretty damn funny .... I am still awaiting his answer ^^

EDIT: As a side note. Removing the cout in the nested loop, makes the process a lot faster (obviously). A friend pointed it out when the result from the application on facebook only came out as 0.05 seconds, while when the cout was removed, it came out as 0.00 seconds :lol:
"The best place to hide a tree, is in a forest"

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Post by ayu »

Now, to explain the code a bit (haven't had time to comment it)

The purpose of the game is quite simple.
When you are ready you press the start button, and when you press the first letter (logically that would be 'A') the clock will start.
You type the whole alphabet 5 times as fast as you can, simply.

The purpose of this program is to cheat in that game and automate the process of writing the alphabet 5 times.


First we need two libraries (we don't really NEED the iostream library, but I like to tell the user what is happening)

Code: Select all

#include <windows.h>
#include <iostream>


Then we tell the compiler what namespace we are using, so that we don't have to write std:: in front of every member of that namespace (in this case std)

Code: Select all

using namespace std;


Now, in the main function you will first need a char array for the list of characters in the alphabet (used for virtual keys that the system uses), and a byte array for the hardware scan codes.

Quick explanation about scan codes
There are two types of scan-codes: the hardware codes and the virtual codes. The hardware codes are sent by the keyboard to the keyboard port, which are translated by windows to the virtual key codes
If you want to read more ...

Code: Select all

int main()
{
   char   charArr[]   = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};

   byte   hexArr[]   = {0x9e, 0xb0, 0xae, 0xa0, 0x92, 0xa1, 0xa2, 0xa3, 0x97, 0xa4, 0xa5, 0xa6, 0xb2, 0xb1, 0x98, 0x99, 0x90, 0x93, 0x9f, 0x94, 0x96, 0xaf, 0x91, 0xad, 0x95, 0xac};


Now we tell the user that the process will start in 5 seconds, we then call the function "Sleep()" in the windows library, and give it 5000 (ms) as an argument.

Code: Select all


   cout << "Starting in 5 seconds\n";
   Sleep(5000);



Now we want a nested loop, where the outer loop goes on for 5 turns (to write the alphabet 5 times), and one loop for writing all the letters (26 letters in the English alphabet)

Code: Select all

   for(int i = 0; i < 5; ++i)
   {
      //A-Z
      for(int j = 0; j < 26; ++j)
      {


Now, as you can see I commented out the line "Sleep(100)", this is because when I was testing the application I wanted it to stop for 100 ms so that it wasn't too fast when writing the letters. I commented it later to make it as fast as possible.

Code: Select all

         //Sleep(100);


This next part I added because it was informative for the user.
It tells you what round it's currently on, and what letter.
I later commented it out to get the result 0.00 on the game (since the cout added a delay of 0.05 seconds).

Code: Select all

         cout << "Round [" << i+1 << "] - Character [" << charArr[j] << "]\n";


This is the key part (pun?) of the program.
Here I call the windows-function keybd_event().
In the first argument, I call the function VkKeyScan() and give it a character as an argument. This function will then return the virtual key scan code. The next argument is a hardware scan code from the byte array. The two last arguments are flags and special extra values that can be associated with the key event, which is nothing that we are interested in right now. By default this means we are pressing a key.

Code: Select all


         keybd_event(VkKeyScan(charArr[j]),hexArr[j],0 , 0);


Next we call the same function, almost the same, but we add a flag to the third argument this time, "KEYEVENTF_KEYUP", which means that we release the key.

Code: Select all

         keybd_event(VkKeyScan(charArr[j]),hexArr[j], KEYEVENTF_KEYUP,0);
      }
   }


There, as a last small task, we make a system call and pause the application at the end, and return 0 to the system and exits the application.

Code: Select all


   system("PAUSE");
   return 0;
} 
"The best place to hide a tree, is in a forest"

User avatar
IceDane
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 197
Joined: 12 Aug 2009, 16:00
14

Post by IceDane »


Post Reply