i have put the main part i think the mistake is in the part that i wrote and i have attached the file that i use in this program
int main (int argc, char *argv[])
{
FILE *fptr;
char path[40];
int start[1];
int Start[1];
char exit[5]="exit";
char c;
int n1,n2,number_of_words,number_of_rows=0,number_of_col=0,i,k,m,counter=0;
char **words; //words that will be searched in the matrix
int row,col;
char **matrix;
if( (fptr = fopen( "deneme.txt","rb")) == NULL )
{
printf("the file could not be found!!\n");
turn: //label
do
{
printf("please enter a valid file path:");
printf("if you do not want to play the game just write EXIT");
scanf("%s",path);
if(!(strncmp(path,exit,4))){ //if the user does not want to play anymore
printf("you know! if you do not want to play go and find a more interesting game!");
break;
}
}
while((fptr=fopen( path ,"rb"))==NULL); //if the file could not be found or the path does not correct ask the user to enter a valid path
} else {
fscanf(fptr, "%d", &n1); //height
fscanf(fptr, "%d", &n2); //width
matrix=(char**)calloc(n1,sizeof(char *)); //dynamic memory allocation for the matrix
for(m=0;m<n1;m++){
matrix[m]=(char*) calloc(n2,sizeof(char));
}
for(row=0;row<=n1;row++) {
for(col=0;col<=n2;col++){
do
{
c = fgetc(fptr);
}
while(c == ' ' || c=='\n');
if( isalpha(c) == 1){
printf("There is an unvalid character in the letter matrix:\n");
goto turn;
}
matrix[row][col] = c;
printf("%c ",c);
counter++;
if(counter%n1==0){
printf("\n");
}
}
}
number_of_words=fgetc(fptr);
words=(char**)calloc(number_of_words,sizeof(char*)); //dynamic memory allocation for the words
for(i=0;i<number_of_words;i++){
words[i]=(char*)calloc(80,sizeof(char)); //dynamic memory allocation for all the worlds by one by one
}
for(i=0;i<number_of_words;i++){
k=0;
c=words[i][k];
for(row=0;row<number_of_rows;row++){
for(col=0;col<number_of_col;col++){
if(c==matrix[row][col]){
if(row==0 && col==0){
if (eastsearch(matrix,row,col+1,words,i,++k)){
start[0]=row;
Start[0]=col;
printf("%c starts at position %d %d",words,&row,&col) ;
}else if(southsearch(matrix,row+1,col,words,i,++k)){
start[0]=row;
Start[0]=col;
printf("%c starts at position %d %d",words,&row,&col) ;
}else if(southeastsearch(matrix,row+1,col+1,words,i,++k)){
start[0]=row;
Start[0]=col;
printf("%c starts at position %d %d",words,&row,&col) ;
}
}else if(row==0&&col!=0) {
if (eastsearch(matrix,row,col+1,words,i,++k)){
start[0]=row ;
Start[0]=col;
printf("%c starts at position %d %d",words,&row,&col);
}else if(westsearch(matrix,row,col-1,words,i,k)) {
start[0]=row;
Start[0]=col;
printf("%c starts at position %d %d",words,&row,&col);
} else if(southsearch(matrix,row+1,col,words,i,++k)){
start[0]=row;
Start[0]=col;
printf("%c starts at position %d %d",words,&row,&col);
}else if(southeastsearch(matrix,row+1,col+1,words,i,++k)){
start[0]=row;
Start[0]=col;
printf("%c starts at position %d %d",words,&row,&col) ;
}else if(southwestsearch(matrix,row-1,col+1,words,i,++k)){
start[0]=row;
Start[0]=col;
printf("%c starts at position %d %d",words,&row,&col) ;
}
} else if (row!=0&&col==0) {
if(eastsearch(matrix,row,col+1,words,i,++k)){
start[0]=row ;
Start[0]=col;
printf("%c starts at position %d %d",words,&row,&col);
} else if(southsearch(matrix,row+1,col,words,i,++k)){
start[0]=row;
Start[0]=col;
printf("%c starts at position %d %d",words,&row,&col);
}else if(northsearch(matrix,row-1,col,words,i,++k)){
start[0]=row;
Start[0]=col;
printf("%c starts at position %d %d",words,&row,&col);
} else if(southeastsearch(matrix,row+1,col+1,words,i,++k)){
start[0]=row;
Start[0]=col;
printf("%c starts at position %d %d",words,&row,&col);
} else if(northeastsearch(matrix,row-1,col+1,words,i,++k)){
start[0]=row;
Start[0]=col;
printf("%c starts at position %d %d",&words[i],&row,&col);
}
} else if (row==0&&col==number_of_col-1) {
if(westsearch(matrix,row,col-1,words,i,++k)) {
start[0]=row;
Start[0]=col;
printf("%c starts at position %d %d",words,&row,&col);
} else if (southsearch(matrix,row+1,col,words,i,++k)){
start[0]=row;
Start[0]=col;
printf("%c starts at position %d %d",words,&row,&col);
} else if (southwestsearch(matrix,row-1,col+1,words,i,++k)){
start[0]=row;
Start[0]=col;
printf("%c starts at position %d %d",words,&row,&col) ;
}
} else if (row==number_of_rows-1&&col==0) {
if (eastsearch(matrix,row,col+1,words,i,++k)){
start[0]=row ;
Start[0]=col;
printf("%c starts at position %d %d",words,&row,&col);
} else if(northsearch(matrix,row-1,col,words,i,++k)){
start[0]=row;
Start[0]=col;
printf("%c starts at position %d %d",words,&row,&col);
} else if (northeastsearch(matrix,row-1,col+1,words,i,++k)){
start[0]=row;
Start[0]=col;
printf("%c starts at position %d %d",words,&row,&col);
}
} else if (row==number_of_rows-1&&col!=0) {
if(eastsearch(matrix,row,col+1,words,i,++k)){
start[0]=row ;
Start[0]=col;
printf("%c starts at position %d %d",words,&row,&col);
} else if (westsearch(matrix,row,col-1,words,i,++k)) {
start[0]=row;
Start[0]=col;
printf("%c starts at position %d %d",words,&row,&col);
} else if(northsearch(matrix,row-1,col,words,i,++k)){
start[0]=row;
Start[0]=col;
printf("%c starts at position %d %d",words,&row,&col);
} else if (northeastsearch(matrix,row-1,col+1,words,i,++k)){
start[0]=row;
Start[0]=col;
printf("%c starts at position %d %d",words,&row,&col);
} else if (northwestsearch(matrix,row-1,col-1,words,i,++k)) {
start[0]=row;
Start[0]=col;
printf("%c starts at position %d %d",words,&row,&col);
}
} else if (row==number_of_rows-1&&col==number_of_col-1) {
if (northsearch(matrix,row-1,col,words,i,++k)){
start[0]=row;
Start[0]=col;
printf("%c starts at position %d %d",words,&row,&col);
} else if (westsearch(matrix,row,col-1,words,i,++k)) {
start[0]=row;
Start[0]=col;
printf("%c starts at position %d %d",words,&row,&col);
} else if (northwestsearch(matrix,row-1,col-1,words,i,++k)) {
start[0]=row;
Start[0]=col;
printf("%c starts at position %d %d",words,&row,&col);
}
} else if (row!=0&& col==number_of_col-1) {
if(westsearch(matrix,row,col-1,words,i,++k)) {
start[0]=row;
Start[0]=col;
printf("%c starts at position %d %d",words,&row,&col);
} else if (northsearch(matrix,row-1,col,words,i,++k)){
start[0]=row;
Start[0]=col;
printf("%c starts at position %d %d",words,&row,&col);
} else if (southsearch(matrix,row+1,col,words,i,++k)){
start[0]=row;
Start[0]=col;
printf("%c starts at position %d %d",words,&row,&col);
} else if (southwestsearch(matrix,row-1,col+1,words,i,++k)){
start[0]=row;
Start[0]=col;
printf("%c starts at position %d %d",words,&row,&col) ;
} else if (northwestsearch(matrix,row-1,col-1,words,i,++k)) {
start[0]=row;
Start[0]=col;
printf("%c starts at position %d %d",words,&row,&col);
}
} else {
if (eastsearch(matrix,row,col+1,words,i,++k)){
start[0]=row ;
Start[0]=col;
printf("%c starts at position %d %d",words,&row,&col);
} else if (westsearch(matrix,row,col-1,words,i,++k)) {
start[0]=row;
Start[0]=col;
printf("%c starts at position %d %d",words,&row,&col);
} else if(northsearch(matrix,row-1,col,words,i,++k)){
start[0]=row;
Start[0]=col;
printf("%c starts at position %d %d",words,&row,&col);
} else if(southsearch(matrix,row+1,col,words,i,++k)){
start[0]=row;
Start[0]=col;
printf("%c starts at position %d %d",words,&row,&col);
} else if (southwestsearch(matrix,row-1,col+1,words,i,++k)){
start[0]=row;
Start[0]=col;
printf("%c starts at position %d %d",words,&row,&col) ;
} else if (northwestsearch(matrix,row-1,col-1,words,i,++k)) {
start[0]=row;
Start[0]=col;
printf("%c starts at position %d %d",words,&row,&col);
} else if (northeastsearch(matrix,row-1,col+1,words,i,++k)){
start[0]=row;
Start[0]=col;
printf("%c starts at position %d %d",words,&row,&col);
} else if (southeastsearch(matrix,row+1,col+1,words,i,++k)){
start[0]=row;
Start[0]=col;
printf("%c starts at position %d %d",words,&row,&col) ;
}
}
}
}
}
}
}
return 0;
}
Last edited by dsmith : 18-May-2004 at 16:43.
Reason: Please use [c] & [/c] for syntax highlighting
|