Advertisement
Guest User

messy code central

a guest
Aug 15th, 2013
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <string>
  5. #include <sstream>
  6. #include <math.h>
  7. #include <ctime>
  8. #include <Windows.h>
  9.  
  10. using namespace std;
  11.  
  12. class CPlayer
  13. {
  14.     string name;
  15.     int lives;
  16.     vector<string> guessed;
  17.     int wins;
  18.     string word;
  19.     string fakeword;
  20. public:
  21.  
  22.     int getguess(){return guessed.size();}
  23.     string getguess(int i){return guessed.at(i);}
  24.     string tellword(){return word;}
  25.     string tellfword(){return fakeword;}
  26.     int telllives(){return lives;}
  27.     int tellwins(){return wins;}
  28.     string tellname(){return name;}
  29.  
  30.     void setname(string a = "Player"){name = a;}   
  31.     bool incwin(bool a);
  32.     void declives(int a = 1){lives -= a;}
  33.     void setword(string a);
  34.     void setlives(unsigned int a = 16){lives = a;}
  35.     int guess(string a, bool b);
  36.  
  37.     void reset(){lives = 16; word.clear(); fakeword.clear(); guessed.clear();}
  38. }player[2];
  39.  
  40. void CPlayer::setword(string a)
  41. {
  42.     word = a;
  43.     for(unsigned int i = 0; i < a.length(); i++)
  44.     {
  45.         fakeword += '_';
  46.     }
  47. }
  48.  
  49. int CPlayer::guess(string a, bool b)
  50. {
  51.     if((a.at(0) < 65  || a.at(0) > 122) || (a.at(0) > 90 && a.at(0) < 97))
  52.     {
  53.         cout << "You can only guess letters: ";
  54.         return 3;
  55.     }
  56.     if(a.length() == 1)
  57.     {
  58.         guessed.push_back(a);
  59.         bool flag = false;
  60.         for(unsigned int i = 0; i < (player[b].word.length()); i++)
  61.         {
  62.             if(a[0] == player[b].word.at(i) || a[0] == player[b].word.at(i) + 32 || a[0] == player[b].word.at(i) - 32)
  63.             {
  64.                 player[b].fakeword.at(i) = player[b].word.at(i);
  65.                 flag = true;
  66.                 if(player[b].fakeword == player[b].word)
  67.                 {
  68.                     return (incwin(!b) == true ? 2 : 0);
  69.                 }
  70.             }else if(i == player[b].word.length() - 1 && flag == false)
  71.             {
  72.                 declives();
  73.             }
  74.         }
  75.     }else
  76.     {
  77.         for(int i = 0; i < player[b].word.length(); i++)
  78.         {
  79.             if(player[b].word.length() != a.length() || player[b].word.at(i) != a.at(i) && player[b].word.at(i) != a.at(i) + 32 && player[b].word.at(i) != a.at(i) -32)
  80.             {
  81.                 declives(2);
  82.                 return 1;
  83.             }
  84.         }
  85.         return (incwin(!b) == true ? 2 : 0);
  86.     }
  87.     if(player[!b].lives == 0)
  88.     {
  89.         incwin(b);
  90.         return 0;
  91.     }
  92.     return 1;
  93. }
  94.  
  95. bool CPlayer::incwin(bool a)
  96. {
  97.     player[a].wins ++;
  98.     cout << player[a].name << " wins!" << endl;
  99.     cout << player[1].name << "'s word was " << player[1].word << "." << endl;
  100.     cout << "Current score: " << player[a].name << " " << player[a].wins << " : " << player[!a].name << " " << player[!a].wins << endl;
  101.     cout << "Play again? (y/n): ";
  102.     while(true)
  103.     {
  104.         string ans;
  105.         getline(cin, ans);
  106.         if(ans.length() == 1)
  107.         {
  108.             if(ans.at(0) == 89 || ans.at(0) == 121)
  109.             {
  110.                 return 1;
  111.             }else if (ans.at(0) == 78 || ans.at(0) == 110)
  112.             {
  113.                 return 0;
  114.             }else
  115.             {
  116.                 cout << "y/n : ";
  117.             }
  118.         }else
  119.         {
  120.             cout << "y/n : ";
  121.         }
  122.         ans.clear();
  123.     }
  124. }
  125.  
  126.  
  127.  
  128. void print(bool a);
  129. void lines(bool a, int b = 0, bool c = true, int d = 1);
  130. void boxes (bool a, string b, int c);
  131.  
  132. //Lots of comments to make this garbage readable.
  133. //No I don't care that it's garbage, move along.
  134. int main(int argc, char* argv[])
  135. {
  136.     string scroller;
  137.     cout << "Hangman WIP © Fear 2013";
  138.     getline(cin, scroller);
  139.     cout << "Feel free to submit word lists so I can add them in.";
  140.     getline(cin, scroller);
  141.     cout << "This version only has the Animal library.";
  142.     getline(cin, scroller);
  143.     cout << endl << endl << endl;
  144.     //Get username
  145.     cout << "Username: ";
  146.     string a;
  147.     getline(cin, a);
  148.     player[0].setname(a);
  149.     player[1].setname("Hangbot");
  150.  
  151.     while(true) // LOOP A
  152.     {
  153.         player[0].reset();
  154.         player[1].reset();
  155.         player[1].setlives(99999999);
  156.         cout << "Easy(1), Medium(2), Hard(3): ";
  157.         string qdiffi;
  158.         int idiffi;
  159.         stringstream ss;
  160.         while(true)
  161.         {
  162.             getline(cin, qdiffi);
  163.             if(qdiffi.length() == 1)
  164.             {
  165.                 ss << qdiffi;
  166.                 if(ss >> idiffi)
  167.                 {
  168.                     player[0].setlives(16-pow(2.0,idiffi));
  169.                     break;
  170.                 }
  171.             }
  172.         }
  173.         //Get word list #
  174.         fstream dict;
  175.         string dir = argv[0];
  176.         for(int i = 0; i < dir.length(); i++)
  177.         {
  178.             if(dir.at(i) == '\\')
  179.             {
  180.                 dir.insert(i, "\\");
  181.                 i++;
  182.             }
  183.         }
  184.         int j = dir.find("\\Hangman.exe");
  185.         dir.replace(j, dir.back(), "DICTIONARY0.txt");
  186.         dict.open(dir);
  187.         vector<string> wordlist;
  188.         if(dict.good())
  189.         {
  190.             //Load contents
  191.             string temp;
  192.             while(getline(dict, temp))
  193.             {
  194.                 wordlist.push_back(temp);
  195.             }
  196.         }else
  197.         {
  198.             cout << "Error locating dictionary file, exiting." << endl;
  199.             cin.get();
  200.             return 0;
  201.         }
  202.  
  203.         //Get bot's word
  204.         srand(time(NULL));
  205.         int randword = rand() % wordlist.size();
  206.         player[1].setword(wordlist.at(randword));
  207.  
  208.         //Get player's word
  209.         cout << "Your word (letters only, no spaces): ";
  210.         string a;
  211.         bool flaga = false;
  212.         while(!flaga)
  213.         {
  214.             getline(cin, a);
  215.             if(a.size() >= 2)
  216.             {
  217.                 for(unsigned int i = 0; i < a.length(); i++)
  218.                 {
  219.                     if(a.at(i) < 65 || a.at(i) > 122)
  220.                     {
  221.                         cout << "Letters only!: ";
  222.                         break;
  223.                     }else if(i == a.length() - 1)
  224.                     {
  225.                         //nested loops are the worst
  226.                         flaga = !flaga;
  227.                         break;
  228.                     }
  229.                 }
  230.             }else
  231.             {
  232.                 cout << "Your word must be 2 or more characters long!: ";
  233.             }
  234.         }
  235.         player[0].setword(a);
  236.  
  237.         bool bturn = false;
  238.         bool flagb = true;
  239.         while(flagb) // LOOP B
  240.         {
  241.             print(bturn);
  242.             while(flagb)
  243.             {
  244.                 cout << "Guess a letter: ";
  245.                 string pguess;
  246.                 if(!bturn)
  247.                 {
  248.                     getline(cin, pguess);
  249.                 }else
  250.                 {
  251.                     bool flagc = false;
  252.                     while(!flagc)
  253.                     {
  254.                         pguess = rand() % 26 + 97;
  255.                         if(player[bturn].getguess() >= 1)
  256.                         {
  257.                             for(int i = 0; i < player[bturn].getguess(); i++)
  258.                             {
  259.                                 if(pguess == player[bturn].getguess(i))
  260.                                 {
  261.                                     break;
  262.                                 }else if(i == player[bturn].getguess() - 1)
  263.                                 {
  264.                                     flagc = true;
  265.                                     break;
  266.                                 }
  267.                             }
  268.                         }else
  269.                         {
  270.                             flagc = true;
  271.                         }
  272.                     }
  273.                     Sleep(500);
  274.                     cout << pguess << endl;
  275.                     Sleep(500);
  276.                 }
  277.                 int a = player[bturn].guess(pguess, !bturn);
  278.                 if(a == 0)
  279.                 {
  280.                     return 0;
  281.                 }else if(a == 1)
  282.                 {
  283.                     break;
  284.                 }else if(a == 2)
  285.                 {
  286.                     flagb = false;
  287.                     break;
  288.                 }
  289.             }
  290.             bturn = !bturn;
  291.         }
  292.     }
  293. }
  294.  
  295. void print(bool a)
  296. {
  297.     cout << endl;
  298.     //x = player word or name, depending on which is longer:
  299.     int x = ((player[a].tellname().length() > player[a].tellword().length()) ? player[a].tellname().length() : player[a].tellword().length());
  300.     string name = player[a].tellname();
  301.     string word = player[a].tellword();
  302.     lines(a, 0, true, x); //see lines function
  303.     boxes(a, name, x);
  304.     if(!a) 
  305.     {
  306.         stringstream ss;
  307.         ss << player[a].telllives();
  308.         string templives = ss.str();
  309.         boxes(a, "Lives: " + templives, x);
  310.         boxes(a, "Word: " + word, x); //see boxes function
  311.     }
  312.     boxes(a, "Revealed: " + player[a].tellfword(), x);
  313.     boxes(a, "Used:", x);
  314.  
  315.     vector<string> temp;
  316.     for(int i = 0; i < player[a].getguess(); i++) temp.push_back(player[a].getguess(i)); //download
  317.     string ins; //empty string
  318.     for(int i = 0; i < temp.size() && temp.size() > 0; i++) //while there is still content..
  319.     {
  320.         ins = ins + temp.at(i); //add each letter to the string one by one...
  321.         ins.append(" "); //add a space...
  322.        
  323.         if(ins.size() == 16 + ((name.length() % 2 == 0) ? name.length() : name.length() + 1))
  324.         {
  325.             boxes(a, ins, x); //print it...
  326.             ins.clear(); //then clear it...
  327.         }
  328.     }
  329.     if(ins.size() > 0) //if there is any remaining letters..
  330.     {
  331.         boxes(a, ins, x); //then print them!
  332.     }
  333. }
  334.  
  335. //a = player, b = subtraction from length calculation, c = optional newline, d = x (from earlier)
  336. void lines(bool a, int b, bool c, int d)
  337. {
  338.     for(unsigned int i = 0; i < ((( (d % 2 == 0) ? 24 : 23) + d - 1) - b); i++)
  339.     {
  340.         cout << "-";
  341.     }
  342.     if(c)
  343.     {
  344.         cout << endl;
  345.     }
  346. }
  347.  
  348. //a = player, b = non-modifiable content length, c = x (from earlier)
  349. void boxes(bool a, string b, int c)
  350. {
  351.     cout << "|";
  352.     for(unsigned int i = 0; i < (( (c % 2 == 0) ? 22 : 21) + c - 1) - b.length(); i++)
  353.     {
  354.         if(i == (22 + c - b.length())/2)
  355.         {
  356.             cout << b;
  357.         }
  358.         cout << " ";
  359.     }
  360.     cout << "|" << endl;
  361.     cout << "|";
  362.     lines(a, 2, false, c);
  363.     cout << "|" << endl;
  364. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement