c - Strange error while adding a feature to my little game (0xC0000005) -
hello everyone,
i decided time ago write own version of minesweepers practice , did it. game ran perfectly, after deciding add "choose difficulty" option window freezes , error message, saying program not respond. line 0xc0000005 appeares. have tryed many, many things: moving code main() seperate function(now in int playgame()), allocating more memory in heap, creating seperate .c file store piece of code, nothing worked sofar. came code after few weeks, still have no clue why happening. can me this? hope code not hard read. added comments explaining what. still new c.
#include <stdio.h> #include <stdlib.h> #include <time.h> #include "diflvl.c" int displayfiled(char **field); //prints out map of field int combine(char answer, int answer1); //combines user input , field numeration int randomizer(int **minearray); //generates random mine map int difficulty(); int playgame(); int main(){ int playgame(); playgame(); system("pause"); return 0; } int randomizer(int **minearray){ int difficulty(); int i, j; srand(time(null)); int mines; int placemine; int totalmines; //int diflvl=2; int diflvl=difficulty(); for(i=0, totalmines=0; i<10; i++){ for(j=0, mines=0; j<10 && mines<diflvl; j++){ placemine= rand() % 2; minearray[i][j] = placemine; if(placemine==1){ ++mines; }; }; totalmines+=mines; }; return totalmines; } int displayfiled(char **field){ int i, j; printf(" b c d e f g h j\n"); printf(" --------------------\n"); (i=0; i<10; i++){ if (i==9){ printf("%d |", i+1); }else{ printf("%d |", i+1); }; (j=0; j<10; j++){ printf("%c ", field[i][j]); if (j==9){ printf("\n"); }; }; }; printf("\n"); return 0; } int playgame(){ int displayfiled(char **field); int combine(char answer, int answer1); int randomizer(int ** minearray); char y_char; //column character (a, b, c etc.) int x; //row int y; //y_char converted number int **minearray; //stores map of mines char **fielddisplay; //prints out map of field int i, j; //counters int life=1; int movepl=0; //no dying on first move int globalmines; //number of mines placed int openedfields=0; //counts number of fields opened //int diflvl; //int difficulty(); //diflvl= difficulty(); /*disabled trhee lines above while trying solutions*/ /*int difficulty() called int randomizer()*/ system("cls"); /*allocates memory minearray*/ minearray= (int*)calloc(10, sizeof(int)); for(i = 0; < 10; i++){ minearray[i] = calloc(10, sizeof(int)); }; /*allocates memory fielddisplay*/ fielddisplay= (int*)calloc(10, sizeof(int)); for(i = 0; < 10; i++){ fielddisplay[i] = calloc(10, sizeof(int)); }; /*default of fields ?*/ (i=0; i<10; i++){ (j=0; j<10; j++){ fielddisplay[i][j]='?'; }; }; globalmines= randomizer(minearray); while(life==1 && openedfields<(100-globalmines)){ /*for checking purposes only*/ /*for (i=0; i<10; i++){ (j=0; j<10; j++){ printf("%d ", minearray[i][j]); if (j==9){ printf("\n"); }; }; };*/ //printf("\ndifficulty level %d\n", diflvl); printf("total number of mines %d\n\n", globalmines); printf("\tmove nr. %d\n\n", movepl+1); displayfiled(fielddisplay); printf("which field want activate?\ntype first letter, space , number (a 1, b 10 etc.)\n"); scanf("%c %d", &y_char, &x); if (y_char >= 'a' && y_char <= 'z'){ y = y_char - 'a'; }else if(y_char >= 'a' && y_char <= 'z'){ y = y_char - 'a'; }; /*checks if field mine*/ /*x-1 because player chooses 1 10*/ if (minearray[x-1][y]==0 && fielddisplay[x-1][y]=='?'){ movepl++; fielddisplay[x-1][y]='0'; openedfields=openedfields+1; open : if (((x-2)<10) && ((x-2)>=0)){ if (minearray[x-2][y]==0 && fielddisplay[x-2][y]=='?'){ fielddisplay[x-2][y]='0'; openedfields=openedfields+1; }; }; if ((x<10) && (x>=0)){ if (minearray[x][y]==0 && fielddisplay[x][y]=='?'){ fielddisplay[x][y]='0'; openedfields=openedfields+1; }; }; if (((y+1)<10) && ((y+1)>=0)){ if (minearray[x-1][y+1]==0 && fielddisplay[x-1][y+1]=='?'){ fielddisplay[x-1][y+1]='0'; openedfields=openedfields+1; }; }; if (((y-1)<10) && ((y-1)>=0)){ if (minearray[x-1][y-1]==0 && fielddisplay[x-1][y-1]=='?'){ fielddisplay[x-1][y-1]='0'; openedfields=openedfields+1; }; }; system("cls"); //clears console screen }else if (minearray[x-1][y]==0 && fielddisplay[x-1][y]=='0'){ system("cls"); printf("you can't choose opened field!\n\n"); }else if(minearray[x-1][y]==1 && movepl==0){ /*only activates on first turn if players hits mine*/ movepl++; minearray[x-1][y]= 0; fielddisplay[x-1][y]='0'; globalmines=globalmines-1; goto open; system("cls"); }else{ system("cls"); printf("you died ! died ! died !\n\n"); printf("moves made: %d\n\n", movepl-1); fielddisplay[x-1][y]='1'; displayfiled(fielddisplay); --life; }; }; if(openedfields==(100-globalmines)){ printf("congratulations! won game!\n\n"); displayfiled(fielddisplay); }; for(i = 0; < 10; i++){ free(minearray[i]); }; free(minearray); for(i = 0; < 10; i++){ free(fielddisplay[i]); }; free(fielddisplay); return 0; }
the diflvl.c file:
#include <stdio.h> #include <stdlib.h> int difficulty(){ int diflvl; while(1){ printf("please choose difficulty level:\n"); printf("easy-1\nnormal-2\nnightmare-3\n"); printf("your answer: "); scanf(" %d", &diflvl); if(diflvl>=1 && diflvl<=3){ break; }else{ system("cls"); continue; }; }; system("cls"); return diflvl; }
i created it, because thought maybe main() had many code in , maybe why difficulty option wasnt working right.
edit after user promped enter difficulty level, mine map created, after choosing filed, program crashes.
solved scanf("%c %d", &y_char, &x); changed scanf(" %c %d", &y_char, &x);
first, don't allocate two-dimensional fields correctly. "outer" field must hold int *
, not int
:
minearray = calloc(10, sizeof(*minearray)); (i = 0; < 10; i++) { minearray[i] = calloc(10, sizeof(*minearray[i])); }
another potential source of segmentation fault y
might end uninitialised , therefore garbage value. cause scanf
:
scanf("%c %d", &y_char, &x);
most scanf
formats skip white space before conversion, %c
doesn't. read newline character char %c
when expect read letter. because new-line character white space, can hot-fix placing space before %c
? format:
scanf(" %c %d", &y_char, &x);
(i hot-fix, because isn't solution. scanf
doesn't treat new-line characters specially; space. better solution might read line first fgets
, scan sscanf
. @ least can treat each line frash input. (and input should ensure bad input ignored.)
lastly, strange include *.c
file. if want spread ypur project on various files, idea, should write header file each *.c
, has file's interface. include header files in other *.c
files; compile *.c
files objects separately , link them. rocess controlled makefiles or projects.
Comments
Post a Comment