Advertisement
Necromide

P6 main.cpp

Jan 23rd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.91 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include "Bitmap.h"
  4. #include <string>
  5. #include <cstring>
  6.  
  7. using namespace std;
  8.  
  9. const int zeile = 32;
  10. const int spalte = 32;
  11.  
  12. void output(int (&arr)[zeile][spalte])
  13. {
  14.     cout << "|";
  15.  
  16.     for (int x = 0; x < spalte; x++)
  17.     {
  18.  
  19.         cout << "__";
  20.  
  21.     }
  22.  
  23.     cout << "|" << endl;
  24.  
  25.     for (int i = 0; i < zeile; i++)
  26.     {
  27.  
  28.         cout << "|";
  29.  
  30.         for (int j = 0; j < spalte; j++)
  31.         {
  32.  
  33.             if (arr[i][j] == 0)
  34.             {
  35.  
  36.                 cout << "  ";
  37.  
  38.             }
  39.             else
  40.             {
  41.  
  42.                 cout << "()";
  43.  
  44.             }
  45.  
  46.             if (j == (spalte - 1))
  47.             {
  48.  
  49.                 cout << "|" << endl;
  50.  
  51.             }
  52.         }
  53.     }
  54.     cout << "|";
  55.  
  56.     for (int x = 0; x < spalte; x++)
  57.  
  58.         cout << "__";
  59.  
  60.     cout << "|" << endl;
  61. }
  62.  
  63. void createImage(int (&arr)[zeile][spalte], int nummer)
  64. {
  65.  
  66.     //Einfaches Farbbild mit ein paar Testpixeln
  67.     RgbImage rgb(zeile, spalte);
  68.  
  69.     rgb.fill(RgbPixel(255, 255, 255));
  70.  
  71.     for (int i = 0; i < spalte; i++)
  72.     {
  73.  
  74.         for (int j = 0; j < zeile; j++)
  75.         {
  76.  
  77.             if (arr[j][i] == 1)
  78.             {
  79.  
  80.                 rgb.setPixel(i, j, COLOR::Blue);
  81.  
  82.             }
  83.         }
  84.     }
  85.  
  86.     string xString(to_string(nummer));
  87.  
  88.     string aPrefix("files/");
  89.     string bPrefix("files/scaled20/");
  90.  
  91.     string endung(".bmp");
  92.  
  93.     string a;
  94.     a = aPrefix + xString + endung;
  95.  
  96.     string b;
  97.     b = bPrefix + xString + endung;
  98.     //Als Datei speichern
  99.     rgb.store(a);
  100.  
  101.     //Skalierte Version erstellen und speichern
  102.     RgbImage tmp = rgb.scale(20);
  103.  
  104.     tmp.store(b);
  105.  
  106.     //Alternative: Anonymes Objekt
  107.     //rgb.scale(10).store(c);
  108. }
  109.  
  110. int neighbours(int (&arr)[zeile][spalte], int row, int col)
  111. {
  112.  
  113.     int living = 0;
  114.  
  115.     if (arr[(row - 1)][(col - 1)] == 1)living++;
  116.  
  117.     if (arr[(row - 1)][(col + 1)] == 1)living++;
  118.  
  119.     if (arr[(row + 1)][(col - 1)] == 1)living++;
  120.  
  121.     if (arr[(row + 1)][(col + 1)] == 1)living++;
  122.  
  123.     if (arr[(row - 1)][col] == 1)living++;
  124.  
  125.     if (arr[(row + 1)][col] == 1)living++;
  126.  
  127.     if (arr[row][(col - 1)] == 1)living++;
  128.  
  129.     if (arr[row][(col + 1)] == 1)living++;
  130.  
  131.     return living;
  132. }
  133. //Regel 1: Tote Zelle mit genau 3 lebenden Nachbarn erwacht wieder zum Leben
  134.  
  135. bool ruleOne(int (&arr)[zeile][spalte], int row, int col)
  136. {
  137.  
  138.     int living = neighbours(arr, row, col);
  139.  
  140.     if (living == 3)
  141.  
  142.         return true;
  143.  
  144.     return false;
  145.  
  146. }
  147. //Regel 2: Lebende Zelle mit weniger als 2 Nachbarn stirbt
  148.  
  149. /*bool ruleTwo(int (&arr)[zeile][spalte], int row, int col)
  150. {
  151.  
  152.     int living = neighbours(arr, row, col);
  153.  
  154.     if (living < 2)
  155.     {
  156.         return true;
  157.     }
  158.     return false;
  159. }*/
  160.  
  161. //Regel 3: Eine LEBENDE Zelle mit 2 oder 3 Nachbarn bleibt am leben
  162.  
  163. bool ruleThree(int (&arr)[zeile][spalte], int row, int col)
  164. {
  165.  
  166.     int living = neighbours(arr, row, col);
  167.  
  168.     if (living == 2 || living == 3)
  169.     {
  170.  
  171.         return true;
  172.     }
  173.  
  174.     return false;
  175. }
  176. //Regel 4: Lebende Zelle mit mehr als 3 Nachbarn stirbt
  177.  
  178. /*bool ruleFour(int (&arr)[zeile][spalte], int row, int col)
  179. {
  180.  
  181.     int living = neighbours(arr, row, col);
  182.  
  183.     if (living > 3)
  184.     {
  185.         return true;
  186.     }
  187.     return false;
  188. }*/
  189.  
  190. int main()
  191. {
  192.  
  193.     int spielfeld[zeile][spalte];
  194.  
  195.     int gamefield[zeile][spalte];
  196.  
  197.     for (int i = 0; i < zeile; i++)
  198.     {
  199.  
  200.         for (int j = 0; j < spalte; j++)
  201.         {
  202.  
  203.             spielfeld[i][j] = 0;
  204.  
  205.             gamefield[i][j] = 0;
  206.  
  207.         }
  208.     }
  209.  
  210.  
  211.     std::string board = "";
  212.     board += "                                ";
  213.     board += " ##                          ## ";
  214.     board += " # #                         ## ";
  215.     board += "                                ";
  216.     board += "   # #                          ";
  217.     board += "                                ";
  218.     board += "     # #                        ";
  219.     board += "      ##                        ";
  220.     board += "                                ";
  221.     board += "                                ";
  222.     board += "                           #  # ";
  223.     board += "                          #     ";
  224.     board += "                          #   # ";
  225.     board += "                          ####  ";
  226.     board += "                                ";
  227.     board += "                                ";
  228.     board += "                                ";
  229.     board += "                                ";
  230.     board += "  #                             ";
  231.     board += "   #                            ";
  232.     board += " ###                            ";
  233.     board += "                                ";
  234.     board += "                                ";
  235.     board += "                                ";
  236.     board += "                                ";
  237.     board += "                                ";
  238.     board += "                      ## #      ";
  239.     board += " ##                ## # ## ###  ";
  240.     board += " # #            ####  ##      # ";
  241.     board += "   #           #    #   #   ##  ";
  242.     board += "   ##           ##              ";
  243.     board += "                                ";
  244.  
  245.     char boardFeld[zeile * spalte];
  246.     strcpy(boardFeld, board.c_str());
  247.  
  248.     int zaehler = 0;
  249.     int schritte = 0;
  250.  
  251.     for (int j = 0; j < zeile * spalte; j++)
  252.     {
  253.         if (j != 0 && schritte % spalte == 0)
  254.         {
  255.  
  256.             zaehler++;
  257.  
  258.         }
  259.  
  260.         if (boardFeld[j] == '#')
  261.         {
  262.  
  263.             spielfeld[zaehler][j % spalte] = 1;
  264.  
  265.         }
  266.  
  267.         schritte++;
  268.  
  269.     }
  270.  
  271.     int durchgaenge = 0;
  272.  
  273.     while (durchgaenge < 201)
  274.     {
  275.  
  276.         cout << "\n\n";
  277.  
  278.         if (durchgaenge % 2 == 0)
  279.         {
  280.  
  281.             for (int i = 1; i < zeile - 1; i++)
  282.             {
  283.  
  284.                 for (int j = 1; j < spalte - 1; j++)
  285.                 {
  286.                     //Falls der Pixel tot ist
  287.                     if (spielfeld[i][j] == 0)
  288.                     {
  289.                         //Falls Regel 1 zutrifft
  290.                         if (ruleOne(spielfeld, i, j))
  291.                         {
  292.  
  293.                             gamefield[i][j] = 1;
  294.  
  295.                         }
  296.  
  297.                         else
  298.                         {
  299.  
  300.                             gamefield[i][j] = 0;
  301.  
  302.                         }
  303.                     }
  304.                     else
  305.                     {
  306.  
  307.                         if (ruleThree(spielfeld, i, j))
  308.                         {
  309.  
  310.                             gamefield[i][j] = 1;
  311.  
  312.                         }
  313.                         else
  314.                         {
  315.  
  316.                             gamefield[i][j] = 0;
  317.  
  318.                         }
  319.                     }
  320.                 }
  321.             }
  322.             createImage(spielfeld, durchgaenge);
  323.             output(spielfeld);
  324.         }
  325.         else
  326.         {
  327.  
  328.             for (int i = 1; i < zeile - 1; i++)
  329.             {
  330.  
  331.                 for (int j = 1; j < spalte - 1; j++)
  332.                 {
  333.  
  334.                     if (gamefield[i][j] == 0)
  335.                     {
  336.  
  337.                         if (ruleOne(gamefield, i, j))
  338.                         {
  339.  
  340.                             spielfeld[i][j] = 1;
  341.  
  342.                         }
  343.                         else
  344.                         {
  345.  
  346.                             spielfeld[i][j] = 0;
  347.  
  348.                         }
  349.                     }
  350.                     else
  351.                     {
  352.  
  353.                         if (ruleThree(gamefield, i, j))
  354.                         {
  355.  
  356.                             spielfeld[i][j] = 1;
  357.  
  358.                         }
  359.                         else
  360.                         {
  361.  
  362.                             spielfeld[i][j] = 0;
  363.  
  364.                         }
  365.                     }
  366.                 }
  367.             }
  368.             createImage(gamefield, durchgaenge);
  369.             output(gamefield);
  370.         }
  371.         durchgaenge++;
  372.     }
  373.     return 0;
  374. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement