about summary refs log tree commit diff
path: root/usth/ICT2.1/labwork/3/Ex3.c
diff options
context:
space:
mode:
Diffstat (limited to 'usth/ICT2.1/labwork/3/Ex3.c')
-rw-r--r--usth/ICT2.1/labwork/3/Ex3.c46
1 files changed, 46 insertions, 0 deletions
diff --git a/usth/ICT2.1/labwork/3/Ex3.c b/usth/ICT2.1/labwork/3/Ex3.c
new file mode 100644
index 0000000..b934ec0
--- /dev/null
+++ b/usth/ICT2.1/labwork/3/Ex3.c
@@ -0,0 +1,46 @@
+/*
+ * Interactive guessing game.
+ * This is free and unencumbered software released into the public domain.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+
+#include "stack.h"
+
+char *random10(char *c)
+{
+	char *p = malloc(sizeof(char));
+	*p = rand() % 10;
+	while (c != NULL && *p == *c) {
+		free(p);
+		p = malloc(sizeof(char));
+		*p = rand() % 10;
+	}
+	return p;
+}
+
+int main()
+{
+	stack *s = mkstack();
+	char guess, lost = 0;
+	srand(time(NULL));
+	stack_push(s, random10(NULL));
+STEP2:
+	stack_push(s, random10(stack_top(s)));
+	char *p = stack_pop(s);
+	puts(lost ? "Make another guess between 0 and 9"
+		  : "Make a guess between 0 and 9");
+	scanf("%hhd", &guess);
+	if ((guess - *p) * (guess - *(char *) stack_top(s)) < 0) {
+		puts("YOU WIN!");
+		return 0;
+	} else if (lost) {
+		puts("YOU LOSE!");
+		return 0;
+	}
+	
+	lost = 1;
+	goto STEP2;
+}