Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
int units = 10;
int anzhost = 7;
int max_days = 500;
int runs = 100;
int show = 0;
int pause = 0;
char commit = 'n';

void usage(char *prog) {
	fprintf(
			stderr,
			"\n%s usage:\t%s\t"
				"-u (DEFAULT: 7)\n"
				"\t\t\t\t\t-h (DEFAULT: 10)\n"
				"\t\t\t\t\t-d (DEFAULT: 500)\n"
				"\t\t\t\t\t-r (DEFAULT: 100)\n"
				"\t\t\t\t\t-c (DEFAULT: n) [possible types: a(ll), s(uccess) only, m(issed) only, n(one)]\n"
				"\t\t\t\t\t-s (DEFAULT: 0)\n\n\t-->"
				"If you call -s without a time parameter it uses the default pause,\n\tbut enables the show mode. (!!THIS MUST BE THE LAST ARGUMENT!!)\n\t is on MS Windows in milliseconds and on Linux in seconds (afaik)\n"
				"\n\nIf you call %s without arguments, the default values will be taken.\n",
			prog, prog, prog);

	printf("\n\n\t\t\t\t\t\t\t\t\t\t\t\t\tby 0di 2oo4\n\n");
}

int options(int argc, char *argv[]) {
	if (argc == 1) {
		printf("\n\nUSE\n\t\t%s -H OR\n\t\t%s /?\nfor HELP\n\n", argv[0],
				argv[0]);
		sleep(1500);
	}

	if (argc > 13) {
		fprintf(stderr, "!!! too many arguments !!!\n");

		exit(0);

	}

	int i;

	for (i = 1; i < argc; i++) {
		switch (argv[i][1]) {
		case 'H':
		case '?': {
			usage(argv[0]);
			exit(0);
		}
			break;
		case 'u': {
			if (argv[i + 1]) {
				units = atoi(argv[i + 1]);
				i++;
				break;
			}
			break;
		}
		case 'h': {
			if (argv[i + 1]) {
				anzhost = atoi(argv[i + 1]);
				i++;
				break;
			}
			break;
		}
		case 'd': {
			if (argv[i + 1]) {
				max_days = atoi(argv[i + 1]);
				i++;
				break;

			}
			break;
		}
		case 'r': {
			if (argv[i + 1]) {
				runs = atoi(argv[i + 1]);
				i++;
				break;
			}
			break;
		}

		case 's': {
			show = 1;

			if (argv[i + 1]) {
				pause = atoi(argv[i + 1]);

				printf("SHOW MODE ON\nPAUSE: %i\n", pause);

				i++;

				break;

			}
			break;

		}

		case 'c': {
			if (argv[i + 1]) {
				commit = argv[i + 1][0];

				i++;

				break;

			}
			break;

		}

		default:
			fprintf(stderr, "unknown option %s\n", argv[i]);
			usage(argv[0]);
			exit(0);
			break;

		}
	}

	printf("RUNS: %i\n", runs);
	printf("UNITS: %i\n", units);
	printf("HOSTAGES: %i\n", anzhost);
	printf("MAX_DAYS: %i\n", max_days);
	printf("COMMIT: %c\n", commit);

	if (show == 1) {
		printf("SHOW MODE: ON\n");

		printf("PAUSE: %i", pause);

	} else {
		printf("SHOW MODE: OFF");
	}

	sleep(1500);

	return 0;

}

