Advertisement
Lauda

gol.cpp

Apr 16th, 2013
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.07 KB | None | 0 0
  1. #include "GameOfLife.h"
  2.  
  3. #include <iostream>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <time.h>
  7. #include <cilk\cilk.h>
  8.  
  9. using namespace std;
  10.  
  11. GameOfLife::GameOfLife(unsigned int height, unsigned int width, InitModel model)
  12. {
  13.     m_height = height;
  14.     m_width = width;
  15.  
  16.     m_buffer = new unsigned char [m_width * m_height];
  17.     m_nextIter = new unsigned char [m_width * m_height];
  18.  
  19.     // fill buffer
  20.     switch (model)
  21.     {
  22.     case PULSAR:
  23.         {
  24.             if (width > 15 && height > 15)
  25.             {
  26.                 memset(m_buffer, 0x0, m_width * m_height * sizeof(unsigned char));
  27.                 m_buffer[2 * m_width + 4] = 1;
  28.                 m_buffer[2 * m_width + 5] = 1;
  29.                 m_buffer[2 * m_width + 6] = 1;
  30.  
  31.                 m_buffer[2 * m_width + 10] = 1;
  32.                 m_buffer[2 * m_width + 11] = 1;
  33.                 m_buffer[2 * m_width + 12] = 1;
  34.  
  35.                 m_buffer[4 * m_width + 2] = 1;
  36.                 m_buffer[5 * m_width + 2] = 1;
  37.                 m_buffer[6 * m_width + 2] = 1;
  38.  
  39.                 m_buffer[4 * m_width + 7] = 1;
  40.                 m_buffer[5 * m_width + 7] = 1;
  41.                 m_buffer[6 * m_width + 7] = 1;
  42.  
  43.                 m_buffer[4 * m_width + 9] = 1;
  44.                 m_buffer[5 * m_width + 9] = 1;
  45.                 m_buffer[6 * m_width + 9] = 1;
  46.  
  47.                 m_buffer[4 * m_width + 14] = 1;
  48.                 m_buffer[5 * m_width + 14] = 1;
  49.                 m_buffer[6 * m_width + 14] = 1;
  50.  
  51.                 m_buffer[7 * m_width + 4] = 1;
  52.                 m_buffer[7 * m_width + 5] = 1;
  53.                 m_buffer[7 * m_width + 6] = 1;
  54.  
  55.                 m_buffer[7 * m_width + 10] = 1;
  56.                 m_buffer[7 * m_width + 11] = 1;
  57.                 m_buffer[7 * m_width + 12] = 1;
  58.  
  59.                 m_buffer[9 * m_width + 4] = 1;
  60.                 m_buffer[9 * m_width + 5] = 1;
  61.                 m_buffer[9 * m_width + 6] = 1;
  62.  
  63.                 m_buffer[9 * m_width + 10] = 1;
  64.                 m_buffer[9 * m_width + 11] = 1;
  65.                 m_buffer[9 * m_width + 12] = 1;
  66.  
  67.                 m_buffer[10 * m_width + 2] = 1;
  68.                 m_buffer[11 * m_width + 2] = 1;
  69.                 m_buffer[12 * m_width + 2] = 1;
  70.  
  71.                 m_buffer[10 * m_width + 7] = 1;
  72.                 m_buffer[11 * m_width + 7] = 1;
  73.                 m_buffer[12 * m_width + 7] = 1;
  74.  
  75.                 m_buffer[10 * m_width + 9] = 1;
  76.                 m_buffer[11 * m_width + 9] = 1;
  77.                 m_buffer[12 * m_width + 9] = 1;
  78.  
  79.                 m_buffer[10 * m_width + 14] = 1;
  80.                 m_buffer[11 * m_width + 14] = 1;
  81.                 m_buffer[12 * m_width + 14] = 1;
  82.  
  83.                 m_buffer[14 * m_width + 4] = 1;
  84.                 m_buffer[14 * m_width + 5] = 1;
  85.                 m_buffer[14 * m_width + 6] = 1;
  86.  
  87.                 m_buffer[14 * m_width + 10] = 1;
  88.                 m_buffer[14 * m_width + 11] = 1;
  89.                 m_buffer[14 * m_width + 12] = 1;
  90.  
  91.                 break;
  92.             }
  93.             else
  94.             {
  95.                 cout << "Board too small, using RANDOM." << endl;
  96.             }
  97.         }
  98.  
  99.     case RANDOM:
  100.         {
  101.             srand(time(NULL));
  102.             for (unsigned int i = 0; i < m_height; i++)
  103.             {
  104.                 for (unsigned int j = 0; j < m_width; j++)
  105.                 {
  106.                     m_buffer[i * m_width + j] = rand() % 2;
  107.                 }
  108.             }
  109.             break;
  110.         }
  111.  
  112.     default:
  113.         break;
  114.     }
  115. }
  116.  
  117.  
  118. void GameOfLife::printIteration()
  119. {
  120.     for (int row = 0; row < m_height; row++)
  121.     {
  122.         for (int col = 0; col < m_width; col++)
  123.         {
  124.             char c;
  125.  
  126.             if (m_buffer[row * m_width + col] == 1)
  127.                 c = 0xb2;
  128.             else
  129.                 c = 0x20;
  130.  
  131.             cout << c << " ";
  132.         }
  133.         cout << "\n";
  134.     }
  135. }
  136.  
  137.  
  138. int GameOfLife::getNeighbourSum(unsigned int row, unsigned int col)
  139. {
  140.     int sum = 0;
  141.  
  142.     // TODO: PLACE CODE HERE
  143. // provjeravamo da li pozicija koju ocitavamo izalazi iz matrice, odnosno da li se njena adresa nalazi posle prvog clana i prije poslednjeg clana niza
  144. // i vrsimo sumiranje
  145.     for (int i=0; i<=2; i++)
  146.         for (int j=0; j<=2; j++)
  147.             if ((((row+i) * m_width + (col+j)) >=0) && (((row+i) * m_width + (col+j)) <= m_width*m_height))
  148.                 sum += m_buffer[(row+i)*m_width+(col+j)];
  149.  
  150.         sum -= m_buffer[row*m_width+col];
  151.     return sum;
  152. }
  153.  
  154.  
  155. void GameOfLife::nextIterSerial()
  156. {
  157.     for (int row = 0; row < m_height; row++)
  158.     {
  159.         for (int col = 0; col < m_width; col++)
  160.         {
  161.             // TODO: IMPLEMENT getNeighbourSum (done)
  162.             int neighbours = getNeighbourSum(row, col);
  163.             if (m_buffer[row * m_width + col] == 1)
  164.             {
  165.                 if ((neighbours == 2) || (neighbours == 3))
  166.                 {
  167.                     m_nextIter[row * m_width + col] = 1;
  168.                 }
  169.                 else
  170.                 {
  171.                     m_nextIter[row * m_width + col] = 0;
  172.                 }
  173.             }
  174.             else
  175.             {
  176.                 if (neighbours == 3)
  177.                 {
  178.                     m_nextIter[row * m_width + col] = 1;
  179.                 }
  180.                 else
  181.                 {
  182.                     m_nextIter[row * m_width + col] = 0;
  183.                 }
  184.             }
  185.         }
  186.     }
  187.  
  188.     // swtich buffers
  189.     unsigned char* temp = m_buffer;
  190.     m_buffer = m_nextIter;
  191.     m_nextIter = temp;
  192. }
  193.  
  194. void GameOfLife::tempSpawn(uint32 row)
  195. {
  196.     for (int col = 0; col < m_width; col++)
  197.         {
  198.             int neighbours = getNeighbourSum(row, col);
  199.             if (m_buffer[row * m_width + col] == 1)
  200.             {
  201.                 if ((neighbours == 2) || (neighbours == 3))
  202.                 {
  203.                     m_nextIter[row * m_width + col] = 1;
  204.                 }
  205.                 else
  206.                 {
  207.                     m_nextIter[row * m_width + col] = 0;
  208.                 }
  209.             }
  210.             else
  211.             {
  212.                 if (neighbours == 3)
  213.                 {
  214.                     m_nextIter[row * m_width + col] = 1;
  215.                 }
  216.                 else
  217.                 {
  218.                     m_nextIter[row * m_width + col] = 0;
  219.                 }
  220.             }
  221.         }
  222. }
  223.  
  224. void GameOfLife::nextIterParallel()
  225. {
  226.     // TODO: PLACE CODE HERE
  227.  
  228.     cilk_for (int row = 0; row < m_height; row++)
  229.     {
  230.         tempSpawn(row);
  231.     }
  232.  
  233.     // swtich buffers
  234.     unsigned char* temp = m_buffer;
  235.     m_buffer = m_nextIter;
  236.     m_nextIter = temp;
  237. }
  238.  
  239.  
  240. GameOfLife::~GameOfLife()
  241. {
  242.     delete [] m_buffer;
  243.     delete [] m_nextIter;
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement