Advertisement
xerpi

Fire! random

Dec 3rd, 2013
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.42 KB | None | 0 0
  1. /*
  2.     Copyright (c) 2013   xerpi
  3. */
  4.  
  5. #include <iostream>
  6. #include <vector>
  7. #include <cmath>
  8. #include <cstdlib>
  9. #include <unistd.h>
  10. using namespace std;
  11.  
  12. typedef vector<char>  Row;
  13. typedef vector<Row>   Matrix;
  14.  
  15. void read_matrix(Matrix& m, int rows, int columns);
  16. void print_matrix(Matrix& m, int rows, int columns);
  17. void fire_forest(Matrix& m, int rows, int columns);
  18. void random_matrix(Matrix& m, int rows, int columns);
  19.  
  20.  
  21. int main()
  22. {
  23.     int n, m;
  24.     while (cin >> n >> m) {
  25.         Matrix forest(n, Row(m));
  26.         cout << "\033[2J\033[1;1H";
  27.         //read_matrix(forest, n, m);
  28.         random_matrix(forest, n, m);
  29.         fire_forest(forest, n, m);
  30.         cout << "\033[2J\033[1;1H";
  31.         print_matrix(forest, n, m);
  32.         cout << endl;
  33.     }
  34. }
  35.  
  36.  
  37. void fire_forest(Matrix& m, int rows, int columns)
  38. {
  39.     int burned_trees = 1;
  40.     while (burned_trees != 0) {
  41.         cout << "\033[2J\033[1;1H";
  42.         burned_trees = 0;
  43.         for (int i = 0; i < rows; ++i) {
  44.             for (int j = 0; j < columns; ++j) {
  45.                 if (m[i][j] == 'F') {
  46.                     //Dalt
  47.                     if (i > 0) {
  48.                         if (m[i-1][j] == 'A') {
  49.                             m[i-1][j] = 'B';
  50.                             ++burned_trees;
  51.                         }
  52.                     }
  53.                     //Baix
  54.                     if (i < (rows-1)) {
  55.                         if (m[i+1][j] == 'A') {
  56.                             m[i+1][j] = 'B';
  57.                             ++burned_trees;
  58.                         }
  59.                     }
  60.                     //Esquerra
  61.                     if (j > 0) {
  62.                         if (m[i][j-1] == 'A') {
  63.                             m[i][j-1] = 'B';
  64.                             ++burned_trees;
  65.                         }
  66.                     }
  67.                     //Dreta
  68.                     if (j < (columns-1)) {
  69.                         if (m[i][j+1] == 'A') {
  70.                             m[i][j+1] = 'B';
  71.                             ++burned_trees;
  72.                         }
  73.                     }                    
  74.                 }
  75.             }
  76.         }
  77.         for (int i = 0; i < rows; ++i) {
  78.             for (int j = 0; j < columns; ++j) {
  79.                 if (m[i][j] == 'B') {
  80.                     m[i][j] = 'F';
  81.                 }
  82.             }
  83.         }
  84.         print_matrix(m, rows, columns);
  85.         usleep(250 * 1000);  
  86.     }
  87.     for (int i = 0; i < rows; ++i) {
  88.         for (int j = 0; j < columns; ++j) {
  89.             if (m[i][j] == 'F') {
  90.                 m[i][j] = '.';
  91.             }
  92.         }
  93.     }  
  94. }
  95.  
  96. void random_matrix(Matrix& m, int rows, int columns)
  97. {
  98.     for (int i = 0; i < rows; ++i) {
  99.         for (int j = 0; j < columns; ++j) {
  100.             int n = rand()%100;
  101.             if (n < 10) {
  102.                 m[i][j] = 'F';
  103.             } else if (n >= 10 and n < 70) {
  104.                 m[i][j] = 'A';
  105.             } else  {
  106.                m[i][j] = '.';
  107.             }
  108.         }
  109.     }
  110.    
  111. }
  112.  
  113.  
  114.  
  115. void print_matrix(Matrix& m, int rows, int columns)
  116. {
  117.     for (int i = 0; i < rows; ++i) {
  118.         for (int j = 0; j < columns; ++j) {
  119.             cout << m[i][j];
  120.         }
  121.         cout << endl;
  122.     }
  123. }
  124.  
  125. void read_matrix(Matrix& m, int rows, int columns)
  126. {
  127.     for (int i = 0; i < rows; ++i) {
  128.         for (int j = 0; j < columns; ++j) {
  129.             cin >> m[i][j];
  130.         }      
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement