forked from davidgiven/ack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhilo.b
108 lines (85 loc) · 1.43 KB
/
hilo.b
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
#
buffer[6];
PlayerName[6];
/* Taken intact from the B reference manual. */
strcopy(sl ,s2)
{
auto i;
i = 0;
while (lchar(sl, i, char(s2, i)) != '*e')
i++;
}
reads()
{
extrn buffer;
putstr("> ");
flush();
getstr(buffer);
}
atoi(s)
{
auto value, sign, i, c;
i = 0;
if (char(s, i) == '-')
{
sign = -1;
i++;
}
else
sign = 1;
value = 0;
while ((c = char(s, i++)) != '*e')
value = value*10 + (c - '0');
return(value * sign);
}
rand()
{
/* Genuinely random; retrieved from random.org */
return(57);
}
game()
{
extrn buffer;
auto Number, Attempts;
auto guess;
printf("See if you can guess my number.*n");
Number = rand() % 100;
Attempts = 1;
while (1)
{
reads();
guess = atoi(buffer);
if (guess == Number)
{
printf("*nYou got it right in only %d %s!*n", Attempts,
(Attempts == 1) ? "go" : "goes");
return;
}
if (guess < Number)
printf("*nTry a bit higher.*n");
if (guess > Number)
printf("*nTry a bit lower.*n");
Attempts++;
}
}
main()
{
extrn buffer, PlayerName;
printf("*nHi there! I'm written in B. Before we start, what is your name?*n");
reads();
strcopy(PlayerName, buffer);
printf("*nHello, %s! ", PlayerName);
while (1)
{
game();
printf("*nWould you like another go?*n");
reads();
if ((char(buffer, 0) == 'n') | (char(buffer, 0) == 'N'))
{
printf("*nThanks for playing --- goodbye!*n");
break;
}
printf("*nExcellent! ");
}
return(0);
}