Debian Codegraph @

examples/emcu-firmware/uart-game.c-src

raw
1 /*
2 Copyright (c) 2018 Henri Landvik
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10
11
12 The above copyright notice and this permission notice shall be included in all
13 copies or substantial portions of the Software.
14
15 */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20
21 #define ANSI_COLOR_RED "\x1b[31m"
22 #define ANSI_COLOR_RESET "\x1b[0m"
23
24 int readstr(char *ptr, int len);
25
26
27 const char *file_content[22] = {
28 "#",
29 "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)",
30 "a) Study the cave b) Start walking away from the cave",
31 "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.",
32 "You see in distance a light. This must be the way out from the cave and you start walking towards the light.",
33 "",
34 "#",
35 "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)",
36 "You opened the door and now you have escaped the cave.",
37 "",
38 "#",
39 "You find yourself in a forest where everything around you is very green and bright. It is a day and birds are singing.",
40 "In distance you see a lake and you start walking towards it. Write a and press Enter to continue.",
41 "",
42 "#",
43 "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.",
44 "You answered correctly and you see in distance a very high mountain. You plan to go there.",
45 "",
46 "#",
47 "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.",
48 "To enter this tower you must answer this question. Why you are here?",
49 "a) I don't know b) I know c) What's the stupid question"
50 };
51 int file_rows = 0;
52 int chapter_number = 1;
53 int count_chapters = 1;
54 int chapter_locations[5];
55 int chapter_location_now = 0;
56 char answer[20] = "";
57
58
59 /*********************************************************************************/
60 /* READ THE FILE WHERE IS THE STORY AND SAVE IT TO A MULTIDIMENSIONAL CHAR ARRAY */
61 /*********************************************************************************/
62 int read_file(char *filename)
63 {
64 file_rows = 22;
65 /*
66 FILE* file = fopen(filename, "r");
67 if(!file) {
68 perror("File opening failed");
69 return EXIT_FAILURE;
70 }
71
72 while(fgets(file_content[file_rows], sizeof(file_content[file_rows]), file) != NULL) {
73 file_content[file_rows][strcspn(file_content[file_rows], "\n")] = 0; // Remove empty lines from char
74 file_rows++;
75 }
76
77 if (ferror(file)) { puts("I/O error when reading"); }
78 else if (feof(file)) { /* Reading file was succesful */ //}
79
80 /*
81 fclose(file);
82 */
83 return 0;
84 }
85
86 /*********************/
87 /* CLEAR THE CONSOLE */
88 /*********************/
89 void clear_console() {
90 //puts("\x1B[2J");
91 }
92
93 /***********************************/
94 /* PRINT MESSAGE TO CONSOLE SCREEN */
95 /***********************************/
96 void print_message_to_console(char *message) {
97 if(message == "error") {
98 printf(ANSI_COLOR_RED "Wrong answer. Please try again.\n" ANSI_COLOR_RESET);
99 } else {
100 puts(message);
101 }
102 }
103
104 /*****************/
105 /* TYPING READER */
106 /*****************/
107 void typing_reader() {
108 answer[0] = 0;
109 readstr(answer, 19);
110 printf("\n");
111 }
112
113 /***********************/
114 /* PLAY A CHAPTER TEXT */
115 /***********************/
116 void play_chapter_text(char *chapter_text) {
117 printf("%s\n\n",chapter_text);
118 }
119
120 /**************************/
121 /* GO TO THE NEXT CHAPTER */
122 /**************************/
123 void go_to_next_chapter() {
124 chapter_number++;
125 chapter_location_now++;
126 }
127
128 /*************************************************************************/
129 /* FIND THE CHAPTER LOCATIONS FROM THE TEXT FILE AND SAVE IT TO AN ARRAY */
130 /*************************************************************************/
131 void find_chapter_locations()
132 {
133 int j = 0;
134 for(int i = 0; i < file_rows; i++)
135 {
136 if(strcmp(file_content[i],"#") == 0)
137 {
138 chapter_locations[j] = i;
139 j++;
140 }
141 }
142 }
143
144 /********************/
145 /* CONTROL THE GAME */
146 /********************/
147 void controller()
148 {
149 if(chapter_number == 1)
150 {
151 play_chapter_text(file_content[chapter_locations[chapter_location_now]+1]);
152 play_chapter_text(file_content[chapter_locations[chapter_location_now]+2]);
153 typing_reader();
154
155 if(strcmp(answer,"a") == 0)
156 {
157 clear_console();
158 play_chapter_text(file_content[chapter_locations[chapter_location_now]+3]);
159 go_to_next_chapter();
160 controller();
161 }
162 else if(strcmp(answer,"b") == 0)
163 {
164 clear_console();
165 play_chapter_text(file_content[chapter_locations[chapter_location_now]+4]);
166 go_to_next_chapter();
167 controller();
168 }
169 else {
170 clear_console();
171 print_message_to_console("error");
172 controller();
173 }
174 }
175 else if(chapter_number == 2)
176 {
177 play_chapter_text(file_content[chapter_locations[chapter_location_now]+1]);
178 typing_reader();
179
180 if(strcmp(answer,"222") == 0)
181 {
182 clear_console();
183 play_chapter_text(file_content[chapter_locations[chapter_location_now]+2]);
184 go_to_next_chapter();
185 controller();
186 }
187 else {
188 clear_console();
189 print_message_to_console("error");
190 controller();
191 }
192 }
193 else if(chapter_number == 3)
194 {
195 play_chapter_text(file_content[chapter_locations[chapter_location_now]+1]);
196 play_chapter_text(file_content[chapter_locations[chapter_location_now]+2]);
197 typing_reader();
198
199 if(strcmp(answer,"a") == 0)
200 {
201 clear_console();
202 go_to_next_chapter();
203 controller();
204 }
205 else {
206 clear_console();
207 print_message_to_console("error");
208 controller();
209 }
210 }
211 else if(chapter_number == 4)
212 {
213 play_chapter_text(file_content[chapter_locations[chapter_location_now]+1]);
214 typing_reader();
215
216 if(strcmp(answer,"sun") == 0)
217 {
218 clear_console();
219 play_chapter_text(file_content[chapter_locations[chapter_location_now]+2]);
220 go_to_next_chapter();
221 controller();
222 }
223 else {
224 clear_console();
225 print_message_to_console("error");
226 controller();
227 }
228 }
229 else if(chapter_number == 5)
230 {
231 play_chapter_text(file_content[chapter_locations[chapter_location_now]+1]);
232 play_chapter_text(file_content[chapter_locations[chapter_location_now]+2]);
233 play_chapter_text(file_content[chapter_locations[chapter_location_now]+3]);
234 typing_reader();
235
236 if(strcmp(answer,"a") == 0 || strcmp(answer,"b") == 0 || strcmp(answer,"c") == 0)
237 {
238 clear_console();
239 puts("---------------------------------------------------");
240 puts("---------------------------------------------------");
241 puts("YOU DID IT! YOU ENTERED THE TOWER AND WON THE GAME!");
242 puts("---------------------------------------------------");
243 puts("---------------------------------------------------");
244 }
245 else {
246 clear_console();
247 print_message_to_console("error");
248 controller();
249 }
250 }
251 else
252 {
253 puts("ERROR! NO CHAPTER FOUND!");
254 }
255 }
256
257 /*********************/
258 /* START THE PROGRAM */
259 /*********************/
260 void go(void)
261 {
262 read_file("story.txt");
263 clear_console();
264 find_chapter_locations();
265 controller();
266 }