vendredi 17 avril 2020

Random Filling 2D Array of Pointer Problem (C)

This is a 6x6 puzzle. There are 4 words hidden in this puzzle. Words and their positions are: “hello” [e,0] & [e,4] • “world” [b,0] & [f,4] • “great” [a,0] & [a,4] • “random” [f,5] & [a,5] Note that: Row Numbers are denoted with letters a to z.In my code i have given a list of 100 words. I will choose 10 random words from this list and place them in the puzzle table with random directions and positions. (Words can be described left to right, top-down, diagonally or in the reverse order of these directions) • Following word placement, i will fill the rest of the board with random characters. )My puzzle will be 20*20

greatm
wsbtyo
uozlcd
xkrjmn
helloa
vcpwdr

My code is below and the code does not print anything on the screen even though i think i checked all the conditions correctly.I hope that it is not forbidden to share a link, the mentioned words.txt file is in the link https://www67.zippyshare.com/v/EblU1qxR/file.html

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include<string.h>

char *word(FILE *fd){
    char *str0;
    int end, loop, line;
    int len;

    len = 20;
    str0 = malloc(len);
    line = rand() % 100 + 1;
    rewind(fd);
    end = 0;
    for (loop = 0; loop < line; ++loop){
        if (NULL == fgets(str0, len, fd)){
            end = 1;
            break;
        }
    }

    if (end){
        free(str0);
        str0 = NULL;
    }
    return str0;
}

int
main(void){

int i=0,j=0,x=0,y=0,z=0,c=0;
    char *str,str_2;
    srand(time(NULL));
    char*puzzle[20][20]={0};

    char *file = "words.txt";
    FILE *fd = fopen(file, "r");

    if (fd == NULL) {
        printf("Failed to open file\n");
        return 1;
    }

         x=rand()%20+1;
         y=rand()%20+1;       // direction :1 left to right 2 reverse,3 top to bottom 4 reverse,
         z=rand()%8+1;      //5 left top cross to right bottom cross b 6 reverse,7 right top cross to 
                            /*left bottom cross 8 reverse;*/
  while(c<10){
        srand(time(NULL));
        x=rand()%20+1;
        y=rand()%20+1;
        z=rand()%8+1;

    if(z==1){
        for(i=y; ; ){//left to right
                for(j=x;j<x+strlen(str);j++){
                    while(puzzle[i][j]==NULL){
                            str = word(fd);
                            if(strlen(str)+x<=20){
                            puzzle[i][j]=str;
                            free(str);

                            }
                            else{
                             x=rand()%20+1;
                             y=rand()%20+1;
                             continue;
                            }

                    }
                    while(puzzle[i][j]!=NULL){
                          x=rand()%20+1;
                          y=rand()%20+1;
                          continue;


                    }


                }
        }
      }
      if(z==2){//rigth to left
        for(i=y; ;  ){
                for(j=x+strlen(str);j>x;j--){
                         while(puzzle[i][j]==NULL){
                            str = word(fd);
                            if(strlen(str)+x<=20){
                            puzzle[i][j]=str; free(str);
                            }
                            else{
                             x=rand()%20+1;
                             y=rand()%20+1;
                             continue;
                            }

                        }
                        while(puzzle[i][j]!=NULL){
                          x=rand()%20+1;
                          y=rand()%20+1;
                          continue;


                    }

                }
        }
      }

       if(z==3){
        for(i=y;i<y+strlen(str);i++){//top to bottom
                for( j=x ;    ;   ){
                         while(puzzle[i][j]==NULL){
                            str = word(fd);
                            if(strlen(str)+x<=20){
                            puzzle[i][j]=str; free(str);
                            }
                            else{
                             x=rand()%20+1;
                             y=rand()%20+1;
                             continue;
                            }

                        }
                        while(puzzle[i][j]!=NULL){
                          x=rand()%20+1;
                          y=rand()%20+1;
                          continue;


                        }

                }
        }
      }

        if(z==4){//bottom to top
          for(i=y+strlen(str);i>y;i--){
                for(j=x;  ;    ){
                         while(puzzle[i][j]==NULL){
                            str = word(fd);
                            if(strlen(str)+x<=20){
                            puzzle[i][j]=str; free(str);
                            }
                            else{
                             x=rand()%20+1;
                             y=rand()%20+1;
                             continue;
                            }

                            }
                        while(puzzle[i][j]!=NULL){
                          x=rand()%20+1;
                          y=rand()%20+1;
                          continue;


                        }

                }
        }
      }

        if(z==5){
            for(i=y;i<y+strlen(str);i++){
                for(j=x;j<x+strlen(str);j++){
                        while(puzzle[i][j]==NULL){
                            str = word(fd);
                            if(strlen(str)+x<=20){
                            puzzle[i][j]=str; free(str);
                            }
                            else{
                             x=rand()%20+1;
                             y=rand()%20+1;
                             continue;
                            }
                            //take new index again
                            }
                        while(puzzle[i][j]!=NULL){
                          x=rand()%20+1;
                          y=rand()%20+1;
                          continue;


                        }

                }
        }
      }

        if(z==6){
           for(i=y+strlen(str);i>y;i--){
                for(j=x+strlen(str);j>x;j--){
                        while(puzzle[i][j]==NULL){
                            str = word(fd);
                            if(strlen(str)+x<=20){
                            puzzle[i][j]=str; free(str);
                            }
                            else{
                             x=rand()%20+1;
                             y=rand()%20+1;
                             continue;
                            }

                            }
                        while(puzzle[i][j]!=NULL){
                          x=rand()%20+1;
                          y=rand()%20+1;
                          continue;


                        }

                }
        }
      }

      if(z==7){
            for(i=y;i<y+strlen(str);i++){
                for(j=x+strlen(str);j>x;j--){
                       while(puzzle[i][j]==NULL){
                            str = word(fd);
                            if(strlen(str)+x<=20){
                            puzzle[i][j]=str; free(str);
                            }
                            else{
                             x=rand()%20+1;
                             y=rand()%20+1;
                             continue;
                            }

                            }
                        while(puzzle[i][j]!=NULL){
                          x=rand()%20+1;
                          y=rand()%20+1;
                          continue;


                        }

                }
        }
      }

      if(z==8){
        for(i=y+strlen(str);i>y;i--){
            for(j=x;j<x+strlen(str);j++){
                    while(puzzle[i][j]==NULL){
                            str = word(fd);
                            if(strlen(str)+x<=20){
                            puzzle[i][j]=str; free(str);
                            }
                            else{
                             x=rand()%20+1;
                             y=rand()%20+1;
                             continue;
                            }

                            }
                        while(puzzle[i][j]!=NULL){
                          x=rand()%20+1;
                          y=rand()%20+1;
                          continue;


                        }

                }
        }
      }

fclose(fd);
c++;
  }
for(i=0;i<20;i++){
    for(j=0;j<20;j++){
        printf("%s",puzzle[i][j]);
    }

}
return 0;
}



Aucun commentaire:

Enregistrer un commentaire