Add a B version of the hilo program.

This commit is contained in:
David Given 2016-12-29 17:20:51 +00:00
parent 4e68af9781
commit e460adc923
2 changed files with 109 additions and 0 deletions

View file

@ -5,6 +5,7 @@ local conly = {
}
local sourcefiles = filenamesof(
"./hilo.b",
"./hilo.bas",
"./hilo.c",
"./hilo.mod",

108
examples/hilo.b Normal file
View file

@ -0,0 +1,108 @@
#
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);
}