/* Copyright (c) 2018 Henri Landvik Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. */ #include #include #include #define ANSI_COLOR_RED "\x1b[31m" #define ANSI_COLOR_RESET "\x1b[0m" int readstr(char *ptr, int len); const char *file_content[22] = { "#", "You wake up in a mysterious cave and don't remember why you are here. Your head is hurting and you feel tired. What to do you do? (Write a or b and press Enter)", "a) Study the cave b) Start walking away from the cave", "Spider webs are all around you and it is difficult for you to see but in distance you see light. It looks like this is the way out and you start walking towards the light.", "You see in distance a light. This must be the way out from the cave and you start walking towards the light.", "", "#", "There is a door with a lock. It is written that to open this door you must answer a question. What is 2*111? (Write the answer down and press Enter)", "You opened the door and now you have escaped the cave.", "", "#", "You find yourself in a forest where everything around you is very green and bright. It is a day and birds are singing.", "In distance you see a lake and you start walking towards it. Write a and press Enter to continue.", "", "#", "You are next to a lake. You see in the right a tree where is written a question. What goes up in the morning up and goes down in the evening? Write down the answer and press Enter.", "You answered correctly and you see in distance a very high mountain. You plan to go there.", "", "#", "You have arrived on top of a mountain. It is very windy here and you feel cold. There is a tower not so far. You start to go towards it.", "To enter this tower you must answer this question. Why you are here?", "a) I don't know b) I know c) What's the stupid question" }; int file_rows = 0; int chapter_number = 1; int count_chapters = 1; int chapter_locations[5]; int chapter_location_now = 0; char answer[20] = ""; /*********************************************************************************/ /* READ THE FILE WHERE IS THE STORY AND SAVE IT TO A MULTIDIMENSIONAL CHAR ARRAY */ /*********************************************************************************/ int read_file(char *filename) { file_rows = 22; /* FILE* file = fopen(filename, "r"); if(!file) { perror("File opening failed"); return EXIT_FAILURE; } while(fgets(file_content[file_rows], sizeof(file_content[file_rows]), file) != NULL) { file_content[file_rows][strcspn(file_content[file_rows], "\n")] = 0; // Remove empty lines from char file_rows++; } if (ferror(file)) { puts("I/O error when reading"); } else if (feof(file)) { /* Reading file was succesful */ //} /* fclose(file); */ return 0; } /*********************/ /* CLEAR THE CONSOLE */ /*********************/ void clear_console() { //puts("\x1B[2J"); } /***********************************/ /* PRINT MESSAGE TO CONSOLE SCREEN */ /***********************************/ void print_message_to_console(char *message) { if(message == "error") { printf(ANSI_COLOR_RED "Wrong answer. Please try again.\n" ANSI_COLOR_RESET); } else { puts(message); } } /*****************/ /* TYPING READER */ /*****************/ void typing_reader() { answer[0] = 0; readstr(answer, 19); printf("\n"); } /***********************/ /* PLAY A CHAPTER TEXT */ /***********************/ void play_chapter_text(char *chapter_text) { printf("%s\n\n",chapter_text); } /**************************/ /* GO TO THE NEXT CHAPTER */ /**************************/ void go_to_next_chapter() { chapter_number++; chapter_location_now++; } /*************************************************************************/ /* FIND THE CHAPTER LOCATIONS FROM THE TEXT FILE AND SAVE IT TO AN ARRAY */ /*************************************************************************/ void find_chapter_locations() { int j = 0; for(int i = 0; i < file_rows; i++) { if(strcmp(file_content[i],"#") == 0) { chapter_locations[j] = i; j++; } } } /********************/ /* CONTROL THE GAME */ /********************/ void controller() { if(chapter_number == 1) { play_chapter_text(file_content[chapter_locations[chapter_location_now]+1]); play_chapter_text(file_content[chapter_locations[chapter_location_now]+2]); typing_reader(); if(strcmp(answer,"a") == 0) { clear_console(); play_chapter_text(file_content[chapter_locations[chapter_location_now]+3]); go_to_next_chapter(); controller(); } else if(strcmp(answer,"b") == 0) { clear_console(); play_chapter_text(file_content[chapter_locations[chapter_location_now]+4]); go_to_next_chapter(); controller(); } else { clear_console(); print_message_to_console("error"); controller(); } } else if(chapter_number == 2) { play_chapter_text(file_content[chapter_locations[chapter_location_now]+1]); typing_reader(); if(strcmp(answer,"222") == 0) { clear_console(); play_chapter_text(file_content[chapter_locations[chapter_location_now]+2]); go_to_next_chapter(); controller(); } else { clear_console(); print_message_to_console("error"); controller(); } } else if(chapter_number == 3) { play_chapter_text(file_content[chapter_locations[chapter_location_now]+1]); play_chapter_text(file_content[chapter_locations[chapter_location_now]+2]); typing_reader(); if(strcmp(answer,"a") == 0) { clear_console(); go_to_next_chapter(); controller(); } else { clear_console(); print_message_to_console("error"); controller(); } } else if(chapter_number == 4) { play_chapter_text(file_content[chapter_locations[chapter_location_now]+1]); typing_reader(); if(strcmp(answer,"sun") == 0) { clear_console(); play_chapter_text(file_content[chapter_locations[chapter_location_now]+2]); go_to_next_chapter(); controller(); } else { clear_console(); print_message_to_console("error"); controller(); } } else if(chapter_number == 5) { play_chapter_text(file_content[chapter_locations[chapter_location_now]+1]); play_chapter_text(file_content[chapter_locations[chapter_location_now]+2]); play_chapter_text(file_content[chapter_locations[chapter_location_now]+3]); typing_reader(); if(strcmp(answer,"a") == 0 || strcmp(answer,"b") == 0 || strcmp(answer,"c") == 0) { clear_console(); puts("---------------------------------------------------"); puts("---------------------------------------------------"); puts("YOU DID IT! YOU ENTERED THE TOWER AND WON THE GAME!"); puts("---------------------------------------------------"); puts("---------------------------------------------------"); } else { clear_console(); print_message_to_console("error"); controller(); } } else { puts("ERROR! NO CHAPTER FOUND!"); } } /*********************/ /* START THE PROGRAM */ /*********************/ void go(void) { read_file("story.txt"); clear_console(); find_chapter_locations(); controller(); }