SHARE
TWEET

Untitled

a guest Oct 9th, 2019 92 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
  4.     #include <cstdlib>
  5.     #define CLEAR_COMMAND "cls"
  6. #else
  7.     #define CLEAR_COMMAND "clear"
  8. #endif
  9.  
  10. #define RIGHE 3
  11. #define COLONNE 5
  12.  
  13. void stampa_griglia(int** matrice, int righe, int colonne, int turno);
  14.  
  15. int main(){
  16.  
  17.     srand(time(NULL));
  18.  
  19.     int r1x, r1y, r2x, r2y;
  20.     int x, y;
  21.     int turni = 0;
  22.     int sommergibili_trovati = 0;
  23.     std::string coordinate;
  24.  
  25.     int** griglia = (int**) malloc(RIGHE * sizeof(int*));
  26.     for (int i = 0; i < RIGHE; i++) griglia[i] = (int*) std::calloc(COLONNE, sizeof(int));
  27.  
  28.     do{
  29.         r1x = rand() % RIGHE;
  30.         r2x = rand() % RIGHE;
  31.         r1y = rand() % COLONNE;
  32.         r2y = rand() % COLONNE;
  33.     }while(r1x == r2x && r1y == r2y);
  34.  
  35.     griglia[r1x][r1y] = 1;
  36.     griglia[r2x][r2y] = 1;
  37.  
  38.     while (sommergibili_trovati != 2){
  39.         stampa_griglia(griglia, RIGHE, COLONNE, turni + 1);
  40.  
  41.         std::cout << "Coordinate su dove sganciare la prossima bomba (formato: a1): ";
  42.         std::cin >> coordinate;
  43.  
  44.         x = coordinate[1] - '0';
  45.         y = coordinate[0] - 'a';
  46.  
  47.         while(!(y >= 0 && y - 'a' < COLONNE) || !(x >= 0 && x <= RIGHE) || coordinate.length() != 2){
  48.             std::cout << "[ERRORE] Reinserire coordinate su dove sganciare la prossima bomba (formato: a1): ";
  49.             std::cin >> coordinate;
  50.  
  51.             x = coordinate[1] - '0';
  52.             y = coordinate[0] - 'a';
  53.         }
  54.  
  55.         x = abs(x - RIGHE);
  56.  
  57.         if (griglia[x][y] == 2){
  58.             std::cout << "[ERRORE] Posizione gia' inserita!\n";
  59.             std::cin.ignore();
  60.             getchar();
  61.         }
  62.  
  63.         else{
  64.             if (griglia[x][y] == 1){
  65.                 griglia[x][y] = 3;
  66.                 std::cout << "\n\nComplimenti hai trovato il " << ++sommergibili_trovati << "* sommergibile!";
  67.                 std::cin.ignore();
  68.                 getchar();
  69.             }
  70.             else griglia[x][y] = 2;
  71.             turni++;
  72.         }
  73.     }
  74.  
  75.     stampa_griglia(griglia, RIGHE, COLONNE, turni);
  76.     std::cout << "\n[HAI VINTO] Numero turni giocati: " << turni << "\n";
  77.  
  78.     std::cout << "\n\nPress enter to continue...";
  79.     std::cin.ignore();
  80.     getchar();
  81.  
  82.     return EXIT_SUCCESS;
  83. }
  84.  
  85. void stampa_griglia(int** matrice, int righe, int colonne, int turno){
  86.     system(CLEAR_COMMAND);
  87.     std::cout << ">> Turno #" << turno << "\n";
  88.     std::cout << std::string(COLONNE * 2 + 4, '-') << "\n";
  89.     for(int i = 0; i < righe; i++){
  90.         std::cout << righe - i << "| ";
  91.         for(int j = 0; j < colonne; j++){
  92.             std::cout << ((matrice[i][j] == 1) ? 0 : matrice[i][j]) << " ";
  93.         }
  94.         std::cout << "|\n";
  95.     }
  96.     std::cout << std::string(COLONNE * 2 + 4, '-') << "\n   ";
  97.     for (int i = 0; i < COLONNE; i++) std::cout << (char) ('a' + i) << " ";
  98.     std::cout << "\n\n";
  99. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top