MrPaan

updated

Dec 12th, 2018
493
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <windows.h>
  4. #include <string>
  5. #include <cstdlib>
  6. #include <ctime>
  7. #include <time.h>
  8.  
  9. using namespace std;
  10.  
  11. class hangMan{
  12.    
  13.     public:
  14.         int playerNumb;
  15.         int pickCat;
  16.         void PickCategory();
  17.         void PickPlayer();
  18.         int emulateImage(int numb);
  19.         int mainMenu();
  20.         int Play();
  21.         int GuessWord(char guess,string realwords, string &guesswords);
  22.         string ChooseCat();
  23.    
  24. };
  25.  
  26. void hangMan::PickPlayer(){
  27.     cout << "Please select number of player : " << endl;
  28.     cout << "(1). One Player" << endl;
  29.     cout << "(2). Two Player" <<endl;
  30.     cin >> playerNumb;
  31. }
  32.  
  33.  
  34. void hangMan::PickCategory(){
  35.     cout << "Select Category : " << endl;
  36.     cout << "(1). Country : " << endl;
  37.     cout << "(2). State in Malaysia : " << endl;
  38.     cout << "(3). District in Johor : " << endl;
  39.     cin >> pickCat;
  40. }
  41.  
  42. string hangMan::ChooseCat(){
  43.     //srand(time(NULL));
  44.     int n = rand()%((10+1)-1) + 1;
  45.     //int n = rand() % (10-1 + 1) + 1;
  46.     int cat = pickCat;
  47.    
  48.     string country[] = {
  49.     "Malaysia",
  50.     "Indonesia",
  51.     "Singapore",
  52.     "Brunei",
  53.     "Thailand",
  54.     "Israel",
  55.     "Africa",
  56.     "Nigeria",
  57.     "Korea",
  58.     "Sunda"
  59.     };
  60.    
  61.     string state[] = {
  62.     "Johor",
  63.     "Kedah",
  64.     "Sabah",
  65.     "Sarawak",
  66.     "Kelantan",
  67.     "Terengganu",
  68.     "Perak",
  69.     "Melaka",
  70.     "Penang",
  71.     "Kedah"
  72.     };
  73.    
  74.     string district[] = {
  75.     "Kluang",
  76.     "Batu Pahat",
  77.     "Kota Tinggi",
  78.     "Skudai",
  79.     "Kulai",
  80.     "Pontian",
  81.     "Johor Bahru",
  82.     "Segamat",
  83.     "Mersing",
  84.     "Muar"
  85.     };
  86.    
  87.     if(cat == 1) {return country[n];}
  88.     else if(cat == 2) {return state[n];}
  89.     else if(cat == 3) {return district[n];}
  90.    
  91.    
  92. }
  93.  
  94. int hangMan::GuessWord(char guess,string realwords,string &guesswords){
  95.     int i = 0;
  96.     int matches = 0;
  97.     int count = realwords.length();
  98.    
  99.     for(i = 0; i < count; i++){
  100.        
  101.         if(guess == guesswords[i]) {return 0;}
  102.         if(guess == realwords[i]){
  103.             guesswords[i] = guess;
  104.             matches++;
  105.         }
  106.     }
  107.    
  108.     return matches;
  109. }
  110.  
  111. int hangMan::emulateImage(int numb){
  112.  
  113. //Atas
  114. cout << "+---------------------------------+" << endl;
  115. cout << "|             HANG MAN            |" << endl;
  116. cout << "+---------------------------------+" << endl;
  117.  
  118. cout << "|               |                 |" << endl;
  119.  
  120. if(numb == 4) {
  121.  
  122. cout << "|               O                 |" << endl;
  123.  
  124. }
  125.  
  126. if(numb == 3){
  127.     cout << "|               |                 |" << endl;
  128.     cout << "|               O                 |" << endl;
  129. }
  130.  
  131. if(numb == 2) {
  132.         cout << "|               |                 |" << endl;
  133.         cout << "|               |                 |" << endl;
  134.         cout << "|               O                 |" << endl;
  135.    
  136. }
  137. if(numb == 1){
  138.         cout << "|               |                 |" << endl;
  139.         cout << "|               |                 |" << endl;
  140.         cout << "|               |                 |" << endl;
  141.         cout << "|               O                 |" << endl;
  142.    
  143. }
  144.  
  145. if(numb == 0){
  146.         cout << "|               |                 |" << endl;
  147.         cout << "|               |                 |" << endl;
  148.         cout << "|               |                 |" << endl;
  149.         cout << "|               X                 |" << endl;
  150.    
  151. }
  152. cout << "|              /|\\                |" << endl;
  153. cout << "|               |                 |" << endl;
  154. cout << "|              / \\                |" << endl;
  155. cout << "|         +----------+            |" << endl;
  156. cout << "|         |          |            |" << endl;
  157. cout << "+---------------------------------+" << endl;
  158. cout << "|         Guess the word          |" << endl;
  159. cout << "+---------------------------------+" << endl;
  160. }
  161.  
  162. int hangMan::mainMenu(){
  163.     cout << "+---------------------------------+" << endl;
  164.     cout << "|             HANG MAN            |" << endl;
  165.     cout << "+---------------------------------+" << endl << endl<< endl;
  166. }
  167.  
  168. int hangMan::Play(){
  169.    
  170.     int tengokPlayer = playerNumb,convArr;
  171.     char letter;
  172.     string wordslala = ChooseCat();
  173.     int win = 0;
  174.    
  175.     int counter,salah = 0;
  176.     int multiplePlayer[2][2],mCounter = 1,nowPlaying;
  177.    
  178.     //
  179.     //8 kali loop , 2 player
  180.     if(tengokPlayer == 1 ) { counter = 4; } else if(tengokPlayer == 2) { counter = 8; }
  181.    
  182.     if(counter == 4){
  183.        
  184.         string unknown(wordslala.length(),'*');
  185.        
  186.         int bilHuruf = wordslala.length();
  187.         int bil = 0;
  188.         while(salah < counter){
  189.            
  190.                 cout << "Your Words to guess : " << unknown << endl;
  191.                 cout << "Input Letter : ";
  192.                 cin >> letter;
  193.                 cout << endl;
  194.                 system("CLS");
  195.                 if (GuessWord(letter, wordslala, unknown)==0) {
  196.                 salah++;
  197.                 emulateImage(counter-salah);
  198.                 cout << endl << "Whoops! That letter isn't in there!" << endl;
  199.                 cout << "You have " << counter - salah << " try !" << endl;
  200.                  }
  201.                 else { emulateImage(counter-salah);  cout << "You've found a letter" << endl; bil++;}
  202.                
  203.                 if(bilHuruf == bil){
  204.                     win = 1;break;
  205.                    
  206.                 }
  207.             }
  208.             cout << "The words is " << wordslala << endl;
  209.             if(win == 1) {cout << "You Win !" << endl;}
  210.     }else if(counter == 8){
  211.    
  212.     int mark1 = 0,mark2 = 0;
  213.     int win;
  214.             //Player 1 Declare
  215.             multiplePlayer[0][0] = 1;
  216.             multiplePlayer[0][1] = 4;
  217.            
  218.             //Player 2 Declare
  219.             multiplePlayer[1][0] = 2;
  220.             multiplePlayer[1][1] = 4;
  221.            
  222.             nowPlaying = multiplePlayer[0][0];
  223.            
  224.             string unknown(wordslala.length(),'*');
  225.             string secretWords1 = unknown;
  226.             string secretWords2 = unknown;
  227.            
  228.             int bilHurufMulti = wordslala.length();
  229.             int betulSatu = 0;
  230.             int betulDua = 0;
  231.            
  232.         while(true){
  233.             mCounter++;
  234.             //Check boleh masuk game ke tidak player 1 dan 2
  235.             //index 0 , tentukan player
  236.             //index 1 , berapa kali try
  237.            
  238.             if(nowPlaying == 1){
  239.                 //Check Boleh Main Tak
  240.                 if(multiplePlayer[0][1] != 0){
  241.                         cout << endl << "Player 1 " << endl;
  242.                                         cout << "Your Words to Guess : " << secretWords1 << endl;
  243.                                         cout << "Input letter : ";
  244.                                         cin >> letter;
  245.                                         cout << endl;
  246.                                         system("CLS");
  247.                                         if (GuessWord(letter, wordslala, secretWords1)==0) {
  248.                                         multiplePlayer[0][1]--;
  249.                                         convArr = multiplePlayer[0][1];
  250.                                         emulateImage(convArr);
  251.                                         cout << endl << "Whoops! That letter isn't in there!" << endl;
  252.                                         cout << "You have " << multiplePlayer[0][1]<< " try left" << endl;
  253.                                         system("pause"); system("cls");
  254.                                          }
  255.                                         else {
  256.                                         ++mark1;
  257.                                         betulSatu++;
  258.                                         emulateImage(multiplePlayer[0][1]);
  259.                                         cout << "Player 1" << betulSatu << bilHurufMulti << endl;
  260.                                         cout << "You've found a letter ";
  261.                                         cout << secretWords1 << endl;
  262.                                         system("pause"); system("cls");}
  263.                                        
  264.                                         nowPlaying = 2;
  265.                                     }
  266.                             nowPlaying = 2;
  267.                            
  268.                         //  if(multiplePlayer[0][1] == 0) {win = 2; break;}
  269.                    
  270.                 }
  271.                
  272.                 if(nowPlaying == 2){
  273.                     //Check Boleh Main Tak
  274.                         if(multiplePlayer[1][1] != 0){
  275.                         cout << endl << "Player 2 " << endl;
  276.                                         cout << "Your Words to Guess : " << secretWords1 << endl;
  277.                                         cout << "Input letter : ";
  278.                                         cin >> letter;
  279.                                         cout << endl;
  280.                                         system("CLS");
  281.                                         if (GuessWord(letter, wordslala, secretWords1)==0) {
  282.                                         multiplePlayer[1][1]--;
  283.                                         convArr = multiplePlayer[1][1];
  284.                                         emulateImage(convArr);
  285.                                         cout << endl << "Whoops! That letter isn't in there!" << endl;
  286.                                         cout << "You have " << multiplePlayer[1][1] << " try left" << endl;
  287.                                         system("pause"); system("cls");
  288.                                          }
  289.                                         else {
  290.                                         ++mark2;
  291.                                         betulDua++;
  292.                                         emulateImage(multiplePlayer[1][1]);
  293.                                         cout << "Player 2" << endl;
  294.                                         cout << "You've found a letter ";
  295.                                         cout << secretWords1 << endl;
  296.                                         system("pause");
  297.                                         system("cls"); }
  298.                                         nowPlaying = 1;
  299.                                     }
  300.                                     nowPlaying = 1;
  301.                                     //if(multiplePlayer[1][1] == 0) {win = 1; break;}
  302.                 }
  303.                
  304.                 //Kalau dua2 0
  305.                 if((multiplePlayer[0][1] == 0) && (multiplePlayer[1][1] == 0)){
  306.                     break;
  307.                 }
  308.                
  309.                 int totalBetul = betulSatu + betulDua;
  310.                 if(bilHurufMulti == betulSatu) { break;}
  311.                 if(bilHurufMulti == betulDua) { break; }
  312.                 if(totalBetul == bilHurufMulti) { break; }
  313.                
  314.             }      
  315.             //Check Winner
  316.             if(mark1 > mark2){
  317.             cout << "Player 1 Win" << endl;
  318.             cout << "The words is " << wordslala << endl;
  319.             }
  320.             else if(mark2 > mark1){
  321.             cout << "Player 2 Win" << endl;
  322.             cout << "The words is " << wordslala << endl;
  323.             }else if(mark1 == mark2){
  324.             cout << "Draw Game" << endl;
  325.             cout << "The words is " << wordslala << endl;
  326.             }
  327.         }
  328.         }
  329.        
  330.        
  331.  
  332. int main(){
  333.     hangMan play;
  334.    
  335.     play.mainMenu();
  336.     play.PickPlayer();
  337.     system("CLS");
  338.     play.mainMenu();
  339.     play.PickCategory();
  340.     play.ChooseCat();
  341.     system("CLS");
  342.     play.Play();
  343. }
Advertisement
Add Comment
Please, Sign In to add comment