Advertisement
Guest User

morracinese

a guest
Oct 17th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.17 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <ctime>
  3. #include <iostream>
  4. #include <string>
  5. using namespace std;
  6.  
  7. int main() {
  8.   int round, i = 0;
  9.   int player_score = 0;
  10.   int pc_score = 0;
  11.   int pc_choice = 0;
  12.   char pc_to_human;
  13.   char player_choice;
  14.   // strings are completely unuseful but make the game easier to understand
  15.   string pc_full_choice, player_full_choice = "";
  16.  
  17.   cout << "Giochiamo a morra cinese! "
  18.        << "quanti turni vuoi giocare?" << endl;
  19.   cin >> round;
  20.  
  21.   if (cin.fail()) {
  22.     cout << "numero di round non valido" << endl;
  23.   } else {
  24.  
  25.     for (i; i < round; i++) {
  26.       cout << "----------------------------" << endl;
  27.       cout << "Inserisci la tua giocata\n[c] => carta \n[f] => forbice\n[s] => "
  28.               "sasso \n"
  29.            << endl;
  30.       cin >> player_choice;
  31.  
  32.       if (player_choice == 'c')
  33.         player_full_choice = "carta";
  34.  
  35.       else if (player_choice == 'f')
  36.         player_full_choice = "forbice";
  37.  
  38.       else if (player_choice == 's')
  39.         player_full_choice = "sasso";
  40.  
  41.       if (player_choice != 'c' && player_choice != 'f' && player_choice != 's')
  42.         cout << "la tua scelta non รจ valida" << endl;
  43.  
  44.       else {
  45.  
  46.         srand(time(0));
  47.         pc_choice = rand() % 3;
  48.  
  49.         if (pc_choice == 0) {
  50.           pc_to_human = 'c';
  51.           pc_full_choice = "carta";
  52.         } else if (pc_choice == 1) {
  53.           pc_to_human = 'f';
  54.           pc_full_choice = "forbice";
  55.         } else {
  56.           pc_to_human = 's';
  57.           pc_full_choice = "sasso";
  58.         }
  59.  
  60.         cout << "hai giocato " << player_full_choice << ", io gioco "
  61.              << pc_full_choice << "\n"
  62.              << endl;
  63.  
  64.         if (player_choice == pc_to_human)
  65.           cout << "Hai pareggiato\n" << endl;
  66.  
  67.         else {
  68.           switch (player_choice) {
  69.           case 'c':
  70.             if (pc_to_human == 's') {
  71.               cout << "Hai vinto, carta batte sasso!\n" << endl;
  72.               player_score++;
  73.             } else {
  74.               cout << "Hai perso, forbice batte carta!\n" << endl;
  75.               pc_score++;
  76.             }
  77.  
  78.             break;
  79.           case 'f':
  80.             if (pc_to_human == 'c') {
  81.               cout << "Hai vinto, forbice batte carta!\n" << endl;
  82.               player_score++;
  83.  
  84.             } else {
  85.               cout << "Hai perso, sasso batte carta!\n" << endl;
  86.               pc_score++;
  87.             }
  88.             break;
  89.           case 's':
  90.             if (pc_to_human == 'f') {
  91.               cout << "Hai vinto, sasso batte forbice!\n" << endl;
  92.               player_score++;
  93.             } else {
  94.               cout << "Hai perso, carta batte sasso!\n" << endl;
  95.               pc_score++;
  96.             }
  97.             break;
  98.           }
  99.         }
  100.       }
  101.     }
  102.     if (player_score > pc_score)
  103.       cout << "===> RISULTATO FINALE: hai vinto " << player_score << " - "
  104.            << pc_score << endl;
  105.     else if (player_score == pc_score)
  106.       cout << "===> RISULTATO FINALE: abbiamo pareggiato " << player_score
  107.            << " - " << pc_score << endl;
  108.     else
  109.       cout << "==> RISULTATO FINALE: ho vinto io " << pc_score << " - "
  110.            << player_score << endl;
  111.   }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement