Arnold joseph - 2015-10-15

It runs but crashes after i input the name of the player, i am still a
beginner programmer so pardon my codes for being so messy.

include <time.h>

include <stdio.h>

include <stdlib.h>

include <string.h>

define maxans 31

define easy_maxans 21

define maxs 10

void player(char player_name_ptr[maxs]);
int easy_start_game(char player_name[maxs][maxs],char
player_highest[maxs][maxs],int
easy_highest,int easy_score,int
right_answers[maxans]);
int normal_start_game(int
normal_highest,int normal_score,int
right_answers[maxans]);
int hard_start_game(int
hard_highest,int hard_score,int
right_answers[maxans]);
void draw_boxes(char answers[maxans][maxs],int right_answers[maxans]);
void displayall(char answers[maxans][maxs]);
int get_random_number(int max);
void shuffle_string(char
text);

int main()
{

  int right_answers[maxans], rigth_answers_index,len=0;
  int

opt,opt2,easy_highest=0,normal_highest=0,hard_highest=0,easy_score,normal_score,hard_score,i;
char player_name[maxs][maxs],player_highest[maxs][maxs];
char* player_name_ptr[maxs];

system("color 1c");
while(1){
  easy_score=0;
  normal_score=0;
  hard_score=0;
  // clear answers
  for (rigth_answers_index = 0; rigth_answers_index < maxans;

rigth_answers_index++) {
right_answers[rigth_answers_index] = 0;
}
printf("\n\n");
printf("\t\t\t ____\n");
printf("\t\t\t| WORD TWISTER |\n");
printf("\t\t\t|____|\n\n\n");
printf("\t\t\t[1] - Play game\n");
printf("\t\t\t[2] - highest_score\n");
printf("\t\t\t[3] - Exit\t");
scanf("%d",&opt);
if(opt==1){
for(i=0;i<maxs;i++){
len=strlen(player_name[i]);
if(len<=0){
player(player_name_ptr);
strcpy(player_name[i],player_name_ptr[maxs]);
printf("\n");
}
else{
continue;
}
break;
}
printf("\t\t\tChoose Difficulty:\n");
printf("\t\t\t[1] - Easy\n");
printf("\t\t\t[2] - Normal\n");
printf("\t\t\t[3] - Hard\t");
scanf("%d",&opt2);
if(opt2==1)
{

easy_start_game(player_name,player_highest,&easy_highest,&easy_score,right_answers);

              }
              else if(opt2==2)
              {

normal_start_game(&normal_highest,&normal_score,right_answers);
}
else if(opt2==3)
{

hard_start_game(&hard_highest,&hard_score,right_answers);
}
}
else if(opt==2)
{
system("cls");
for(i=0;i<maxans;i++){
printf("\t\t\t Easy Highest
Score:%s:%d",player_highest[i],easy_highest);
printf("\n");
}
printf("\n");
printf("\t\t\t Normal Highest Score:%d",normal_highest);
printf("\n");
printf("\t\t\t Hard Highest Score:%d",hard_highest);
}
else if(opt==3)
{
break;
}
else
("Invalid Option");
}

  return 0;

}
void player(char* player_name_ptr[maxs]){

 printf("\t\t\tEnter Player Name");
 scanf("%s",player_name_ptr[maxs]);

}

int easy_start_game(char player_name[maxs][maxs],char
player_highest[maxs][maxs],int easy_highest,int easy_score,int
right_answers[maxans]) {
char answers[easy_maxans][maxs]
={"alp","cap","lap","pal","act","alt","cat","lat","pat","apt","pac","tap",

"acta","paca","plat","clap","pact","talc","clap",
"clapt"};
int i,j,gameover=0,opt,opt2,life=5,found_answer=0,len,score=0;
char *original_text;
char shuffle_text[maxs];
char user_input[maxs];

 strcpy(shuffle_text, answers[19]);
 shuffle_string(shuffle_text);

 for (i = 0; i < maxans; i++) {
     system("cls");
     original_text = answers[i];
     draw_boxes(answers,right_answers);
     printf("\t\t\t    Text: %s\n", shuffle_text);
     printf("Score:%d\n", *easy_score);
     while (1) {
           printf("Life Left %d\n",life);
           printf("Please enter your answer: \t");
           scanf("%s", user_input);
           found_answer = 0;
           for (j = 0; j < maxans; j++) {
                if (strcmp(user_input, answers[j]) == 0) {
                      right_answers[j] = 1;
                      found_answer = 1;
                      len=strlen(answers[j]);
                      if(len==5)
                      {

                                *easy_score+=300;
                      }
                      else if(len==4)
                      {
                           *easy_score+=200;
                       }
                       else if(len<=3)
                       {
                           *easy_score+=50;
                       }
                      break;
                  }
           }
          if (!found_answer) {
               life-=1;
               if(life==0)
               {
                          gameover = 1;
               }
          }
           printf("\n");
           break;
     }
     if (gameover) {
                   system("cls");
                   displayall(answers);
                   printf("\t\t\tGame over...\nThese are the correct

answers\n");
printf("\t\t\tBack to Main Menu?\n[1] - Yes\n[2] -
No");
scanf("%d",&opt2);
if(opt2==1){
printf("Going back to Main Menu");
system("cls");
if(easy_score>easy_highest){

easy_highest=easy_score;

strcpy(player_highest[i],player_name[i]);
}
}
else if(opt2==2){
break;
}
break;
}
}
if(easy_score>easy_highest){
easy_highest=easy_score;
strcpy(player_highest[i],player_name[i]);

 }

}
int normal_start_game(int normal_highest,int normal_score,int
right_answers[maxans]) {
char answers[maxans][maxs] ={"air","ear","ave","ran","van",
"ire","vina","near","earn","rain",
"rive","rave","vain","vein","vine",
"rein","naive","raven","riven","ravin",
"invar","naevi","naiver","vainer","ravine",};
int i,j,gameover=0,opt,opt2,life=5,found_answer=0,len,score=0;
char *original_text;
char shuffle_text[maxs];
char user_input[maxs];

 strcpy(shuffle_text, answers[24]);
 shuffle_string(shuffle_text);

 for (i = 0; i < maxans; i++) {
     system("cls");
     original_text = answers[i];
     draw_boxes(answers,right_answers);
     printf("\t\t\t    Text: %s\n", shuffle_text);
     printf("\n\n");
     printf("Score:%d\n", *normal_score);
     while (1) {
           printf("Life Left %d\n",life);
           printf("Please enter your answer: \t");
           scanf("%s", user_input);
           found_answer = 0;
           for (j = 0; j < maxans; j++) {
                if (strcmp(user_input, answers[j]) == 0) {
                      right_answers[j] = 1;
                      found_answer = 1;
                      len=strlen(answers[j]);
                      if(len==6)
                      {
                                *normal_score+=500;
                      }
                      else if(len==5)
                      {
                                *normal_score+=300;
                      }
                      else if(len==4)
                      {
                           *normal_score+=200;
                       }
                       else if(len<=3)
                       {
                           *normal_score+=50;
                       }
                      break;
                  }
           }
          if (!found_answer) {
               life-=1;
               if(life==0)
               {
                          gameover = 1;
               }
          }
           printf("\n");
           break;
     }
     if (gameover) {
                   system("cls");
                   displayall(answers);
                   printf("\t\t\tGame over...\nThese are the correct

answers\n");
printf("\t\t\tBack to Main Menu?\n[1] - Yes\n[2] -
No");
scanf("%d",&opt2);
if(opt2==1){
printf("Going back to Main Menu");
system("cls");
}
else if(opt2==2){
break;
}
break;
}
}
if(normal_score>normal_highest){
normal_highest=normal_score;
}

}
int hard_start_game(int hard_highest,int hard_score,int
right_answers[maxans]) {
char answers[maxans][maxs] ={"dear","ears","eras","seam","seem",
"ease","arms","sear","seer","arse",
"seed","armed","dears","erase","dream",
"derma","eased","meads","reads","smear",
"dreams","remade","seamer","seared","dermas",

"reamed","seamed","erased","edemas","madres",};
int i,j,gameover=0,opt,opt2,life=5,found_answer=0,len,score=0;
char *original_text;
char shuffle_text[maxs];
char user_input[maxs];

 strcpy(shuffle_text, answers[29]);
 shuffle_string(shuffle_text);

 for (i = 0; i < maxans; i++) {
     system("cls");
     original_text = answers[i];
     draw_boxes(answers,right_answers);
     printf("\t\t\t    Text: %s\n", shuffle_text);
     printf("\n\n");
     printf("Score:%d\n", *hard_score);
     while (1) {
           printf("Life Left %d\n",life);
           printf("Please enter your answer: \t");
           scanf("%s", user_input);
           found_answer = 0;
           for (j = 0; j < maxans; j++) {
                if (strcmp(user_input, answers[j]) == 0) {
                      right_answers[j] = 1;
                      found_answer = 1;
                      len=strlen(answers[j]);
                      if(len==6)
                      {
                                *hard_score+=500;
                      }
                      else if(len==5)
                      {
                                *hard_score+=300;
                      }
                      else if(len==4)
                      {
                           *hard_score+=200;
                       }
                       else if(len<=3)
                       {
                           *hard_score+=50;
                       }
                      break;
                  }
           }
          if (!found_answer) {
               life-=1;
               if(life==0)
               {
                          gameover = 1;
               }
          }
           printf("\n");
           break;
     }
     if (gameover) {
                   system("cls");
                   displayall(answers);
                   printf("\t\t\tGame over...\nThese are the correct

answers\n");
printf("\t\t\tBack to Main Menu?\n[1] - Yes\n[2] -
No");
scanf("%d",&opt2);
if(opt2==1){
printf("Going back to Main Menu");
system("cls");
}
else if(opt2==2){
break;
}
break;
}
}
if(hard_score>hard_highest){
hard_highest=hard_score;
}
}
void draw_boxes(char answers[maxans][maxs],int right_answers[maxans]){
int i, j, len;

printf(" \t\t\t ____________________________\n");
printf("\t\t\t|        WORD TWISTER        |\n");
printf("\t\t\t|____________________________|\n");
printf("\n");
 for (i = 0; i < maxans; i++) {
     printf("\t\t          ");
        if (right_answers[i]){
        printf("%s", answers[i]);
        }
     else {
          len = strlen(answers[i]);
         for (j = 0; j < len; j++){
             printf("__.");
             }

}

         printf("\n");
 }

}
void displayall(char answers[maxans][maxs]){
int i, j, len;

 for (i = 0; i < maxans; i++) {
         printf("\t\t          ");
         printf("%s",answers[i]);
         printf("\n");
 }

}
int get_random_number(int max) {
srand(time(0));
// rand() generates a random number from 0 to 32767
return rand() % max;
}

void shuffle_string(char clue[maxans]) {
char temp;
int len = strlen(clue);
int i;
int j;

  for (i = 0; i < len; i++) {
      j = get_random_number(len);

      if (i == j)
         continue;

      temp = clue[i];
      clue[i] = clue[j];
      clue[j] = temp;
  }

}