Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib>
- #include <iostream>
- #include "Bitmap.h"
- #include <string>
- #include <cstring>
- using namespace std;
- const int zeile = 32;
- const int spalte = 32;
- void output(int (&arr)[zeile][spalte])
- {
- cout << "|";
- for (int x = 0; x < spalte; x++)
- {
- cout << "__";
- }
- cout << "|" << endl;
- for (int i = 0; i < zeile; i++)
- {
- cout << "|";
- for (int j = 0; j < spalte; j++)
- {
- if (arr[i][j] == 0)
- {
- cout << " ";
- }
- else
- {
- cout << "()";
- }
- if (j == (spalte - 1))
- {
- cout << "|" << endl;
- }
- }
- }
- cout << "|";
- for (int x = 0; x < spalte; x++)
- cout << "__";
- cout << "|" << endl;
- }
- void createImage(int (&arr)[zeile][spalte], int nummer)
- {
- //Einfaches Farbbild mit ein paar Testpixeln
- RgbImage rgb(zeile, spalte);
- rgb.fill(RgbPixel(255, 255, 255));
- for (int i = 0; i < spalte; i++)
- {
- for (int j = 0; j < zeile; j++)
- {
- if (arr[j][i] == 1)
- {
- rgb.setPixel(i, j, COLOR::Blue);
- }
- }
- }
- string xString(to_string(nummer));
- string aPrefix("files/");
- string bPrefix("files/scaled20/");
- string endung(".bmp");
- string a;
- a = aPrefix + xString + endung;
- string b;
- b = bPrefix + xString + endung;
- //Als Datei speichern
- rgb.store(a);
- //Skalierte Version erstellen und speichern
- RgbImage tmp = rgb.scale(20);
- tmp.store(b);
- //Alternative: Anonymes Objekt
- //rgb.scale(10).store(c);
- }
- int neighbours(int (&arr)[zeile][spalte], int row, int col)
- {
- int living = 0;
- if (arr[(row - 1)][(col - 1)] == 1)living++;
- if (arr[(row - 1)][(col + 1)] == 1)living++;
- if (arr[(row + 1)][(col - 1)] == 1)living++;
- if (arr[(row + 1)][(col + 1)] == 1)living++;
- if (arr[(row - 1)][col] == 1)living++;
- if (arr[(row + 1)][col] == 1)living++;
- if (arr[row][(col - 1)] == 1)living++;
- if (arr[row][(col + 1)] == 1)living++;
- return living;
- }
- //Regel 1: Tote Zelle mit genau 3 lebenden Nachbarn erwacht wieder zum Leben
- bool ruleOne(int (&arr)[zeile][spalte], int row, int col)
- {
- int living = neighbours(arr, row, col);
- if (living == 3)
- return true;
- return false;
- }
- //Regel 2: Lebende Zelle mit weniger als 2 Nachbarn stirbt
- /*bool ruleTwo(int (&arr)[zeile][spalte], int row, int col)
- {
- int living = neighbours(arr, row, col);
- if (living < 2)
- {
- return true;
- }
- return false;
- }*/
- //Regel 3: Eine LEBENDE Zelle mit 2 oder 3 Nachbarn bleibt am leben
- bool ruleThree(int (&arr)[zeile][spalte], int row, int col)
- {
- int living = neighbours(arr, row, col);
- if (living == 2 || living == 3)
- {
- return true;
- }
- return false;
- }
- //Regel 4: Lebende Zelle mit mehr als 3 Nachbarn stirbt
- /*bool ruleFour(int (&arr)[zeile][spalte], int row, int col)
- {
- int living = neighbours(arr, row, col);
- if (living > 3)
- {
- return true;
- }
- return false;
- }*/
- int main()
- {
- int spielfeld[zeile][spalte];
- int gamefield[zeile][spalte];
- for (int i = 0; i < zeile; i++)
- {
- for (int j = 0; j < spalte; j++)
- {
- spielfeld[i][j] = 0;
- gamefield[i][j] = 0;
- }
- }
- std::string board = "";
- board += " ";
- board += " ## ## ";
- board += " # # ## ";
- board += " ";
- board += " # # ";
- board += " ";
- board += " # # ";
- board += " ## ";
- board += " ";
- board += " ";
- board += " # # ";
- board += " # ";
- board += " # # ";
- board += " #### ";
- board += " ";
- board += " ";
- board += " ";
- board += " ";
- board += " # ";
- board += " # ";
- board += " ### ";
- board += " ";
- board += " ";
- board += " ";
- board += " ";
- board += " ";
- board += " ## # ";
- board += " ## ## # ## ### ";
- board += " # # #### ## # ";
- board += " # # # # ## ";
- board += " ## ## ";
- board += " ";
- char boardFeld[zeile * spalte];
- strcpy(boardFeld, board.c_str());
- int zaehler = 0;
- int schritte = 0;
- for (int j = 0; j < zeile * spalte; j++)
- {
- if (j != 0 && schritte % spalte == 0)
- {
- zaehler++;
- }
- if (boardFeld[j] == '#')
- {
- spielfeld[zaehler][j % spalte] = 1;
- }
- schritte++;
- }
- int durchgaenge = 0;
- while (durchgaenge < 201)
- {
- cout << "\n\n";
- if (durchgaenge % 2 == 0)
- {
- for (int i = 1; i < zeile - 1; i++)
- {
- for (int j = 1; j < spalte - 1; j++)
- {
- //Falls der Pixel tot ist
- if (spielfeld[i][j] == 0)
- {
- //Falls Regel 1 zutrifft
- if (ruleOne(spielfeld, i, j))
- {
- gamefield[i][j] = 1;
- }
- else
- {
- gamefield[i][j] = 0;
- }
- }
- else
- {
- if (ruleThree(spielfeld, i, j))
- {
- gamefield[i][j] = 1;
- }
- else
- {
- gamefield[i][j] = 0;
- }
- }
- }
- }
- createImage(spielfeld, durchgaenge);
- output(spielfeld);
- }
- else
- {
- for (int i = 1; i < zeile - 1; i++)
- {
- for (int j = 1; j < spalte - 1; j++)
- {
- if (gamefield[i][j] == 0)
- {
- if (ruleOne(gamefield, i, j))
- {
- spielfeld[i][j] = 1;
- }
- else
- {
- spielfeld[i][j] = 0;
- }
- }
- else
- {
- if (ruleThree(gamefield, i, j))
- {
- spielfeld[i][j] = 1;
- }
- else
- {
- spielfeld[i][j] = 0;
- }
- }
- }
- }
- createImage(gamefield, durchgaenge);
- output(gamefield);
- }
- durchgaenge++;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement