Advertisement
Guest User

Untitled

a guest
Dec 29th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #include <vector>
  5. #include <unistd.h>
  6. #include <limits>
  7.  
  8.  
  9. using namespace std;
  10.  
  11. const string chars = "abcdefghijklmnopqrstuvwxy";
  12.  
  13.  
  14. void spawn_shell() {
  15.     char* args[] = {(char*)"/bin/bash", NULL};
  16.     execve("/bin/bash", args, NULL);
  17. }
  18.  
  19.  
  20. void print_menu() {
  21.     cout << endl;
  22.     cout << "Enter the command you want to execute:" << endl;
  23.     cout << "[1] swap <index1> <index2>                   (Cost: 1)" << endl;
  24.     cout << "[2] replace <char1> <char2>                  (Cost: 1)" << endl;
  25.     cout << "[3] print                                    (Cost: 1)" << endl;
  26.     cout << "[4] quit                                              " << endl;
  27.     cout << "> ";
  28. }
  29.  
  30. void play() {
  31.     string from(10, '\00');
  32.     string to(10, '\00');
  33.     for (int i = 0; i < 10; ++i) {
  34.         from[i] = chars[rand() % (chars.length() - 1)];
  35.         to[i] = chars[rand() % (chars.length() - 1)];
  36.     }
  37.  
  38.  
  39.     cout << "Perform the following operations on String1 to generate String2 with minimum costs." << endl << endl;
  40.     cout << "[1] swap <index1> <index2>                   (Cost: 1)" << endl;
  41.     cout << "    Swaps the char at index1 with the char at index2  " << endl;
  42.     cout << "[2] replace <char1> <char2>                  (Cost: 1)" << endl;
  43.     cout << "    Replaces the first occurence of char1 with char2  " << endl;
  44.     cout << "[3] print                                    (Cost: 1)" << endl;
  45.     cout << "    Prints the current version of the string          " << endl;
  46.     cout << "[4] quit                                              " << endl;
  47.     cout << "    Give up and leave the game                        " << endl;
  48.     cout << endl;
  49.     cout << "String1: " << from << endl;
  50.     cout << "String2: " << to << endl;
  51.     cout << endl;
  52.        
  53.     unsigned int costs = 0;
  54.     string s(from);
  55.  
  56.     while (true) {
  57.         print_menu();
  58.  
  59.         string command;
  60.         cin >> command;
  61.  
  62.         if (command == "swap") {
  63.             unsigned int i1, i2;
  64.             cin >> i1 >> i2;
  65.             if (cin.good() && i1 < s.length() && i2 < s.length()) {
  66.                 swap(s[i1], s[i2]);
  67.             }
  68.             costs += 1;
  69.         } else if (command == "replace") {
  70.             char c1, c2;
  71.             cin >> c1 >> c2;
  72.             auto index = s.find(c1);
  73.             cout << c1 << c2 << index << endl;
  74.             if (index >= 0) {
  75.                 s[index] = c2;
  76.             }
  77.             costs += 1;
  78.         } else if (command == "print") {
  79.             cout << s << endl;
  80.             costs += 1;
  81.         } else if (command == "quit") {
  82.             cout << "You lost." << endl;
  83.             break;
  84.         } else {
  85.             cout << "Invalid command" << endl;
  86.         }
  87.  
  88.         if (!cin) {
  89.             cin.clear();
  90.             cin.ignore(numeric_limits<streamsize>::max(), '\n');
  91.         }
  92.         if (!cout) {
  93.             cout.clear();
  94.         }
  95.  
  96.         if (s == to) {
  97.             cout << s.length() << endl;
  98.             cout << endl;
  99.             cout << "****************************************" << endl;
  100.             cout << "* Congratulations                       " << endl;
  101.             cout << "* You solved the problem with cost: " << costs << endl;
  102.             cout << "****************************************" << endl;
  103.             cout << endl;
  104.             break;
  105.         }
  106.     }
  107. }
  108.  
  109.  
  110.  
  111.  
  112. int main() {
  113.     srand(time(nullptr));
  114.  
  115.     play();
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement