-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnumguess.slsh
executable file
·68 lines (53 loc) · 1.78 KB
/
numguess.slsh
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
#!/usr/bin/env slsh
require("rand");
define input_number(prompt, default) {
variable line, i = default;
printf(prompt);
fgets(&line, stdin);
sscanf(line, "%i", &i);
return i;
}
define input_string(prompt) {
variable line;
printf(prompt);
fgets(&line, stdin);
return strtrim(line);
}
define slsh_main() {
variable name, limit, maxTries, num, guess, tries, triesWord, again;
printf("Welcome to NumGuess S-Lang version!\n\n");
name = input_string("Enter your name: ");
if(name == "") name = "Player";
printf("\nWelcome %s, ", name);
limit = input_number("enter upper limit: ", 10);
if(limit < 10) limit = 10;
maxTries = typecast(floor(log(limit) / log(2)) + 1, Integer_Type);
do {
printf("\nGuess my number between 1 and %i!\n\n", limit);
(tries, num) = (0, rand_int(1, limit));
do {
guess = input_number("Guess: ", NULL);
if(guess == NULL) {
printf("That's just plain wrong.\n");
} else ifnot(1 <= guess <= limit) {
printf("Out of range.\n");
} else {
tries++;
switch(guess)
{ guess < num : printf("Too low!\n"); }
{ guess > num : printf("Too high!\n"); }
}
} while(guess != num);
triesWord = tries == 1 ? "try" : "tries";
printf("\nWell done %s, you guessed my number from %i %s!\n", name, tries, triesWord);
switch(tries)
{ tries == 1 : printf("You're one lucky bastard!"); }
{ tries < maxTries : printf("You are the master of this game!"); }
{ tries == maxTries : printf("You are a machine!"); }
{ tries <= maxTries * 1.1 : printf("Very good result!"); }
{ tries <= limit : printf("Try harder, you can do better!"); }
{ printf("I find your lack of skill disturbing!"); }
again = strup(input_string("\nPlay again [y/N]? "));
} while(again[0] == 'Y');
printf("\nOkay, bye.\n");
}