about summary refs log tree commit diff
path: root/usth/ICT2.1/labwork/3/Ex3.c
blob: b934ec0c6c476707a6ac2922210120c1154f6413 (plain) (blame)
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
/*
 * 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;
}