   Obfuscate This
  This is a game that I like to play every once in a while. I'll take a simple piece of code and make it the longest, most convoluted piece of obfuscation ever. Last time, we did "Hello, World" with single entries. This time, I want to mix it up a bit. Today we're going to do a simple number guessing game, and we are going to incrementally obfuscate it. So, one person might replace all occurances of the number 1 with "cos(0)", and the next person might replace all instances of "for(int i....)" with "for(char c...)" or "for(float f...)". Whatever, you'll figure it out.Some limitations: no #define's. They're cheapno interjections of obscure APIs, it should all be standard library codeso obviously that means no graphics, all text modeno "implementing an operating system" as your obfuscation, keep things within reason.100% secure code is not important, we will assume the user knows the proper input format.okay, here we go:#include <iostream>#include <cstdlib>int main(int argc, char** argv){int num, guess;bool done = false, found;char c;while(!done){num = std::rand()%100;std::cout<<"I'm thinking of a number from 0 to 100: ";found = false;while(!found){std::cin>>guess;if(guess == num){std::cout<<"Correct!"<<std::endl;found = true;}else if(guess < num){std::cout<<"Too low."<<std::endl;}else{std::cout<<"Too high."<<std::endl;}}std::cout<<"Again? (Y|N): ";std::cin>>c;done = (c == 'n' || c == 'N');}return 0;}
#include <iostream>#include <cstdlib>int main(int argc, char** argv){int num, guess; bool done = false, found;char c;while(!done){num = std::rand()%100;std::cout<<"I'm thinking of a number from 0 to 100: ";found = false;while(!found){std::cin>>guess;std::cout<<(guess==num?  (found=true,"Correct!"):  (guess < num? "Too low." : "Too high."))  <<std::endl;}std::cout<<"Again? (Y|N): ";std::cin>>c;done = (c == 'n' || c == 'N');}return 0;};#include <iostream>#include <cstdlib>int main(int argc, char** argv){int num, guess; bool done = false, found;char c;   std::string message;   message = "I'm thinking of a number from 0 to 100: ";while(!done){num = std::rand()%101;std::cout << message << std::endl;found = false;while(!found){std::cin>>guess;std::cout<<(guess==num?  (found=true,"Correct!"):  (guess < num? "Too low." : "Too high."))  <<std::endl;}std::cout<<"Again? (Y|N): ";std::cin>>c;done = (c == 'n' || c == 'N');}return 0;}Wouldn't num = std::rand()%100; be a number between 0 and 99? Not 0 - 100.   ;#include <iostream>#include <cstdlib>#include <algorithm>void PrintChar(char c){std::cout << c;}int main(int argc, char** argv){int num, guess; bool done = false, found;char c;   const std::string message;   message = "I'm thinking of a number from 0 to 100 : ";while(!done){num = std::rand()%101; std::for_each(message.begin(), message.end(), PrintChar);std::cout << std::endl;found = false;while(!found){std::cin>>guess;std::cout<<(guess==num?  (found=true,"Correct!"):  (guess < num? "Too low." : "Too high."))  <<std::endl;}std::cout<<"Again? (Y|N): ";std::cin>>c;done = (c == 'n' || c == 'N');}return 0;}for future reference, does boost count as standard?   ;  Here ya go:#include <iostream>#include <cstdlib>#include <string>int main(int argc, char** argv){int num, guess; bool found;char c;   std::string message;   std::string correct("Correct!");   std::string too("Too ");   std::string low("low.");   std::string high("high.");   std::string again("Again? (Y|N): ");   message = "I'm thinking of a number from 0 to 100: ";while(1){num = std::rand()%101;std::cout << message << std::endl;found = false;while(!found){std::cin>>guess;std::cout<<(guess==num?  (found=true,correct):  (guess < num? too+low : too+high))  <<std::endl;}std::cout<<again;std::cin>>c; if (std::toupper(c) == 'N')break;}return 0;}I couldn't find an easy way to turn the while (!found) into a while (1).   ;  What this code needs is a few design patterns. I'm too lazy, but I'm sure someone with a good knowledge of OOP can produce some of the most expertly obfuscated code imaginable. [wink]   ;  Short, didn't have much time.  Should have tried harder with input and outputs.  I call it "Shall we play again ?"I tried to leave all your bugs in but I might have fixed a few accidentally. [wink]#include <iostream>#include <cstdlib>int main(int, char**) {int Y=42,N=Y*Y;char c;while((((c=Y*N)-'N') != '\0') && (c-'N' != ' ')) {Y = Y*(std::rand()%100) / (N/Y);std::cout<<"I'm thinking of a number from 0 to 100: ";while((N-Y)!=0){std::cin >> N;((Y-N)==0) ? std::cout<< "Correct!" : ((N-Y)>=1) ? std::cout <<"Too high\n" : std::cout <<"Too low\n";}std::cout<<"Again? (Y|N): ";N=Y*(Y=42);std::cin>>c;}}Edit: Removed some whitespace to get rid of the scrollbar.[Edited by - jfclavette on June 5, 2006 11:02:50 PM];Quote:Original post by WashuBah, incrementally obfuscate it, tis zee boring! Ah well, deleted original reply :PMutter.[Edited by - Nathan Baum on June 5, 2006 11:26:26 PM];#include <iostream>int main(int, char**) {int y=42,n=y*y;char c=y*n;   int MAX;   char DIF;   cout<<"What difficulty do you want? [HARD = H][MEDIUM = M][EASY = E]\n";   cin>>DIF;   switch(DIF){  case 'H': MAX = 151; break;  case 'M': MAX = 101; break;  default: MAX = 51; break;   }while(((c-'N') != '\0') && (c-'N' != ' ')) {y *= y*(std::rand()%MAX)/n;std::cout<<"I'm thinking of a number from 0 to " << MAX << ": ";while((n-y)!=0){std::cin >> n;((y-n)==0) ? std::cout<< "Correct!" : ((n-y)>=1) ? std::cout <<"Too high" : std::cout <<"Too low";}std::cout<<"Again? (Y|N): ";std::cin>>c;}return 0;}Added the abillity to choose your difficulty. Is upgrading allowed, or do you just want the same code sloppified?   ;  Bah, incrementally obfuscate it, tis zee boring! Ah well, deleted original reply :P[Edited by - Washu on June 5, 2006 11:36:55 PM]