-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNumberGuessingGame.hl
More file actions
33 lines (28 loc) · 952 Bytes
/
Copy pathNumberGuessingGame.hl
File metadata and controls
33 lines (28 loc) · 952 Bytes
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
/@ A number guessing game showcasing conditions, loops, and functions @/
fx get_random(int start, int end) {
string seed = input("Enter any number for randomness seed: ");
int seed_length = seed.length();
int range = end - start + 1;
int random_number = start + (seed_length % range);
return random_number;
}
print("Welcome to the Number Guessing Game!");
const int MIN = 1;
const int MAX = 100;
int secret_number = get_random(MIN, MAX);
int attempts = 0;
while (true) {
print("Guess a number between " + STR(MIN) + " and " + STR(MAX) + ": ");
string g = input("");
int guess = INT(g);
attempts = attempts + 1;
if (guess < secret_number) {
print("Too low! Try again.");
} elseif (guess > secret_number) {
print("Too high! Try again.");
} else {
print("Congratulations! You've guessed the number!");
print("Total attempts: " + STR(attempts));
endloop;
}
}