csansoon

P8.11 X12847 Battleship (1)

Nov 29th, 2018
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. typedef vector< vector<bool> > Board;
  7.  
  8. const int N = 10;
  9.  
  10. void dibuixar_tauler(const Board& tauler) {
  11.     cout << "  12345678910" << endl;
  12.    
  13.     for (int i = 0; i < N; ++i) {
  14.         cout << char(int('a') + i) << ' ';
  15.         for (int j = 0; j < N; ++j) {
  16.             if (tauler[i][j]) cout << 'X';
  17.             else cout << '.';
  18.         }
  19.         cout << endl;
  20.     }
  21. }
  22.  
  23. void colocar_vaixell(Board& tauler) {
  24.    
  25.     char c1, orientacio;
  26.     int j, mida;
  27.    
  28.     cin >> c1 >> j >> mida >> orientacio;
  29.    
  30.     int i = int(c1 - 'a');
  31.     j = j - 1;
  32.    
  33.     if (orientacio == 'h') {
  34.         for (int k = 0; k < mida; ++k) {
  35.             tauler[i][j+k] = true;
  36.         }
  37.     } else {
  38.         for (int k = 0; k < mida; ++k) {
  39.             tauler[i+k][j] = true;
  40.         }  
  41.     }
  42. }
  43.  
  44. int min(int a, int b) {
  45.     if (a <= b) return a;
  46.     else return b;
  47. }
  48. int max(int a, int b) {
  49.     if (a >= b) return a;
  50.     else return b;
  51. }
  52.  
  53. bool trobar_horizontal(const Board& tauler, int i, int from, int to) {
  54.     for (int j = from; j <= to; ++j) {
  55.         if (tauler[i][j]) return true;
  56.     }
  57.     return false;
  58. }
  59.  
  60. bool trobar_vertical(const Board& tauler, int j, int from, int to) {
  61.     for (int i = from; i <= to; ++i) {
  62.         if (tauler[i][j]) return true;
  63.     }
  64.     return false;
  65. }
  66.  
  67. int calcular_distancia(const Board& tauler, int i0, int j0) {
  68.     int dist = 1;
  69.    
  70.     while (dist <= 10) {
  71.         // Busquem la part de dalt
  72.         if (i0 - dist >= 0) {
  73.             if (trobar_horizontal(tauler, i0-dist, max(0, j0 - dist), min(j0 + dist, N-1))) return dist;
  74.         }
  75.        
  76.         // Busquem la part de baix
  77.         if (i0 + dist < N) {
  78.             if (trobar_horizontal(tauler, i0+dist, max(0, j0 - dist), min(j0 + dist, N-1))) return dist;
  79.         }
  80.        
  81.         // Busquem la part esquerra
  82.         if (j0 - dist >= 0) {
  83.             if (trobar_vertical(tauler, j0-dist, max(0, i0 - dist), min(i0 + dist, N-1))) return dist;
  84.         }
  85.        
  86.         // Busquem la part dreta
  87.         if (j0 + dist < N) {
  88.             if (trobar_vertical(tauler, j0+dist, max(0, i0 - dist), min(i0 + dist, N-1))) return dist;
  89.         }
  90.    
  91.         ++dist;
  92.     }
  93.  
  94.     return dist;
  95. }
  96.  
  97. void processar_tirada(const Board& tauler, int i, int j) {
  98.     cout << char(int('a') + i) << j+1;
  99.    
  100.     if (tauler[i][j]) {
  101.         cout << " touched!" << endl;
  102.     } else {
  103.         cout << " water! closest ship at distance " << calcular_distancia(tauler, i, j) << endl;
  104.     }
  105. }
  106.  
  107. int main() {
  108.    
  109.     Board tauler = Board(N, vector<bool>(N, false));
  110.    
  111.     for (int i = 0; i < 10; ++i) colocar_vaixell(tauler);
  112.    
  113.     dibuixar_tauler(tauler);
  114.     cout << endl;
  115.    
  116.     char c;
  117.     int j;
  118.    
  119.     while (cin >> c >> j) {
  120.         int i = int(c - 'a');
  121.         j = j - 1;
  122.         processar_tirada(tauler, i, j);
  123.     }
  124. }
  125.  
  126. // (c) Carlos Sansón (Best pro1 delegate ever for sure) @csansoon
Advertisement
Add Comment
Please, Sign In to add comment