mercredi 31 janvier 2018

Somewhere my if statements mess up, and my GSA's cant gelp me

So, this is supposed to go through a file of 0's, ';'s and 1;s (all in character form originally but i set to integers), however when i run it the code counts the neighboring living cells wrong, pulling 3's in 5 spots where there should be a 2 for my neighbor count.

Current output; Neighbor square for how many neighbors each cell has (counting all 8 surrounding tiles), then the updated matrix after basic game of life operations happen, lastly the original Matrix This is the original matrix below

0;0;0;0;0;0

0;0;0;1;0;0

0;1;0;1;0;0

0;0;1;1;0;0

0;0;0;0;0;0

0;0;0;0;0;0

The rest of the process happens flawlessly, but the counting of neighbors doesn't work. What do i do wrong?

#include <stdio.h>
#include <math.h>
#include <stdlib.h>  
void manage_board(FILE *input, int counter, int iterate);
int main (int argc, const char * argv[]){
    FILE *input = fopen(argv[1],"r");
    int counter = 0,temp1=0,temp2=0;
    char temp = fgetc(input);
    while (temp != EOF){
        if (temp == '1' || temp == '0'){
        counter+=1;
        }
        temp = fgetc(input) ;
    }
    counter = sqrt(counter);
    rewind(input);
    manage_board(input,counter, atoi(argv[2]));
    fclose(input);
    return 0;
}
void manage_board(FILE *input, int counter, int iterate){
    int matrix[counter][counter];
    FILE *output = fopen("output.csv","w");
    int original[counter][counter];
    int updated[counter][counter];
    int rows = 0,cols = 0, neighbors =0;
    char temp = fgetc(input);
    /*for (int i=0; i<counter; i++){
        for (int j=0; j<counter; j++){
            updated[i][j] = 0; 
        }   
        printf ("\n");
    }*/
    while (temp != EOF){
        //printf("%c",temp);
        if (temp == '1' && temp != ';'){
            matrix[cols][rows] = 1;
            rows++;
        }
        else if (temp == '0' && temp != ';'){
            matrix[cols][rows] = 0;
            rows++;
        }
        if (rows==6){
            cols++;
            rows=0;
        }       
        temp = fgetc(input);
    }   
    for (int i=0; i<counter; i++){
        for (int j=0; j<counter; j++){
            updated[i][j] = 0;          
        }   
        //printf ("\n");
    }
    for (int i=0; i<counter; i++){
        for (int j=0; j<counter; j++){
            original[i][j] =matrix[i][j]; 
        }   
        //printf ("\n");
    }   
    for (int loops = 0; loops < iterate;loops++){
        for (int i=0; i<counter; i++){
            for (int j=0; j<counter; j++){              
                if ( i !=counter-1){ //down one
                    if(matrix[i+1][j] == 1){
                        neighbors ++;
                    }
                }
                if ( i !=0){//up one
                    if(matrix[i-1][j] == 1){
                        neighbors++;
                    }
                }
                if ( j != counter-1){//right one
                    if (matrix[i][j+1] == 1){
                    neighbors++;
                    }
                }
                if (j !=0 ){//left one
                    if (matrix[i][j-1] == 1 ){
                        neighbors++;
                    }
                }
                if (i !=counter-1 && j != counter-1){//bot right
                    if(matrix[i+1][j+1] == 1)
                        neighbors++;
                }
                if (j != counter-1 && i !=0 ){//top right
                    if(matrix[i-1][j+1] == 1){
                        neighbors++;
                    }
                }
                if (j !=0 && i !=0){//top left
                    if (matrix[i-1][j-1] == 1){
                        neighbors++;
                    }
                }
                if (i !=counter-1 && j !=0 ){//bottom left
                    if (matrix[i+1][j-1] == 1){
                        neighbors++;
                    }
                }
                ///printf("%d", matrix[i][j]);
                //printf ("%d ",neighbors);
                if (neighbors < 2 ){
                    updated[i][j]=0;
                }
                else if (neighbors == 2 && matrix[i][j] == 1)
                    updated[i][j]=1;
                else if (neighbors > 3){
                    updated[i][j]=0;
                }
                else if (neighbors = 3){
                    updated[i][j]=1;
                }
                printf("%d ",neighbors);
                neighbors = 0;
            }
            printf("\n");
        }   
        for (int i=0; i<counter; i++){
            for (int j=0; j<counter; j++){
                matrix[i][j]= updated[i][j];
                printf("%d",updated[i][j]);
                updated[i][j] = 0;
            }
            printf("\n");
        }
    }
        printf("original board\n");
        for (int i=0; i<counter; i++){
            for (int j=0; j<counter; j++){
                printf("%d", original[i][j]);
            }
            printf ("\n");
        }       
        //printf(" \nnew matrix\n\n");
/*      for (int i=0; i<counter; i++){
            for (int j=0; j<counter; j++){
                    if (updated[i][j] == 1){
                        fprintf( output, "%d %d \n", i, j);
                        //printf(updated)
                    }
            }
        }
    */  
/* A live cell with fewer than two live neighbors dies. 
A live cell with more than three live neighbors also dies. 
A live cell with exactly two or three live neighbors lives. 
A dead cell with exactly three live neighbors becomes alive.*/
}

Aucun commentaire:

Enregistrer un commentaire