Snippets
Created by
Karol Preiskorn
last modified
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | /**
* Brian's Brain
*
* Rules:
* -------
* Each cell has three possible states: passive, active, and semi-active.
* If a cell is active, it goes to semi-active state on the next step
* If a cell is semi-active, it becomes passive on the next step
* If a cell is passive, it becomes active if and only if it has exactly 2 active neighbors
*
* http://meil.pw.edu.pl/za/ZA/Dydaktyka/Informatyka-1
* http://zvold.blogspot.com/2010/01/conways-life-and-brians-brain-cellular.html
**/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "winbgi2.h"
void main() {
printf("Brian's Brain automate \n");
printf("==================-===\n\n");
printf("Rules:\n");
printf("* Each cell has three possible states: passive, active, and semi-active.\n");
printf("* If a cell is active, it goes to semi-active state on the next step\n");
printf("* If a cell is semi-active, it becomes passive on the next step\n");
printf("* If a cell is passive, it becomes active if and only if it has exactly 2 active neighbors\n\n");
const int v_widht=300;
const int v_hight=300;
int v_state=0;
graphics(v_widht, v_hight); // init
// variables
int range_min = 0;
int range_max = 100;
int v_cell_color = 0;
int v_time=200;
// cells coloring
int v_color_active = 130;
int v_color_semi_active = 220;
int v_color_passive = 0;
// matrix coordinates
int x=0;
int y=0;
int t=0;
// matrix init
int matrix[v_widht][v_hight];
int matrix_next_step[v_widht][v_hight];
// generate random enviroment
for(x = 0; x < v_widht; x++) {
for(y = 0; y < v_hight; y++ ){
// warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
v_state = (double)rand() / (RAND_MAX + 1) * (range_max - range_min) + range_min;
if(v_state >= 0 && v_state < 33) {
v_cell_color = v_color_passive; // pasive
}
if(v_state >= 33 && v_state < 66) {
v_cell_color = v_color_active; // active
}
if(v_state >= 66 && v_state <= 100) {
v_cell_color = v_color_semi_active; // semi-active
}
//printf("Put cell (%d, %d) in state %d\n", x, y, v_cell_color);
matrix[x][y] = v_cell_color;
putpixel(x, y, v_cell_color);
}
}
// simulate enviroment t times
for(t = 0; t < v_time; t++) {
for(x = 0; x < v_widht; x++) {
for(y = 0; y < v_hight; y++){
if(matrix[x][y] != v_color_active && matrix[x][y] != v_color_passive && matrix[x][y] != v_color_semi_active) {
printf ("\nColor error! Time %d, Cell (%d, %d) color %d \n", t, x, y, matrix[x][y]);
wait();
exit(1);
}
// If a cell is active, it goes to semi-active state on the next step
if(matrix[x][y] == v_color_active) {
matrix_next_step[x][y] = v_color_semi_active;
}
// If a cell is semi-active, it becomes passive on the next step
if(matrix[x][y] == v_color_semi_active) {
matrix_next_step[x][y] = v_color_passive;
}
// If a cell is passive, it becomes active if and only if it has exactly 2 active neighbors
if(matrix[x][y] == v_color_passive){
int l_2_active = 0;
// vertical and horizontal
if(matrix[x+1][y] == v_color_active) {
l_2_active++;
}
if(matrix[x-1][y] == v_color_active) {
l_2_active++;
}
if(matrix[x][y+1] == v_color_active) {
l_2_active++;
}
if(matrix[x][y-1] == v_color_active) {
l_2_active++;
}
// diagonal
if(matrix[x-1][y+1] == v_color_active) {
l_2_active++;
}
if(matrix[x+1][y+1] == v_color_active) {
l_2_active++;
}
if(matrix[x-1][y-1] == v_color_active) {
l_2_active++;
}
if(matrix[x+1][y-1] == v_color_active) {
l_2_active++;
}
// set active cell
if( l_2_active == 2 ) {
matrix_next_step[x][y] = v_color_active;
} else {
matrix_next_step[x][y] = matrix[x][y];
}
}
// printf("Put cell (%d, %d) in state %d\n", x, y, matrix_next_step[x][y]);
}
}
// print cells - generate enviroment
int l_active_count = 0;
int l_semi_active_count = 0;
int l_passive_count = 0;
for(x = 0; x < v_widht; x++) {
for(y = 0; y < v_hight; y++){
// printf("Put cell (%d, %d) in state %d\n", x, y, v_cell_color);
// rewrite to old array
matrix[x][y] = matrix_next_step[x][y];
// print graph
putpixel(x, y, matrix[x][y]);
if (matrix[x][y] == v_color_active) l_active_count++;
if (matrix[x][y] == v_color_semi_active) l_semi_active_count++;
if (matrix[x][y] == v_color_passive) l_passive_count++;
}
}
printf("End of step %d sum of active %d, semi-active %d, passive %d\n", t, l_active_count, l_semi_active_count, l_passive_count);
}
printf("End of program\n");
wait();
}
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.