#include #include #include //Global variables int matrix[20][20], dynamic_matrix[3][400], n; int ioriginal, joriginal, i2, j2, max_points = 0, score = 0; FILE *output; //Function declarations void pathfinder(int i, int j, int up, int down, int left, int right, int distance, int turns); void erase(int a, int b, int c, int d, int rows, int columns, int score); //Main function int main() { //Variables int rows, columns, different_tiles, i, j, k, p, q, f, g; FILE *input; char filename[13], letter; //First part of program; reads from data.txt, stores it in "matrix" array and closes file printf("\nPlease enter a filename (xxxxxxxx.yyy): "); scanf("%s", filename); //Checking for wrong filename while((input = fopen(filename, "r")) == NULL) { printf("Filename invalid\nEnter a file name or exit to exit: "); scanf("%s", filename); //If filename is exit, then the program exits if(strcmp(filename, "exit") == 0) { printf("\n"); system ("PAUSE"); return -1; } } //Reading #of rows, #of columns and #of different tiles fscanf(input, "%d %d %d", &rows, &columns, &different_tiles); //Fills matrix with ones for(i=0; i < 20; i++) { for(j=0; j < 20; j++) { matrix[i][j] = 1; } } //Fills matrix with zeros for(i=1; i < rows+3; i++) { for(j=1; j < columns+3; j++) { matrix[i][j] = 0; } } //Scans letters from data.txt. scanf("%*[^\n]") is used to jump to next line in data.txt for(i=2; i < rows+2; i++) { scanf("%*[^\n]"); fscanf(input, "%*c"); for(j=2; j < columns+2; j++) { fscanf(input, "%c", &letter); matrix[i][j] = letter; } } //Closing file input fclose(input); printf("\n"); //Printing output with numbers beside matrix without the ones and zeros for(i=2; i< rows+2; i++) { printf("\n\n"); if(rows+2-i > 9) printf("%d ", rows+2-i); else printf("%d ", rows+2-i); for(j=2; j< columns+2; j++) { if(matrix[i][j] == 0) printf(" "); else printf("%c ", matrix[i][j]); } } printf("\n\n "); for(i=0; i< columns; i++) { if(i > 8) printf("%d ",i+1); else printf("%d ", i+1); } printf("\n\n"); printf("Original board"); printf("\n\n"); //Opening and writing beginning of file (&START) in log file if((output = fopen("logfile.txt", "w")) == NULL) return 0; fprintf(output,"&START\n"); //Loop to erase cells XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX for(k=0; k < rows*columns/2 ;k++) { max_points = 0; n = 1; //Filling dynamic_matrix with zeros for(q = 0; q < 400; q++) dynamic_matrix[0][q] = 0; for(q = 0; q < 400; q++) dynamic_matrix[1][q] = 0; for(q = 0; q < 400; q++) dynamic_matrix[2][q] = 0; f = 2+rand()%(rows+1); g = 2+rand()%(columns+1); ioriginal = f; joriginal = g; pathfinder(f, g, 0, 0, 0, 0, 0, 0); /*Checking if two or more entries in the matrix has same ending coordinates and if so, erasing same coordinates, and using the shortest distance*/ for(i=0; i max_points) { max_points = dynamic_matrix[2][p]*dynamic_matrix[2][p]; i2 = dynamic_matrix[0][p]; j2 = dynamic_matrix[1][p]; } } /*If max points is equal to zero, meaning initial coordinates point to a zero then don't erase initial coordiante and decrease k so that one more loop can take place*/ if(max_points > 0) { score += max_points; erase(ioriginal, joriginal, i2, j2, rows, columns, score); printf("Initial coordinates: (%d,%d)\n", joriginal-1, rows+2-ioriginal); printf("End coordinates: (%d,%d)\nTotal points: %d\n", j2-1, rows+2-i2, score); } else k--; } //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX //Writing &END and closing file output fprintf(output,"&END"); fclose(output); //Erase if using Microsoft's compiler system("PAUSE"); return 0; } //Pathfinder function void pathfinder(int i, int j, int up, int down, int left, int right, int distance, int turns) { //Increasing distance; first time function runs distance has to be one distance++; /*If there are more than two turns made or the cell is zero the first time the function runs, stop function*/ if(turns > 2 || matrix[i][j] == 0 && distance == 1) return; //If the cell is the same as the original cell, but not including itself, taly score if(matrix[i][j] == matrix[ioriginal][joriginal] && distance > 1) { dynamic_matrix[0][n] = i; dynamic_matrix[1][n] = j; dynamic_matrix[2][n] = distance; n++; return; } //If the matrix was not the same as original and not zero, then it must be one or itself if(matrix[i][j] != 0 && distance > 1) return; /*(Same explanation for other four arguments) If the cell before was not above the one in this function, then this cell can produce a cell above it. Then if the previous cell was on the right or left of this one, and this cell is going above this one, that means there was a turn.*/ if(down == 0) { if(left > 0 || right > 0) pathfinder(i-1, j, up+1, down, 0, 0, distance, turns+1); else pathfinder(i-1, j, up+1, down, left ,right, distance, turns); } if(left == 0) { if(down > 0 || up > 0) pathfinder(i, j+1, 0, 0, left ,right+1, distance, turns+1); else pathfinder(i, j+1, up, down, left ,right+1, distance, turns); } if(up == 0) { if(left > 0 || right > 0) pathfinder(i+1, j, up, down+1, 0, 0, distance, turns+1); else pathfinder(i+1, j, up, down+1, left ,right, distance, turns); } if(right == 0) { if(down > 0 || up > 0) pathfinder(i, j-1, 0, 0, left+1 ,right, distance, turns+1); else pathfinder(i, j-1, up, down, left+1 ,right, distance, turns); } } //Erase function, erases coordinates, writes in log file and prints changes void erase(int a, int b, int c, int d, int rows, int columns, int score) { int i, j; //Erasing coordinates matrix[a][b] = 0; matrix[c][d] = 0; //Writing coordinates in log file fprintf(output,"%-4d%-4d%-4d%-4d%d\n", rows+2-a, b-1, rows+2-c, d-1, score); //Delays next lines by three seconds //Sleep(3000); //Clears screen system("cls"); //Printing output with numbers beside matrix without the ones and zeros for(i=2; i< rows+2; i++) { printf("\n\n"); if(rows+2-i > 9) printf("%d ", rows+2-i); else printf("%d ", rows+2-i); for(j=2; j< columns+2; j++) { if(matrix[i][j] == 0) printf(" "); else printf("%c ", matrix[i][j]); } } printf("\n\n "); for(i=0; i< columns; i++) { if(i > 8) printf("%d ",i+1); else printf("%d ", i+1); } printf("\n\n"); }