Bukz

n00b hangman v2

Aug 2nd, 2011
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 25.56 KB | None | 0 0
  1. // Hangman by Bukz
  2.  
  3. #include <iostream>
  4. #include <fstream>
  5. #include <string>
  6. #include <stdlib.h>
  7. #include <time.h>
  8. #include <vector>
  9.  
  10. using namespace std;
  11.  
  12. // Sound stuff
  13. #ifndef __WIN32__
  14.  
  15. #define _WIN32_WINNT  0x0500
  16. #include <tchar.h>
  17. #include <windows.h>
  18. #include <Wincon.h>
  19. #pragma comment(lib, "winmm.lib")
  20.  
  21. // Console Colors
  22. #define FG_RED (FOREGROUND_RED | FOREGROUND_INTENSITY)
  23. #define FG_GREEN (FOREGROUND_GREEN | FOREGROUND_INTENSITY)
  24. #define FG_BLUE (FOREGROUND_BLUE | FOREGROUND_INTENSITY)
  25. #define FG_YELLOW (FG_RED | FG_GREEN)
  26. #define FG_WHITE (FG_RED | FG_GREEN | FG_BLUE)
  27. #define FG_GRAY (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
  28. #define BG_RED (BACKGROUND_RED | BACKGROUND_INTENSITY)
  29. #define BG_GREEN (BACKGROUND_GREEN | BACKGROUND_INTENSITY)
  30. #define BG_BLUE (BACKGROUND_BLUE | BACKGROUND_INTENSITY)
  31. #define BG_YELLOW (BG_RED | BG_GREEN)
  32. #define BG_WHITE (BG_RED | BG_GREEN | BG_BLUE)
  33. #define BG_GRAY (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE)
  34.  
  35. #define bg_blue_fg_white (FG_WHITE | BG_BLUE)
  36. #define bg_white_fg_black (BG_WHITE)
  37. #define bg_white_fg_red (BG_WHITE | FG_RED)
  38. #define bg_white_fg_green (BG_WHITE | FG_GREEN)
  39. #define bg_white_fg_yellow (BG_WHITE | FG_YELLOW)
  40. #define bg_white_fg_blue (BG_WHITE | FG_BLUE)
  41. #define bg_white_fg_gray (BG_WHITE | FG_GRAY)
  42. #define bg_red_fg_white (BG_RED | FG_WHITE)
  43. #define bg_green_fg_white (BG_GREEN | FG_WHITE)
  44. #define bg_yellow_fg_white (BG_YELLOW | FG_WHITE)
  45. #define bg_gray_fg_white (BG_GRAY | FG_WHITE)
  46. #define bg_gray_fg_blue (BG_GRAY | FG_BLUE)
  47. #define bg_gray_fg_yellow (BG_GRAY | FG_YELLOW)
  48.  
  49. #define red() (SetConsoleTextAttribute(screen, bg_white_fg_red))
  50. #define redbg() (SetConsoleTextAttribute(screen, bg_red_fg_white))
  51. #define green() (SetConsoleTextAttribute(screen, bg_white_fg_green))
  52. #define greenbg() (SetConsoleTextAttribute(screen, bg_green_fg_white))
  53. #define blue() (SetConsoleTextAttribute(screen, bg_white_fg_blue))
  54. #define bluebg() (SetConsoleTextAttribute(screen, bg_blue_fg_white))
  55. #define white() (SetConsoleTextAttribute(screen, FG_WHITE))
  56. #define whitebg() (SetConsoleTextAttribute(screen, bg_white_fg_black))
  57. #define yellow() (SetConsoleTextAttribute(screen, bg_white_fg_yellow))
  58. #define yellowbg() (SetConsoleTextAttribute(screen, bg_yellow_fg_white))
  59. #define gray() (SetConsoleTextAttribute(screen, bg_white_fg_gray))
  60. #define graybg() (SetConsoleTextAttribute(screen, bg_gray_fg_white))
  61.  
  62. #endif
  63.  
  64. // Constants
  65. const int MAX_INCORRECT_GUESSES = 7;
  66.  
  67. // Globals
  68. bool debug = false;
  69. bool draw = true;
  70.  
  71. #ifndef __WIN32__
  72. bool sounds = true;
  73. #else
  74. bool sounds = false;
  75. #endif
  76. int incorrect;
  77. string word, maskedWord;
  78. vector <char> tried;
  79. vector <string> dictionary;
  80. vector <string> howto;
  81.  
  82. // Protos
  83. void drawHangMan(int pos);
  84. void readWordsFile();
  85. void readHowToFile();
  86. void getInputLoop();
  87. void checkDigitInput(int input);
  88. void toggleDebug();
  89. void reseed(int msg = 0);
  90. #ifndef __WIN32__
  91. void toggleSound();
  92. #endif
  93. void viewDictionary();
  94. void about();
  95. void splashScreen();
  96. void doSpecialCommand(char input);
  97. bool isAlphaStr(string word);
  98. bool isUniqueStr(string word);
  99. int searchDictionary(string word);
  100. string genAlreadyGuessedStr();
  101.  
  102. // Function definitions
  103. int rnd(int upperLimit) { return rand() % (upperLimit - 1) + 1; }
  104.  
  105. void clearInput()
  106. {
  107.     fflush(stdin);
  108.     cin.clear();
  109. }
  110.  
  111. void coutspace(string output) { cout << endl << output << endl << endl; }
  112.  
  113. #ifndef __WIN32__
  114. void toggleSound()
  115. {
  116.     HANDLE screen;
  117.     screen = GetStdHandle(STD_OUTPUT_HANDLE);
  118.    
  119.     if(sounds) { PlaySound(NULL, 0, 0); sounds = false; red(); coutspace("Sounds disabled."); whitebg(); }
  120.     else
  121.     {
  122.         sounds = true;
  123.         green();
  124.         coutspace("Sounds enabled.");
  125.         whitebg();
  126.         if(sounds) PlaySound(TEXT("sounds\\switch.wav"), NULL, SND_FILENAME | SND_ASYNC);
  127.     }
  128. }
  129. #endif
  130.  
  131. void readWordsFile()
  132. {
  133.     #ifndef __WIN32__
  134.     HANDLE screen;
  135.     screen = GetStdHandle(STD_OUTPUT_HANDLE);
  136.     #endif
  137.     string line;
  138.     ifstream file ("data\\words.txt");
  139.  
  140.     if(file.is_open())
  141.     {
  142.         while(file.good())
  143.         {
  144.             getline(file, line);
  145.             dictionary.push_back(line);
  146.         }
  147.         file.close();
  148.         #ifndef __WIN32__
  149.         graybg();
  150.         #endif
  151.         cout << "Successfully read file: /data/words.txt" << endl;
  152.         #ifndef __WIN32__
  153.         whitebg();
  154.         #endif
  155.     }
  156.     else
  157.     {
  158.         #ifndef __WIN32__
  159.         red();
  160.         #endif
  161.         cout << "Error opening file: \"/data/words.txt\" for reading." << endl;
  162.         #ifndef __WIN32__
  163.         whitebg();
  164.         Sleep(5000);
  165.         #endif
  166.         exit(0);
  167.     }
  168. }
  169.  
  170. void readHowToFile()
  171. {
  172.     #ifndef __WIN32__
  173.     HANDLE screen;
  174.     screen = GetStdHandle(STD_OUTPUT_HANDLE);
  175.     #endif
  176.     string line;
  177.     ifstream file ("data\\howto.txt");
  178.  
  179.     if(file.is_open())
  180.     {
  181.         while(file.good())
  182.         {
  183.             getline(file, line);
  184.             howto.push_back(line);
  185.         }
  186.         file.close();
  187.         #ifndef __WIN32__
  188.         graybg();
  189.         #endif
  190.         cout << "Successfully read file: /data/howto.txt" << endl << endl;
  191.         #ifndef __WIN32__
  192.         whitebg();
  193.         #endif
  194.     }
  195.     else
  196.     {
  197.         #ifndef __WIN32__
  198.         red();
  199.         #endif
  200.         cout << "Error opening file: \"/data/howto.txt\" for reading." << endl;
  201.         #ifndef __WIN32__
  202.         whitebg();
  203.         #endif
  204.     }
  205. }
  206.  
  207. void howTo()
  208. {
  209.     #ifndef __WIN32__
  210.     HANDLE screen;
  211.     screen = GetStdHandle(STD_OUTPUT_HANDLE);
  212.     #endif
  213.     if(howto.size())
  214.     {
  215.         #ifndef __WIN32__
  216.         graybg();
  217.         #endif
  218.         cout << endl;
  219.         for(unsigned int i = 0; i < howto.size(); i++)
  220.             cout << "\t" << howto.at(i) << endl;
  221.         cout << endl;
  222.         #ifndef __WIN32__
  223.         whitebg();
  224.         #endif
  225.     }
  226.     else
  227.     {
  228.         #ifndef __WIN32__
  229.         red();
  230.         #endif
  231.         coutspace("Nothing in the how to for display.");
  232.         #ifndef __WIN32__
  233.         whitebg();
  234.         #endif
  235.     }
  236. }
  237.  
  238. void maskWord()
  239. {
  240.     string tmp;
  241.  
  242.     for(unsigned int i = 0; i < word.length(); i++)
  243.         tmp += '-';
  244.  
  245.     maskedWord = tmp;
  246. }
  247.  
  248. string zapCase(string tmpword, int upper = 0)
  249. {
  250.     string tmp;
  251.  
  252.     for(unsigned int i = 0; i < tmpword.length(); i++)
  253.     {
  254.         if(upper) tmp += toupper(tmpword.at(i));
  255.         else tmp += tolower(tmpword.at(i));
  256.     }
  257.     return tmp;
  258. }
  259.  
  260. int slen(string s) { return s.length(); }
  261.  
  262. void newgame()
  263. {
  264.     #ifndef __WIN32__
  265.     HANDLE screen;
  266.     screen = GetStdHandle(STD_OUTPUT_HANDLE);
  267.     #endif
  268.    
  269.     splashScreen();
  270.     #ifndef __WIN32__
  271.     green();
  272.     #endif
  273.     cout << endl << "Starting Hangman...";
  274.     #ifndef __WIN32__
  275.     blue();
  276.     #endif
  277.     coutspace("Enter 0 (zero) for help...");
  278.     #ifndef __WIN32__
  279.     whitebg();
  280.     #endif
  281.     tried.clear();
  282.     if(debug) cout << "There are " << dictionary.size() << " words to choose from." << endl;
  283.     int wordPos = rnd(dictionary.size());
  284.     word = zapCase(dictionary.at(wordPos), 1);
  285.     if(debug) cout << "Word number " << wordPos + 1 << " has been randomly chosen." << endl;
  286.     maskWord();
  287.     incorrect = 0;
  288.  
  289.     getInputLoop();
  290. }
  291.  
  292. void endGame(int won)
  293. {
  294.     #ifndef __WIN32__
  295.     HANDLE screen;
  296.     screen = GetStdHandle(STD_OUTPUT_HANDLE);
  297.     #endif
  298.     char a = ' ';
  299.  
  300.     if(won)
  301.     {
  302.         #ifndef __WIN32__
  303.         if(sounds) PlaySound(TEXT("sounds\\win.wav"), NULL, SND_FILENAME | SND_ASYNC);
  304.         #endif
  305.         cout<< endl;
  306.         #ifndef __WIN32__
  307.         greenbg();
  308.         #endif
  309.         cout << "You won! The word is " << word << ".";
  310.         #ifndef __WIN32__
  311.         whitebg();
  312.         #endif
  313.         cout << endl;
  314.     }
  315.     else
  316.     {
  317.         #ifndef __WIN32__
  318.         if(sounds) PlaySound(TEXT("sounds\\lose.wav"), NULL, SND_FILENAME | SND_ASYNC);
  319.         #endif
  320.         cout << endl;
  321.         #ifndef __WIN32__
  322.         redbg();
  323.         #endif
  324.         cout << "You lost! The word was " << word << ".";
  325.         #ifndef __WIN32__
  326.         whitebg();
  327.         #endif
  328.         cout << endl;
  329.     }
  330.  
  331.  
  332.     while((tolower(a) != 'y') && (tolower(a) != 'n'))
  333.     {
  334.         clearInput();
  335.         #ifndef __WIN32__
  336.         blue();
  337.         #endif
  338.         cout << "Play again? y/n? ";
  339.         #ifndef __WIN32__
  340.         whitebg();
  341.         #endif
  342.         cin.get(a);
  343.     }
  344.  
  345.     if(tolower(a) == 'y') newgame();
  346.     else exit(0);
  347. }
  348.  
  349. bool hasChar(char letter)
  350. {
  351.     char tmp = toupper(letter);
  352.     for(unsigned int i = 0; i < word.length(); i++)
  353.         if(word.at(i) == tmp) { return true; break; }
  354.     return false;
  355. }
  356.  
  357. int countChars(char letter)
  358. {
  359.     int times = 0;
  360.     char tmp = toupper(letter);
  361.  
  362.     for(unsigned int i = 0; i < word.length(); i++)
  363.         if(word.at(i) == tmp) times += 1;
  364.  
  365.     return times;
  366. }
  367.  
  368. int countMaskedChars(string word)
  369. {
  370.     int ctr = 0;
  371.     for(unsigned int i = 0; i < word.length(); i++)
  372.         if(word.at(i) == '-') ctr += 1;
  373.  
  374.     return ctr;
  375. }
  376.  
  377. void updateWord(char letter)
  378. {
  379.     string tmp, tmpchar;
  380.     string oldmask = maskedWord;
  381.  
  382.     for(unsigned int i = 0; i < word.length(); i++)
  383.     {
  384.         if(word.at(i) == letter) tmp += word.at(i);
  385.         else tmp += maskedWord.at(i);
  386.     }
  387.  
  388.     maskedWord = tmp;
  389.     if(debug)
  390.         if(oldmask != maskedWord)
  391.             cout << "Old mask " << oldmask << " has been updated to " << maskedWord << endl;
  392. }
  393.  
  394. bool alreadyGuessed(char letter)
  395. {
  396.     for(unsigned int i = 0; i < tried.size(); i++)
  397.         if(tried.at(i) == letter) { return true; break; }
  398.     return false;
  399. }
  400.  
  401. string genAlreadyGuessedStr()
  402. {
  403.     string tmp;
  404.     for(unsigned int i = 0; i < tried.size(); i++)
  405.     {
  406.         tmp += tried.at(i);
  407.         tmp += ' ';
  408.     }
  409.  
  410.     return tmp;
  411. }
  412.  
  413. void guess(char letter)
  414. {
  415.     #ifndef __WIN32__
  416.     HANDLE screen;
  417.     screen = GetStdHandle(STD_OUTPUT_HANDLE);
  418.     #endif
  419.     char input = toupper(letter);
  420.  
  421.     if(!hasChar(input) || alreadyGuessed(input))
  422.     {
  423.         cout << endl;
  424.         #ifndef __WIN32__
  425.         redbg();
  426.         #endif
  427.         cout << "INCORRECT!";
  428.         #ifndef __WIN32__
  429.         whitebg();
  430.         #endif
  431.         cout << endl;
  432.         #ifndef __WIN32__
  433.         if(sounds) PlaySound(TEXT("sounds\\incorrect.wav"), NULL, SND_FILENAME | SND_ASYNC);
  434.         #endif
  435.         if(alreadyGuessed(input)) cout << endl << "You have already guessed: " << endl <<
  436.             genAlreadyGuessedStr() << endl;
  437.         else tried.push_back(input);
  438.  
  439.         incorrect++;
  440.         if(draw) drawHangMan(incorrect);
  441.  
  442.         if(incorrect >= MAX_INCORRECT_GUESSES)
  443.         {
  444.             endGame(0);
  445.             return;
  446.         }
  447.  
  448.         #ifndef __WIN32__
  449.         SetConsoleTextAttribute(screen, bg_gray_fg_yellow);
  450.         #endif
  451.         cout << endl << "You have " << MAX_INCORRECT_GUESSES - incorrect << " incorrect guesses remaining." << endl;
  452.         #ifndef __WIN32__
  453.         whitebg();
  454.         #endif
  455.     }
  456.  
  457.     if(hasChar(input))
  458.     {
  459.         if(!alreadyGuessed(input))
  460.         {
  461.             cout << endl;
  462.             #ifndef __WIN32__
  463.             greenbg();
  464.             #endif
  465.             cout << "CORRECT!";
  466.             #ifndef __WIN32__
  467.             whitebg();
  468.             #endif
  469.             cout << endl;
  470.             #ifndef __WIN32__
  471.             if(sounds) PlaySound(TEXT("sounds\\correct.wav"), NULL, SND_FILENAME | SND_ASYNC);
  472.             #endif
  473.             tried.push_back(input);
  474.         }
  475.         updateWord(input);
  476.     }
  477.  
  478.     if(word == maskedWord)
  479.     {
  480.         endGame(1);
  481.         return;
  482.     }
  483.  
  484.     getInputLoop();
  485. }
  486.  
  487. void getInputLoop()
  488. {
  489.     char input = '0';
  490.  
  491.     cout << endl << "Hint: " << maskedWord << " (" << word.length() << " characters, " <<
  492.         word.length() - countMaskedChars(maskedWord) << " unmasked)" << endl << endl;
  493.  
  494.     while (!isalpha(input))
  495.     {
  496.         clearInput();
  497.         cout << "Enter a letter (A-Z): ";
  498.         cin.get(input);
  499.  
  500.         if(isdigit(input)) checkDigitInput(input);
  501.         doSpecialCommand(input);
  502.     }
  503.  
  504.     guess(input);
  505. }
  506.  
  507. void checkDigitInput(int input)
  508. {
  509.     char a = '0';
  510.     string tmp;
  511.  
  512.     switch(input)
  513.     {
  514.         case '1': newgame(); return; break;
  515.         case '2': toggleDebug(); break;
  516.         case '3':
  517.             cout << endl << "Current Game Information" << endl << "------------------------" << endl;
  518.  
  519.             cout << endl << "\tHint" << endl << "\t----" << endl << "\t" << maskedWord <<
  520.                 " (" << word.length() << " characters, " << word.length() - countMaskedChars(maskedWord) << " unmasked)" << endl << "\t----"<< endl;
  521.  
  522.             cout << endl << "\tIncorrect" << endl << "\t---------" << endl <<
  523.                 "\tYou have " << MAX_INCORRECT_GUESSES - incorrect << " incorrect guesses remaining." <<
  524.                 endl << "\t---------" << endl;
  525.  
  526.             cout << endl << "\tAlready Guessed" << endl << "\t---------------" << endl;
  527.  
  528.             if(slen(genAlreadyGuessedStr()))
  529.                 cout << "\t" << genAlreadyGuessedStr();
  530.             else cout << "\tNONE";
  531.  
  532.             coutspace("\t---------------");
  533.  
  534.             break;
  535.         case '4':
  536.             #ifndef __WIN32__
  537.             if(debug && sounds) PlaySound(TEXT("sounds\\dictionary.wav"), NULL, SND_FILENAME | SND_ASYNC);
  538.             #endif
  539.             viewDictionary();
  540.             break;
  541.         case '5':
  542.             if(debug)
  543.             {
  544.                 while(!isalpha(a))
  545.                 {
  546.                     clearInput();
  547.                     cout << "Enter a letter to check the current word for: ";
  548.                     cin.get(a);
  549.                 }
  550.                 if(hasChar(a)) cout << endl << "The current word has " <<
  551.                     countChars(a) << " " << a << "'s in it." << endl << endl;
  552.                 else cout << endl << "Could not find " << a << " in the current word." << endl << endl;
  553.             }
  554.             else coutspace("Debug mode must be on to do a letter search.");
  555.  
  556.             break;
  557.         case '6':
  558.             if(debug)
  559.             {
  560.                 while(slen(tmp) < 2 || !isAlphaStr(tmp) || !isUniqueStr(tmp))
  561.                 {
  562.                     clearInput();
  563.                     coutspace("Enter a word to check the dictionary for:");
  564.                     getline(cin, tmp);
  565.                 }
  566.  
  567.                 if(searchDictionary(tmp) > -1) cout << endl << "Word number " << searchDictionary(tmp) <<
  568.                     " - \"" << tmp << "\" was found in the dictionary." << endl << endl;
  569.                 else coutspace("Could not find that word in the dictionary.");
  570.             }
  571.             else coutspace("Debug mode must be on to do a dictionary search.");
  572.  
  573.             break;
  574.         case '7': reseed(1); break;
  575.         case '8':
  576.             if(draw) { draw = false; coutspace("Draw hangman turned off."); }
  577.             else { draw = true; coutspace("Draw hangman turned on."); }
  578.             #ifndef __WIN32__
  579.             if(sounds) PlaySound(TEXT("sounds\\switch.wav"), NULL, SND_FILENAME | SND_ASYNC);
  580.             #endif
  581.  
  582.             break;
  583.         case '9':
  584.             #ifndef __WIN32__
  585.             if(sounds)
  586.             {
  587.               coutspace("Goodbye!");
  588.               PlaySound(TEXT("sounds\\quit.wav"), NULL, SND_FILENAME | SND_ASYNC);
  589.               Sleep(4500);
  590.             }
  591.             #endif
  592.             exit(0);
  593.  
  594.             break;
  595.         default:
  596.            
  597.             cout << endl << "Help" << endl << "----" << endl <<
  598.             "Enter 1 to start a new game." << endl <<
  599.             "Enter 2 to toggle debug mode." << endl <<
  600.             "Enter 3 to view information about the current game." << endl <<
  601.             "Enter 4 to view the dictionary." << endl <<
  602.             "Enter 5 to search the current word for a letter." << endl <<
  603.             "Enter 6 to search the dictionary for a word." << endl <<
  604.             "Enter 7 to reseed the random number generator." << endl <<
  605.             "Enter 8 to toggle the drawing of hangman after each incorrect guess." << endl <<
  606.             "Enter 9 to quit the game." << endl << endl <<
  607.             "Other Special Commands" << endl << "----------------------" << endl <<
  608.             "Enter ~ to view information about the program." << endl <<
  609.             "Enter ! to toggle game sounds on/off." << endl <<
  610.             "Enter ? to view a how to of the game.";
  611.             coutspace("----------------------");
  612.  
  613.             break;
  614.     }
  615. }
  616.  
  617. void doSpecialCommand(char input)
  618. {
  619.     switch(input)
  620.     {
  621.         case '~': about(); break;
  622.         case '!':
  623.             #ifndef __WIN32__
  624.             toggleSound();
  625.             #endif
  626.             break;
  627.         case '?': howTo(); break;
  628.         default: break;
  629.     }
  630. }
  631.  
  632. bool isAlphaStr(string word)
  633. {
  634.     bool valid = true;
  635.     char curchar;
  636.  
  637.     for(unsigned int i = 0; i < word.length(); i++)
  638.     {
  639.         curchar = word.at(i);
  640.         if(!isalpha(curchar)) { valid = false; break; }
  641.     }
  642.  
  643.     return valid;
  644. }
  645.  
  646. bool isUniqueStr(string word)
  647. {
  648.     if(word.length())
  649.     {
  650.         char fchar = word.at(0);
  651.         char curchar;
  652.         int ctr = 0;
  653.  
  654.         for(unsigned int i = 0; i < word.length(); i++)
  655.         {
  656.             if(i)
  657.             {
  658.                 curchar = word.at(i);
  659.                 if(fchar != curchar) { break; }
  660.                 else { ctr += 1; }
  661.             }
  662.         }
  663.  
  664.         if(ctr == (word.length() - 1)) return false;
  665.         else return true;
  666.     }
  667.  
  668.     return false;
  669. }
  670.  
  671. void viewDictionary()
  672. {
  673.     if(debug)
  674.     {
  675.     cout << endl << "Dictionary (" << dictionary.size() << ")" << endl << "----------" << endl;
  676.     for(unsigned int i = 0; i < dictionary.size(); i++)
  677.         cout << i + 1 << ". " << dictionary.at(i) << endl;
  678.     cout << "----------" << endl;
  679.     }
  680.     else coutspace("To view the dictionary debug mode must be on.");
  681. }
  682.  
  683. int searchDictionary(string word)
  684. {
  685.     int tmp = -1;
  686.     string tmpword = zapCase(word, 1);
  687.     string dictword;
  688.  
  689.     for(unsigned int i = 0; i < dictionary.size(); i++)
  690.     {
  691.         dictword = zapCase(dictionary.at(i), 1);
  692.         if(tmpword == dictword) { tmp = i; break; }
  693.     }
  694.  
  695.     return tmp;
  696. }
  697.  
  698. void toggleDebug()
  699. {
  700.     if(debug) { debug = false; coutspace("Debug mode off."); }
  701.     else { debug = true; coutspace ("Debug mode on."); }
  702.     if(sounds) PlaySound(TEXT("sounds\\switch.wav"), NULL, SND_FILENAME | SND_ASYNC);
  703. }
  704.  
  705. void reseed(int msg)
  706. {
  707.     srand(static_cast<int>(time(NULL)));
  708.     if(debug || msg) coutspace("Random number generator srand() has been reseeded.");
  709. }
  710.  
  711. void drawHangMan(int pos)
  712. {
  713.     #ifndef __WIN32__
  714.     HANDLE screen;
  715.     screen = GetStdHandle(STD_OUTPUT_HANDLE);
  716.     SetConsoleTextAttribute(screen, bg_gray_fg_blue);
  717.     #endif
  718.     switch(pos)
  719.     {
  720.         case 1:
  721.             cout << endl << "/---------" << endl <<
  722.                             "|        |" << endl <<
  723.                             "|        O" << endl <<
  724.                             "|"          << endl <<
  725.                             "|"          << endl <<
  726.                             "|"          << endl <<
  727.                             "|"          << endl <<
  728.                             "|"          << endl << endl;
  729.             break;
  730.  
  731.         case 2:
  732.             cout << endl << "/---------" << endl <<
  733.                             "|        |" << endl <<
  734.                             "|        O" << endl <<
  735.                             "|        |" << endl <<
  736.                             "|"          << endl <<
  737.                             "|"          << endl <<
  738.                             "|"          << endl <<
  739.                             "|"          << endl << endl;
  740.             break;
  741.  
  742.         case 3:
  743.             cout << endl << "/---------"  << endl <<
  744.                             "|        |"  << endl <<
  745.                             "|        O"  << endl <<
  746.                             "|       \\|" << endl <<
  747.                             "|"           << endl <<
  748.                             "|"           << endl <<
  749.                             "|"           << endl <<
  750.                             "|"           << endl << endl;
  751.             break;
  752.  
  753.         case 4:
  754.             cout << endl << "/---------"   << endl <<
  755.                             "|        |"   << endl <<
  756.                             "|        O"   << endl <<
  757.                             "|       \\|/" << endl <<
  758.                             "|"            << endl <<
  759.                             "|"            << endl <<
  760.                             "|"            << endl <<
  761.                             "|"            << endl << endl;
  762.             break;
  763.  
  764.         case 5:
  765.             cout << endl << "/---------"   << endl <<
  766.                             "|        |"   << endl <<
  767.                             "|        O"   << endl <<
  768.                             "|       \\|/" << endl <<
  769.                             "|        |"   << endl <<
  770.                             "|"            << endl <<
  771.                             "|"            << endl <<
  772.                             "|"            << endl << endl;
  773.             break;
  774.  
  775.         case 6:
  776.             cout << endl << "/---------"   << endl <<
  777.                             "|        |"   << endl <<
  778.                             "|        O"   << endl <<
  779.                             "|       \\|/" << endl <<
  780.                             "|        |"   << endl <<
  781.                             "|       /"    << endl <<
  782.                             "|"            << endl <<
  783.                             "|"            << endl << endl;
  784.             break;
  785.  
  786.         default:
  787.             cout << endl << "/---------"   << endl <<
  788.                             "|        |"   << endl <<
  789.                             "|        O"   << endl <<
  790.                             "|       \\|/" << endl <<
  791.                             "|        |"   << endl <<
  792.                             "|       / \\" << endl <<
  793.                             "|"            << endl <<
  794.                             "|"            << endl << endl;
  795.             break;
  796.     }
  797.     #ifndef __WIN32__
  798.     whitebg();
  799.     #endif
  800. }
  801.  
  802. void splashScreen()
  803. {
  804.     #ifndef __WIN32__
  805.     HANDLE screen;
  806.     screen = GetStdHandle(STD_OUTPUT_HANDLE);
  807.     red();
  808.     if(sounds) PlaySound(TEXT("sounds\\splash.wav"), NULL, SND_FILENAME | SND_ASYNC);
  809.     #endif
  810.     cout << endl <<
  811.         " _    _                   __  __" << endl <<
  812.         "| |  | |                 |  \\/  |" << endl <<
  813.         "| |__| | __ _ _ __   __ _| \\  / | __ _ _ __" << endl <<
  814.         "|  __  |/ _` | '_ \\ / _` | |\\/| |/ _` | '_ \\" << endl <<
  815.         "| |  | | (_| | | | | (_| | |  | | (_| | | | |" << endl <<
  816.         "|_|  |_|\\__,_|_| |_|\\__, |_|  |_|\\__,_|_| |_|" << endl <<
  817.         "                     __/ |" << endl <<
  818.         "                    |___/            ";
  819.    
  820.     #ifndef __WIN32__
  821.     blue();
  822.     #endif
  823.     cout << "-By Bukz" << endl;
  824.     #ifndef __WIN32__
  825.     whitebg();
  826.     #endif
  827. }
  828.  
  829. void about()
  830. {
  831.     #ifndef __WIN32__
  832.     HANDLE screen;
  833.     screen = GetStdHandle(STD_OUTPUT_HANDLE);
  834.     red();
  835.     #endif
  836.    
  837.     cout << endl << "\tAbout HangMan";
  838.     coutspace("\t-------------");
  839.     cout << "\tHangMan version 2.1" << endl;
  840.  
  841.     cout << "\tWritten in C++ by ";
  842.    
  843.     #ifndef __WIN32__
  844.     blue();
  845.     #endif
  846.     cout << "Shane \"Bukz\" Pinkley" << endl << endl;
  847.     #ifndef __WIN32__
  848.     red();
  849.     #endif
  850.    
  851.     cout << "\tLicensed under the Attribution-NonCommercial-ShareAlike" << endl << "\t3.0 Unported (CC BY-NC-SA 3.0) creative commons license." << endl << endl <<
  852.         "\tFor more information about the license visit:" << endl << endl;
  853.    
  854.     #ifndef __WIN32__
  855.     blue();
  856.     #endif
  857.     cout << "\t\thttp://creativecommons.org/licenses/by-nc-sa/3.0/" << endl << endl;
  858.     #ifndef __WIN32__
  859.     red();
  860.     #endif
  861.    
  862.     cout << "\tAll sounds were taken from:" << endl << endl;
  863.    
  864.     #ifndef __WIN32__
  865.     blue();
  866.     #endif
  867.     cout << "\t\thttp://www.freesound.org" << endl << endl;
  868.     #ifndef __WIN32__
  869.     red();
  870.     #endif
  871.    
  872.     cout << "\tand are licensed under the Sampling Plus 1.0 creative" << endl << "\tcommons license." << endl << endl <<
  873.         "\tFor more information about the license visit:" << endl << endl;
  874.    
  875.     #ifndef __WIN32__
  876.     blue();
  877.     #endif
  878.     cout << "\t\thttp://creativecommons.org/licenses/sampling+/1.0/" << endl << endl;
  879.    
  880.     #ifndef __WIN32__
  881.     red();
  882.     #endif
  883.     cout << "\t2011 All Rights Reserved" << endl;
  884.     coutspace("\t-------------");
  885.    
  886.     #ifndef __WIN32__
  887.     whitebg();
  888.     #endif
  889. }
  890.  
  891. #ifndef __WIN32__
  892. void screenFun()
  893. {
  894.     // For console title
  895.     SetConsoleTitle(_T("Hangman 2.1 by Bukz"));
  896.    
  897.     // For console full screen workaround
  898.     HANDLE hOut;
  899.     COORD NewSBSize;
  900.     SMALL_RECT Area = {0, 0, 0, 0};
  901.  
  902.     hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  903.  
  904.     NewSBSize = GetLargestConsoleWindowSize(hOut);
  905.  
  906.     SetConsoleScreenBufferSize(hOut, NewSBSize);
  907.  
  908.     Area.Right = NewSBSize.X - 1;
  909.     Area.Bottom = NewSBSize.Y - 1;
  910.  
  911.     SetConsoleWindowInfo(hOut, TRUE,  &Area);
  912.  
  913.     // For console auto-maximize
  914.     HWND hWnd = GetConsoleWindow();
  915.     ShowWindow(hWnd,SW_SHOWMAXIMIZED);
  916.  
  917.     // For console colors
  918.     system("color f0");
  919. }
  920. #endif
  921.  
  922. void init()
  923. {
  924.     #ifndef __WIN32__
  925.     // Setup the screen
  926.     screenFun();
  927.     #endif
  928.  
  929.     // Read words.txt and howto.txt
  930.     readWordsFile();
  931.     readHowToFile();
  932.  
  933.     // Seed the random number generator
  934.     reseed();
  935.  
  936.     newgame();
  937. }
  938.  
  939. int main()
  940. {
  941.     init();
  942.  
  943.     return 0;
  944. }
Advertisement
Add Comment
Please, Sign In to add comment