-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.fw
More file actions
71 lines (58 loc) · 1.66 KB
/
program.fw
File metadata and controls
71 lines (58 loc) · 1.66 KB
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
(: out rt.console.out)
(: in rt.console.in)
// this highlight on github is brilliant indeed
(: read-digits! (fn [] int32
((parse-int (in.next-line!))
(do
(out.println! "Invalid input :(")
(read-digits!)
)
)
))
(: generate-number (fn [(= digits int32)] string8
(let possible-digits (mut ['1' '2' '3' '4' '5' '6' '7' '8' '9'])
(do
(: symbols (mut (vec char8 digits)))
(for i in symbols
(i.set! (possible-digits.remove! (rnd-int 0 possible-digits.size))))
(string8 symbols)
)
)
))
(: read-guess! (fn [(= number string8)] string8
(let input (in.next-line!)
((== input.length number.length)
input
(do
(out.println! "Your guess must be " digits " digits long, try again!")
(read-guess! number)
)
)
)
))
(: guess-number! (fn [(= number string8)] unit
(let running (mut true) (while running (do
(: input (read-guess! number))
(: bulls (mut 0))
(: cows (mut 0))
((== input number)
(do
(out.println! digits " bulls! You won!")
(set! running false))
(for i in input (let ch (i)
((== (number.get i.index) ch)
(inc! bulls)
((> (number.index-of ch) 0) (inc! cows) ())
)))
)
)))))
(out.println! "Welcome! Enter amount of digits to guess: ")
(: digits (read-digits!))
(: number (generate-number digits))
(out.println! "Number guessed, now try to guess it.\n"
"The output will indicate how far are you from the number:\n"
"bull means that your guess contains a correct digit in a right place,
cow means that your guess contains a correct digit in a wrong place")
(guess-number! number)
// oh no
// i just realized that i can make the language a lot more readable using the same principle i used in my old macro system