Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Life.h"
- #include <stdlib.h>
- #include <string.h>
- Matrix newMatrix(unsigned int length){
- Matrix res;
- res.length=length;
- res.data = malloc(sizeof(bool*) * length);
- int i;
- for (i = 0; i < length ; i ++){
- res.data[i] = malloc(sizeof(bool*) * length);
- }
- int j;
- for(i=0; i<length; i++){
- for(j=0; j<length; j++){
- res.data[i][j]=false;
- }
- }
- return res;
- }
- void deleteMatrix(Matrix m){
- int i=0;
- int length=m.length;
- for (i;i<length;i++){
- free(m.data[i]);
- }
- free(m.data);
- }
- char* showMatrix(Matrix m){
- char* res;
- res = malloc(((m.length*(m.length+1))+1)*sizeof(char));
- showMatrixIn(m,res);
- return res;
- }
- void showMatrixIn(Matrix m,char* result){
- int i;
- int j;
- int q=0;
- for(i=0; i<m.length; i++){
- for(j=0; j<=m.length; j++){
- if(j==m.length){
- result[q]='\n';
- q++;
- }
- if(j!=m.length&&m.data[i][j]==true){
- result[q]='*';
- q++;
- }
- if(j<m.length&&m.data[i][j]==false){
- result[q]=' ';
- q++;
- }
- }
- }
- result[q]='\0';
- }
- unsigned int anzahlBelegterNachbarn(Matrix m,unsigned int x, unsigned int y){
- unsigned int result=0;
- unsigned int startX=x-1;
- unsigned int startY=y-1;
- unsigned int stopX= x+1;
- unsigned int stopY=y+1;
- unsigned int i;
- unsigned int j;
- if (x==0) {
- startX=x;
- }
- if (x==m.length-1) {
- stopX=x;
- }
- if (y==0) {
- startY=y;
- }
- if (y==m.length-1) {
- stopY=y;
- }
- for (i=startX; i<=stopX; i++){
- for (j=startY;j<=stopY;j++){
- if (i==x&& j==y){}
- else {
- if (m.data[i][j]) result++;
- }
- }
- }
- return result;
- }
- Matrix zugGOL(Matrix m){
- Matrix res = newMatrix(m.length);
- zugGOLIn(m,res);
- return res;
- }
- void zugGOLIn(Matrix m, Matrix result){
- int i;
- int j;
- for(i=0; i<m.length; i++){
- for(j=0; j<m.length; j++){
- int anzahl = anzahlBelegterNachbarn(m,i,j);
- if(m.data[i][j]==true){
- if(anzahl<=1) result.data[i][j]=false;
- if(anzahl>=4) result.data[i][j]=false;
- if(anzahl==2||anzahl==3) result.data[i][j]=true;
- }else{
- if(anzahl==3) result.data[i][j]=true;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement