-
Notifications
You must be signed in to change notification settings - Fork 2
/
number_game.fb
84 lines (63 loc) · 1.46 KB
/
number_game.fb
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
func main() i32 {
// Initialize random number generator (broken nil keyword)
srand(time(nil as *i64) as u32);
// Get target number
var target = rand() % 20 + 1;
// Loop
for {
// Get guess
var str [128]u8;
var ptr = &str[0];
printf("Guess a number between 1 and 20: ");
input(ptr, 128);
var guess = parseInt(ptr);
// Compare guess
if (guess == target) {
printf("Congratulations, you win!\n\n");
return 0;
}
else if (guess > target) {
printf("Wrong guess, you guessed too high\n\n");
}
else {
printf("Wrong guess, you guessed too low\n\n");
}
}
return 0;
}
func parseInt(str *u8) i32 {
var i = 0;
var num = 0;
for {
var ch = str[i];
if (ch == '\0') {
break;
}
if (ch >= '0' && ch <= '9') {
num *= 10;
num += (ch - '0') as i32;
}
i += 1;
}
return num;
}
func input(str *u8, length i32) {
var i = 0;
for {
var ch = getchar();
if (ch == 10) {
str[i] = '\0';
return;
}
str[i] = ch as u8;
i += 1;
if (i >= length - 1) {
return;
}
}
}
extern func time(arg *i64) i64
extern func srand(seed u32) void
extern func rand() i32
extern func printf(format *u8, ...) void
extern func getchar() i32