Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.99 KB | None | 0 0
  1. #include<iostream>
  2. #include<time.h>
  3.  
  4. using namespace std;
  5.  
  6. int** buildboard(int size);
  7. void pizurNekudot(int **board, int size);
  8. void print(int **arr, int size, int generation);
  9. void ongoing(int **board, int size);
  10. bool rules(int **board, int size, int rowkey, int colkey, bool alive);
  11. void swaparr(int **board, int **temp, int size);
  12. bool alldead(int **board, int size);
  13.  
  14.  
  15. int main()
  16. {
  17.     srand(time(NULL));
  18.     int size;
  19.     cin >> size;
  20.     int **board = buildboard(size);
  21.  
  22.     pizurNekudot(board, size);
  23.     print(board, size, 0);
  24.     ongoing(board, size);
  25.     system("PAUSE");
  26. }
  27.  
  28. int** buildboard(int size)// create 2d array as a board
  29. {
  30.     int ** board = new int*[size];
  31.     for (int i = 0; i < size; i++)
  32.     {
  33.         board[i] = new int[size];
  34.     }
  35.     for (int i = 0; i < size; i++)
  36.     {
  37.         for (int g = 0; g < size; g++)
  38.         {
  39.             board[i][g] = 0;
  40.         }
  41.     }
  42.     return board;
  43. }
  44.  
  45. void pizurNekudot(int **board, int size)// randomly select 25% of the board
  46. {
  47.     int randomRow, randomCol;
  48.  
  49.     for (int i = 0; i < (size*size) / 4; i++)
  50.     {
  51.         int randomRow = rand() % size;
  52.         int randomCol = rand() % size;
  53.         if (board[randomRow][randomCol] == 1)
  54.         {
  55.             i--;
  56.         }
  57.         board[randomRow][randomCol] = 1;
  58.     }
  59. }
  60.  
  61. void print(int **arr, int size, int generation) // print the array & generation
  62. {
  63.     cout << "generation" << generation << endl;
  64.     for (int i = 0; i < size; i++)
  65.     {
  66.         for (int g = 0; g < size; g++)
  67.         {
  68.             cout << arr[i][g] << " ";
  69.         }
  70.         cout << endl;
  71.     }
  72.     cout << endl << endl << endl;
  73. }
  74.  
  75. bool rules(int **board, int size, int rowkey, int colkey, bool alive)//get an element and return if he need to be alive or dead by the rules of the game
  76. {
  77.     int alives;
  78.  
  79.     //only if the element is corner
  80.     if (rowkey == size-1 && colkey == 0)
  81.     {
  82.         alives = board[rowkey - 1][colkey] + board[rowkey - 1][colkey + 1] + board[rowkey][colkey + 1];
  83.     }
  84.     if (rowkey == 0 && colkey == size-1)
  85.     {
  86.         alives = board[rowkey + 1][colkey] + board[rowkey + 1][colkey - 1] + board[rowkey + 1][colkey];
  87.     }
  88.     if (rowkey == 0 && colkey == 0)
  89.     {
  90.         alives = board[rowkey + 1][colkey] + board[rowkey + 1][colkey + 1] + board[rowkey][colkey + 1];
  91.     }
  92.     if (rowkey == size-1 && colkey == size-1)
  93.     {
  94.         alives = board[rowkey - 1][colkey] + board[rowkey - 1][colkey - 1] + board[rowkey][colkey - 1];
  95.     }
  96.  
  97.     // only if the element is edge
  98.     if(rowkey == size -1 && (colkey!= 0 && colkey != size-1))
  99.     {
  100.         alives = board[rowkey - 1][colkey] + board[rowkey - 1][colkey - 1] + board[rowkey][colkey + 1] + board[rowkey - 1][colkey + 1] + board[rowkey][colkey - 1];
  101.     }
  102.     if (rowkey == 0 && (colkey != 0 && colkey != size - 1))
  103.     {
  104.         alives = board[rowkey + 1][colkey + 1] + board[rowkey + 1][colkey] + board[rowkey + 1][colkey - 1] + board[rowkey][colkey - 1] + board[rowkey][colkey + 1];
  105.     }
  106.     if (colkey == size-1 && (rowkey != 0 && rowkey != size-1))
  107.     {
  108.         alives = board[rowkey + 1][colkey] + board[rowkey][colkey - 1] + board[rowkey + 1][colkey - 1] + board[rowkey - 1][colkey] + board[rowkey - 1][colkey - 1];
  109.     }
  110.     if (colkey == 0 && (rowkey != 0 && rowkey != size - 1))
  111.     {
  112.         alives = board[rowkey - 1][colkey + 1] + board[rowkey][colkey + 1] + board[rowkey + 1][colkey + 1] + board[rowkey - 1][colkey] + board[rowkey + 1][colkey];
  113.     }
  114.  
  115.     //only if the element isnt edge & corner
  116.     if (colkey != 0 && colkey != size-1 && rowkey != 0 && rowkey != size - 1)
  117.     {
  118.         alives = board[rowkey - 1][colkey + 1] + board[rowkey][colkey + 1] + board[rowkey + 1][colkey + 1] + board[rowkey - 1][colkey] + board[rowkey + 1][colkey] +
  119.             board[rowkey - 1][colkey - 1] + board[rowkey][colkey - 1] + board[rowkey + 1][colkey - 1];
  120.     }
  121.  
  122.     if (alive)
  123.     {
  124.         if (alives >= 4)
  125.             return false;
  126.         if (alives <= 1)
  127.             return false;
  128.         return true;
  129.     }
  130.     else
  131.     {
  132.         if (alives == 3)
  133.             return true;
  134.         return false;
  135.     }
  136.  
  137. }
  138.  
  139. void swaparr(int **board, int **temp, int size) // copy the temp array to the board array
  140. {
  141.     for (int i = 0; i < size; i++)
  142.     {
  143.         for (int g = 0; g < size; g++)
  144.         {
  145.             board[i][g] = temp[i][g];
  146.         }
  147.     }
  148. }
  149.  
  150. bool alldead(int **board, int size) //returns true if all of the cells are dead
  151. {
  152.     for (int i = 0; i < size; i++)
  153.     {
  154.         for (int g = 0; g < size; g++)
  155.         {
  156.             if (board[i][g] == 1)
  157.                 return false;
  158.         }
  159.     }
  160.     return true;
  161. }
  162.  
  163. void ongoing(int **board, int size) //runs as soon as the game starts
  164. {
  165.     int generation = 1;
  166.     bool all_dead = false;
  167.     int **temp = new int *[size];
  168.     for (int i = 0; i < size; i++)
  169.     {
  170.         temp[i] = new int[size];
  171.     }
  172.  
  173.     for (int i = 0; i < size; i++)
  174.     {
  175.         for (int g = 0; g < size; g++)
  176.         {
  177.             temp[i][g] = board[i][g];
  178.         }
  179.     }
  180.  
  181.     while (generation <= 50 && !all_dead)
  182.     {
  183.         for (int i = 0; i < size; i++)
  184.         {
  185.             for (int g = 0; g < size; g++)
  186.             {
  187.                 if (rules(board,size,i,g,board[i][g] == 1))
  188.                 {
  189.                     temp[i][g] = 1;
  190.                 }
  191.                 else
  192.                 {
  193.                     temp[i][g] = 0;
  194.                 }
  195.             }
  196.         }
  197.         swaparr(board, temp, size);
  198.         print(board, size, generation);
  199.         all_dead = alldead(board, size);
  200.         generation++;
  201.     }
  202.  
  203.     if (!all_dead)
  204.     {
  205.         cout << "HAPPY WORLD" << endl;
  206.     }
  207.     else
  208.     {
  209.         cout << "SAD WORLD" << endl;
  210.     }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement