 |
Cheat Engine The Official Site of Cheat Engine
|
| View previous topic :: View next topic |
| Author |
Message |
IMRobbie Newbie cheater
Reputation: 0
Joined: 26 Sep 2009 Posts: 24
|
Posted: Wed Jan 27, 2010 6:14 pm Post subject: [C++] Simplify Code |
|
|
This is a function from my Hangman project. The function actually plays the game. The code works 100% but, as you can see, is a bit lengthly. 2/3 of the code is mostly graphics. Can the people of CEF help me simplify this code?
The function is being sent a string variable that is randomly selected from an array.
| Code: | void playGame(string setWord) {
transform(setWord.begin(), setWord.end(), setWord.begin(), toupper);
//declare booleans
bool gameOver = false;
bool dashReplaced = false;
//declare variables
string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string usedLetters = "__________________________";
string letter = "";
string guessWord = "";
string setWordCopy = "";
int numChars = static_cast<int>(setWord.length());
int numIncorrect = 0;
int alphabetPos = 0;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos = {0, 0};
SetConsoleCursorPosition(hConsole, pos);
drawScaffold();
for(int x = 1; x < (26 * 2); x += 2)
usedLetters.insert(x, " "); //inserts a blank space between each dash in usedLetters
pos.X = 12; pos.Y = 2;
SetConsoleCursorPosition(hConsole, pos);
cout << usedLetters; //outputs the dashes in the used letters box
guessWord.assign(numChars, '_'); //assigns a to however many characters in setWord (numChars)
for(int x = 1; x < (numChars * 2); x += 2)
guessWord.insert(x, " "); //inserts a blank space between each dash in guessWord
for(int x = 0; x < numChars; x++) {
if(setWord.substr(x, 1) == " ") //searches setWord for a blank space
//if setWord does contain a space, replace the dash assign to the space with a space
guessWord.replace((x * 2), 1, " ");
}
pos.X = 30; pos.Y = 15;
SetConsoleCursorPosition(hConsole, pos);
cout << "Guess this word: " << guessWord; //outputs the dashes
while(gameOver == false) {
pos.X = 30; pos.Y = 16;
SetConsoleCursorPosition(hConsole, pos);
cout << "Enter a letter: ";
pos.X = 46; pos.Y = 16;
SetConsoleCursorPosition(hConsole, pos);
cin >> letter;
transform(letter.begin(), letter.end(), letter.begin(), toupper);
//error routine that checks if the player inputed valid data for letter
while(letter < "A" || letter > "Z" || letter.length() >= 2) {
pos.X = 30; pos.Y = 17;
SetConsoleCursorPosition(hConsole,pos);
cout << "You entered an invalid character."; Sleep(1000);
pos.X = 30; pos.Y = 17;
SetConsoleCursorPosition(hConsole,pos);
cout << " ";
pos.X = 30; pos.Y = 16;
SetConsoleCursorPosition(hConsole, pos);
cout << "Enter a letter: ";
pos.X = 46; pos.Y = 16;
SetConsoleCursorPosition(hConsole, pos);
cin >> letter;
transform(letter.begin(), letter.end(), letter.begin(), toupper);
}//end while
//searches alphabet for the first occurence of letter and returns the index value
alphabetPos = alphabet.find(letter);
//replace the index value of alphabetPos with letter
usedLetters.replace((alphabetPos * 2), 1, letter);
pos.X = 12; pos.Y = 2;
SetConsoleCursorPosition(hConsole, pos);
cout << usedLetters;
for(int x = 0; x < numChars; x++) {
//searc hes setWord for letter
if(setWord.substr(x, 1) == letter) {
guessWord.replace((x * 2), 1, letter); //replace the appropriate dash with the letter
dashReplaced = true; //indicate that a replacement was made
}//end if
}//end for
//determine whether a replacement was made
if(dashReplaced == true) {
// searches guessWord for the first occurance of a dash, if none were found, find() returns a -1
if(static_cast<int>(guessWord.find("_", 0)) == -1) {
gameOver = true;
setWordCopy = setWord;
for(int x = 1; x < (numChars * 2); x = x + 2)
setWordCopy.insert(x, " "); //inserts a blank space between each letter in setWordCopy
pos.X = 30; pos.Y = 15;
SetConsoleCursorPosition(hConsole, pos);
cout << "Guess this word: " << setWordCopy; //shows final word
pos.X = 30; pos.Y = 17;
SetConsoleCursorPosition(hConsole, pos);
transform(setWord.begin(), setWord.end(), setWord.begin(), tolower);
cout << "Yes, the word is " << char(96) << setWord << char(39) << endl;
pos.X = 30; pos.Y = 18;
SetConsoleCursorPosition(hConsole, pos);
cout << "Awesome Guessing! =)";
//flashing colors
int numColor = 0;
for(int x = 0; x < 100; x++) {
numColor++;
if(numColor == 15)
numColor = 1;
pos.X = 51; pos.Y = 18;
SetConsoleCursorPosition(hConsole, pos);
SetConsoleTextAttribute(hConsole, numColor);
cout << "<3";
Sleep(50);
pos.X = 51; pos.Y = 18;
SetConsoleCursorPosition(hConsole, pos);
cout << " ";
}
} else {
//display the status of the guessed word
pos.X = 30; pos.Y = 15;
SetConsoleCursorPosition(hConsole, pos);
cout << "Guess this word: " << guessWord << endl;
dashReplaced = false;
}
} else {
numIncorrect++;
if(numIncorrect == 1) {
//Displays head
pos.X = 21; pos.Y = 17;
SetConsoleCursorPosition(hConsole, pos);
cout << "|";
pos.X = 21; pos.Y = 18;
SetConsoleCursorPosition(hConsole, pos);
cout << char(1);
} else if(numIncorrect == 2) {
//Displays body
pos.X = 21; pos.Y = 19;
SetConsoleCursorPosition(hConsole, pos);
cout << char(179);
pos.X = 21; pos.Y = 20;
SetConsoleCursorPosition(hConsole, pos);
cout << char(179);
} else if(numIncorrect == 3) {
//Displays first arm
pos.X = 20; pos.Y = 19;
SetConsoleCursorPosition(hConsole, pos);
cout << char(47);
} else if(numIncorrect == 4) {
//Displays second arm
pos.X = 22; pos.Y = 19;
SetConsoleCursorPosition(hConsole, pos);
cout << char(92);
} else if(numIncorrect == 5) {
//Displays first leg
pos.X = 20; pos.Y = 21;
SetConsoleCursorPosition(hConsole, pos);
cout << char(47);
} else if(numIncorrect == 6) {
//Displays second leg, and the game is over
gameOver = true;
pos.X = 22; pos.Y = 21;
SetConsoleCursorPosition(hConsole, pos);
cout << char(92); Sleep(1000);
transform(setWord.begin(), setWord.end(), setWord.begin(), tolower);
pos.X = 30; pos.Y = 17;
SetConsoleCursorPosition(hConsole, pos);
cout << "Sorry, the word is " << char(96) << setWord << char(39) << endl;
}//end if
}//end if
}//end while
}//end of function |
|
|
| Back to top |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Wed Jan 27, 2010 8:31 pm Post subject: |
|
|
I didn't read the whole thing but one thing that i did notice was that you could use a switch statement when evaluating the number incorrect
_________________
|
|
| Back to top |
|
 |
tombana Master Cheater
Reputation: 2
Joined: 14 Jun 2007 Posts: 456 Location: The Netherlands
|
Posted: Thu Jan 28, 2010 9:05 am Post subject: |
|
|
I didn't look at all of it, but I noticed:
| Code: | } else if(numIncorrect == 3) {
//Displays first arm
pos.X = 20; pos.Y = 19;
SetConsoleCursorPosition(hConsole, pos);
cout << char(47);
} else if(numIncorrect == 4) {
//Displays second arm
pos.X = 22; pos.Y = 19;
SetConsoleCursorPosition(hConsole, pos);
cout << char(92);
} else if(numIncorrect == 5) {
//Displays first leg
pos.X = 20; pos.Y = 21;
SetConsoleCursorPosition(hConsole, pos);
cout << char(47); |
Instead, you might be able to do something like:
| Code: |
struct{
int posX, posY;
char Character;
}MyArray;
//For all the numIncorrects:
MyArray[3].posX = 20;
MyArray[3].posY = 19;
MyArray[3].Character = 92;
MyArray[4].posX = 22;
MyArray[4].posY = 19;
MyArray[4].Character = 92;
//and so on
//And at the part with the if's you replace all those if's by:
pos.X = MyArray[numIncorrect].posX; pos.Y = MyArray[numIncorrect].posY;
SetConsoleCursorPosition(hConsole, pos);
cout << char( MyArray[numIncorrect].Character ); |
And also use a switch statement like HomerSexual said.
|
|
| Back to top |
|
 |
Womanizer Grandmaster Cheater
Reputation: 2
Joined: 30 May 2009 Posts: 958
|
|
| Back to top |
|
 |
Zurkei Grandmaster Cheater Supreme
Reputation: 0
Joined: 15 Nov 2007 Posts: 1132 Location: Makakilo, Hawaii
|
Posted: Thu Jan 28, 2010 10:26 am Post subject: |
|
|
Yours made it 230 lines of code instead of 189 lines of code, and he said simplify it not convert it to another language.
|
|
| Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Thu Jan 28, 2010 1:48 pm Post subject: |
|
|
| if you have learnt OOP, i think this could be coded better as that
|
|
| Back to top |
|
 |
IMRobbie Newbie cheater
Reputation: 0
Joined: 26 Sep 2009 Posts: 24
|
Posted: Fri Jan 29, 2010 1:24 pm Post subject: |
|
|
I didn't think of a switch statement alternate. =) Thanks.
I would love to look into the structures tombana mentioned. I'll check it out.
| Code: | numIncorrect++;
switch(numIncorrect) {
case 1:
//Displays head
pos.X = 21; pos.Y = 17;
SetConsoleCursorPosition(hConsole, pos);
cout << "|";
pos.X = 21; pos.Y = 18;
SetConsoleCursorPosition(hConsole, pos);
cout << char(1);
break;
case 2:
//Displays body
pos.X = 21; pos.Y = 19;
SetConsoleCursorPosition(hConsole, pos);
cout << char(179);
pos.X = 21; pos.Y = 20;
SetConsoleCursorPosition(hConsole, pos);
cout << char(179);
break;
case 3:
//Displays first arm
pos.X = 20; pos.Y = 19;
SetConsoleCursorPosition(hConsole, pos);
cout << char(47);
break;
case 4:
//Displays second arm
pos.X = 22; pos.Y = 19;
SetConsoleCursorPosition(hConsole, pos);
cout << char(92);
case 5:
//Displays first leg
pos.X = 20; pos.Y = 21;
SetConsoleCursorPosition(hConsole, pos);
cout << char(47);
break;
case 6:
//Displays second leg, and the game is over
gameOver = true;
pos.X = 22; pos.Y = 21;
SetConsoleCursorPosition(hConsole, pos);
cout << char(92); Sleep(1000);
transform(setWord.begin(), setWord.end(), setWord.begin(), tolower);
pos.X = 30; pos.Y = 17;
SetConsoleCursorPosition(hConsole, pos);
cout << "Sorry, the word is " << char(96) << setWord << char(39) << endl;
break;
}//end switch |
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Fri Jan 29, 2010 1:40 pm Post subject: |
|
|
| that's basically the most pointless switch i've ever seen
|
|
| Back to top |
|
 |
IMRobbie Newbie cheater
Reputation: 0
Joined: 26 Sep 2009 Posts: 24
|
Posted: Fri Jan 29, 2010 1:41 pm Post subject: |
|
|
| slovach wrote: | | that's basically the most pointless switch i've ever seen |
Curiously, what would you suggest me do?
|
|
| Back to top |
|
 |
DoomsDay Grandmaster Cheater
Reputation: 0
Joined: 06 Jan 2007 Posts: 768 Location: %HomePath%
|
Posted: Fri Jan 29, 2010 5:44 pm Post subject: |
|
|
Keep coding, you'll get better over time. I simplified your code a bit; you can get it much smaller by leaving cin\cout out. | Code: | string strToUpperCase(string str)
{
for (size_t i=0; i < str.length(); i++)
str[i] = toupper(str[i]);
return str;
}
void writeString(string str, short x, short y)
{
COORD pointer = {x,y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pointer);
cout << str;
}
void writeString(string str)
{
cout << str;
}
void writeChar(char c, short x, short y)
{
COORD pointer = {x,y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pointer);
cout << c;
}
void writeChar(char c)
{
cout << c;
}
void _playGame(string magicWord)
{
//=====================================
//Declarations
//booleans
bool gameOver = false;
bool dashReplaced = false;
//variables
string setWord;
string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string usedLetters = "__________________________";
string letter = "";
string guessWord = "";
int numChars = magicWord.length();
int numIncorrect = 0;
//=====================================
//Preproccessing
//-------------------------------------
setWord = strToUpperCase(magicWord); //magic word goes uppercase
for(int x = 1; x < (26 * 2); x += 2) //inserts a blank space between each dash in usedLetters
usedLetters.insert(x, " ");
guessWord.assign(numChars, '_'); //assigns a "_" to however many characters in setWord (numChars)
for(int x = 1; x < (numChars * 2); x += 2)
guessWord.insert(x, " "); //inserts a blank space between each dash in guessWord
for(int x = 0; x < numChars; x++)
if(setWord.substr(x, 1) == " ") //searches setWord for a blank space
guessWord.replace((x * 2), 1, " "); //if setWord does contain a space, replace the dash assign to the space with a space
//=====================================
//Game starts here
//-------------------------------------
// drawScaffold();
writeString(usedLetters,12,2); //outputs the dashes in the used letters box
writeString("Guess this word: ",30,15);
writeString(guessWord); //outputs the dashes
while(gameOver == false)
{
//-------------------------------------
//input loop
do{
writeString(" ",30,17);
writeString("Enter a letter: ",30,16);
writeString("",46,16);
cin >> letter;
letter = strToUpperCase(letter);
if (letter < "A" || letter > "Z" || letter.length() >= 2)
{
writeString("You entered an invalid character.",30,17);
Sleep(1000);
}
else
break;
}while (0);
//adjusts usedLetters according to the selection
usedLetters.replace((alphabet.find(letter) * 2), 1, letter);
writeString(usedLetters,12,2);
//adjusts guessWord according to the selection
for(int x = 0; x < numChars; x++)
{
if(setWord.substr(x, 1) == letter)
{
guessWord.replace((x * 2), 1, letter);
dashReplaced = true;
}
}
//determine whether a replacement was made
if(dashReplaced == true)
{
//updates progress
writeString("Guess this word: ",30,15);
writeString(guessWord);
// searches guessWord for the first occurance of a dash, if none were found, find() returns a -1
if((guessWord.find("_", 0)) == -1)
{
gameOver = true;
writeString("Yes, the word is: ",30,17);
writeString(setWord);
writeString("Awesome Guessing! =)",30,18);
//flashing colors
int numColor = 0;
for(int x = 0; x < 100; x++)
{
numColor++;
if(numColor == 15)
numColor = 1;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), numColor);
writeString("<3",51,18);
Sleep(50);
writeString(" ",51,18);
}
}
else
dashReplaced = false;
}
else
{
numIncorrect++;
switch (numIncorrect)
{
case 1:
writeChar(char(179),21,17);
writeChar(char(1),21,18); //Displays head
break;
case 2:
writeChar(char(179),21,19); //Displays body
writeChar(char(179),21,20);
break;
case 3:
writeChar('/',20,19); //Displays first arm
break;
case 4:
writeChar('\\',22,19); //Displays second arm
break;
case 5:
writeChar('/',20,21); //Displays first leg
break;
case 6:
gameOver = true;
writeChar('\\',22,21); //Displays first leg
Sleep(1000);
writeString("Sorry, the word is: ",30,17);
writeString(setWord);
break;
default:
break;
}
}
}
} |
|
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Sat Jan 30, 2010 4:20 pm Post subject: |
|
|
I'm not going to sort through that clusterfuck to simplify it, however I will give you the following class that you could use to do some simplification:
| Code: | class ConsolePosition {
COORD position_;
public:
ConsolePosition(unsigned int x, unsigned int y) {
position_.X = x;
position_.Y = y;
}
template <typename T>
ConsolePosition & operator=(T const & rhs) {
CONSOLE_SCREEN_BUFFER_INFO oldPosition;
HANDLE const output = ::GetStdHandle(STD_OUTPUT_HANDLE);
::GetConsoleScreenBufferInfo(output, &oldPosition);
::SetConsoleCursorPosition(output, position_);
cout << rhs;
::SetConsoleCursorPosition(output, oldPosition.dwCursorPosition);
return *this;
}
}; |
That way, if you were so inclined, you could do something like:
| Code: | | ConsolePosition(3, 4) = "Hello, world!"; |
Or you could just create a named object to use for later. This code also has the benefit of putting all the disgusting windows code in one manageable place.
EDIT: Also, switch statements are stupid. If it's small you should use an if statement, and if it's large you should use the strategy pattern or a functor of some kind. Maps are pretty cool too.
|
|
| Back to top |
|
 |
Anden100 Grandmaster Cheater
Reputation: 0
Joined: 20 Apr 2007 Posts: 668
|
Posted: Sat Jan 30, 2010 5:29 pm Post subject: |
|
|
| Flyte wrote: | | EDIT: Also, switch statements are stupid. If it's small you should use an if statement, and if it's large you should use the strategy pattern or a functor of some kind. Maps are pretty cool too. |
Due to curiousity, why is switch statements stupid?, i use them myself all the time -.-
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
|