int main(int argc, char *argv[]) {

	options(argc, argv);

	int host_know[anzhost][units];
	//Remember the knowledge of each hostage
	int dayc;
	//Count the total days
	int unitc;
	//Count the Units (weeks), initalise with 0, so the game begins in the 0th week (unit)
	int hostunit;
	//Hostage Unit
	int light;
	//The light is off at the beginning
	int release;
	//If the hostages could be released this si set to 1

	int x, y, z;

	int hod;
	//Number of the "Hostage of the day"
	int miss = 0;
	//Counts all missed runs
	int suc = 0;
	//Counts sucessful runs
	float sucper, missper;
	//stores the percent of suc and miss (relative to runs)
	int days[runs];
	//Counts how many days it took for a sucessful run

	//Initialize Day-Array with 0
	for (x = 0; x < runs; x++) {
		days[x] = 0;

	}

	//Hostages are named like that: 0-6, therefore the weeks (UNITS) are also 0-6 or higher, depeneding on the constance values.

	//Choose the HOD (hostage of the day :-) ) --> Number between 0 and 6;

	//Turn on the light if it's the unit of the hostage.

	//Add Knowledge to the hostage (Check the week and check if the light is on). Check if the hostage has enought knowlegde to win the game
	//WARNING: DON'T DELETE ANY KNOWLEDGE!!!

	//Make rand() more random
	srand(time(NULL));

	//Increase runs to get the user wished result... (1 of n instead of 0 of n) and to get the correct iteration number
	runs++;

	for (y = 1; y < runs; y++) {
		x = 0;

		z = 0;

		release = 0;

		light = 0;

		hostunit = 0;

		unitc = 0;

		dayc = 0;

		//Initalise Knowledege (nobody knows anything ;-) )
		while (x < anzhost) {
			while (z < units) {
				host_know[x][z] = 0;

				z++;

			}
			x++;

			z = 0;

		}

		while (dayc < max_days) {
			if (release == 1) {
				break;

			}

			//If a new Unit begins, reset the light and increase the unit counters
			if (dayc % units == 0) {
				hostunit++;

				unitc++;

				light = 0;

			}
			dayc++;

			if (hostunit == anzhost) {
				hostunit = 0;

			}

			//It's more randomly if you pass a pseudo random number to srand (the System time in this case)
			//After that you can call rand() which should be more randomly...

			hod = rand() % anzhost;

			//Turn on the light if it's the HOU (Hostage of the Unit[Week]).
			if (hostunit == hod) {
				light = 1;

			}

			//If the light is turned on, the knowledge of the HUD will increase
			if (light == 1) {
				host_know[hod][hostunit] = 1;

			}

			//Check if the knowledge is as big as he can release all hostages
			x = 0;

			while (x < units) {
				if (host_know[hod][x] == 1) {
					x++;

				}
				//If the HOD has no info about one other hostage, the test will fail
				else
					break;

				if (x == 7) {
					suc++;

					days[y] = dayc;

					release = 1;

				}
			}

			if (show == 1) {
				//Print Status of each day
				runs--;

				printf(
						"Current Hostage:\t%i\nHostage Unit:\t\t%i\nUNITS:\t\t\t%i\nTotal Days:\t\t%i\nLIGHT:\t\t\t%i\t\t\t\tHOD(%i): %i %i %i %i %i %i %i\nRUN: %i of %i\n\n",
						hod, hostunit, unitc, dayc, light, hod,
						host_know[hod][0], host_know[hod][1],
						host_know[hod][2], host_know[hod][3],
						host_know[hod][4], host_know[hod][5],
						host_know[hod][6], y, runs);

				runs++;

				//Sleep 1 second (1000 millisecond [MS WINDOWS style]), to get surely a random number (time(NULL))
				sleep(pause);

			}

			//Print out SUCCESS-Message
			if (release == 1) {
				printf(
						"\n\nHOSTAGES WERE RELEASED!!! After %i days (%i. run)\n\n",
						dayc, y);

				if (commit == 's' || commit == 'a') {
					system("pause");

				}
			}

		}

		//Count the tries where the hostages won't be released within "max_tries" times
		if (release != 1) {
			miss++;

			printf(
					"MISSED! The Hostage weren't released within %i days (%i. run)\n\n",
					max_days, y);

			if (commit == 'm' || commit == 'a') {
				system("pause");

			}
			dayc = max_days;

		}
	}

	//If there are no successful runs, there are no days to count
	if (suc > 0) {
		//Reset the day counter to reuse it for some other purpose
		dayc = 0;

		//Count all days for every release
		for (x = 0; x < runs; x++) {
			if (days[x] > 0) {
				dayc = dayc + days[x];
			}

		}

		//Calculate the average
		dayc = dayc / suc;

	}

	//Calucalte the percent of success and miss
	//runs = 100%
	runs--;
	//Correct the Display-Fake from above

	missper = miss * 100 / runs;

	sucper = suc * 100 / runs;

	//Print out the FAZIT
	printf("\n\n\n------------------TOTAL-------------------------------");

	printf(
			"\n\nMISS: %i (%f %%)\n\nSUCESS: %i (%f %%)\n\n\nAverage days: %i\n\n\n",
			miss, missper, suc, sucper, dayc);

	return 0;

}