Tutaj bardziej rozbudowana wersja. Komórka może mieć 7 stanów, a sumowanie następuje z 25 sąsiednich komórek. Niestety w tej wersji trzeba zdefiniować funkcję przejść aż dla 151 stanów. Ale efekt zawsze mnie urzekał, chyba warto :)
Tutaj kod:
#include <QImage>
#include <QTextStream>
#include <QVector>
#include <QColor>
int main() {
QTextStream inp(stdin);
QTextStream out(stdout);
out << "Please input the width of image:" << endl;
int width;
inp >> width;
out << "Please input the height of image:" << endl;
int height;
inp >> height;
out << "Please input the loops of the algorithm:" << endl;
int loops;
inp >> loops;
QVector<int> buff( width * height , 0 );
QVector<int> buffCopy;
out << "Please input the number of init points:" << endl;
int n;
inp >> n;
for( int i=0 ; i<n ; i++ ) {
int x,y,v;
out << "Please input the x of the [" << (i+1) << "] point:" << endl;
inp >> x;
out << "Please input the y of the [" << (i+1) << "] point:" << endl;
inp >> y;
if( x < 0 || x >= width || y < 0 || y >= height || buff[ y * width + x] ) {
out << "Invalid point" << endl;
i--;
continue;
}
out << "Please input value (0, 1, 2, 3, 4, 5, 6):" << endl;
inp >> v;
if( v < 0 || v > 6 ) {
out << "Invalid value" << endl;
i--;
continue;
}
buff[ y * width + x] = v;
}
QVector<int> rules(151,0);
for( int i=0 ; i<rules.size() ; i++ ) {
out << "If sum == " << i << " plelase input 1,2,3,4,5,6 to live or 0 to death: " << endl;
int rule;
inp >> rule;
if( rule < 0 || rule > 6) {
out << "Input 0, 1, 2, 4, 5 or 6" << endl;
i--;
continue;
}
rules[i] = rule;
}
for( int loop=0 ; loop < loops ; loop++ ) {
buffCopy = buff;
for( int y=2 ; y<height-2 ; y++ ) {
for( int x=2 ; x<width-2 ; x++ ) {
int sum = 0;
for( int y2=y-2 ; y2<=y+2 ; y2++ ) {
for( int x2=x-2 ; x2<=x+2 ; x2++ ) {
sum += buffCopy[y2*width+x2];
}
}
buff[y*width+x] = rules[ sum ];
}
}
}
QImage img( width , height , QImage::Format_RGBA8888 );
for( int y=0 ; y<height ; y++ ) {
for( int x=0 ; x<width ; x++ ) {
QColor c;
switch( buff[y*width+x] ) {
case 0: c = QColor(0x00,0x00,0x00); break;
case 1: c = QColor(0xFF,0x00,0x00); break;
case 2: c = QColor(0x00,0xFF,0x00); break;
case 3: c = QColor(0x00,0x00,0xFF); break;
case 4: c = QColor(0xFF,0x00,0xFF); break;
case 5: c = QColor(0x00,0xFF,0xFF); break;
case 6: c = QColor(0xFF,0xFF,0x00); break;
default: abort();
}
img.setPixel(x, y, c.rgb() ) ;
}
}
img.save("out.png");
return 0;
}
Przykładowe wejście programu, można wkleić ctrl+v:
1001
1001
1000
9
500
500
2
500
501
2
500
499
2
501
500
2
499
500
2
499
499
3
499
501
3
501
499
3
501
501
3
0
1
2
3
4
5
6
5
4
3
2
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
4
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
2
2
0
0
3
3
0
0
0
1
1
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
2
3
4
5
6
6
5
4
3
2
1
0
Tutaj efekt: