diff options
| author | 2024-08-02 20:07:38 +0300 | |
|---|---|---|
| committer | 2024-08-02 20:07:38 +0300 | |
| commit | c0f92795ac74c03815030765ee6645094146a407 (patch) | |
| tree | f4ffef180bc2d79c399d9a809947223c212b60a6 /dice.orang | |
| download | orang-c0f92795ac74c03815030765ee6645094146a407.tar.gz orang-c0f92795ac74c03815030765ee6645094146a407.tar.xz orang-c0f92795ac74c03815030765ee6645094146a407.zip | |
Initial commit
Diffstat (limited to 'dice.orang')
| -rw-r--r-- | dice.orang | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/dice.orang b/dice.orang new file mode 100644 index 0000000..0fae27b --- /dev/null +++ b/dice.orang | |||
| @@ -0,0 +1,47 @@ | |||
| 1 | def dice sides times = | ||
| 2 | let once _ = randInt 1 (sides + 1) in | ||
| 3 | let aux acc times = | ||
| 4 | if times ?= 0 then acc | ||
| 5 | else aux (acc + once ()) (times - 1) | ||
| 6 | in | ||
| 7 | aux 0 times | ||
| 8 | |||
| 9 | def guessing_game _ = | ||
| 10 | let secret = randInt 1 101 | ||
| 11 | and guessOnce _ = do | ||
| 12 | print "Make your guess: "; | ||
| 13 | let guess = readInt () in | ||
| 14 | if secret > guess then do | ||
| 15 | printLn "My number is larger than that..."; | ||
| 16 | false | ||
| 17 | end else if secret < guess then do | ||
| 18 | printLn "My number is smaller than that..."; | ||
| 19 | false | ||
| 20 | end else | ||
| 21 | true | ||
| 22 | end | ||
| 23 | and shouldEnd times = | ||
| 24 | if guessOnce () then do | ||
| 25 | printLn "Congratulations! You guessed right!"; | ||
| 26 | true | ||
| 27 | end else if times ?= 1 then do | ||
| 28 | printLn "That was your last guess..."; | ||
| 29 | true | ||
| 30 | end else do | ||
| 31 | printLn "Guess again..."; | ||
| 32 | false | ||
| 33 | end | ||
| 34 | and aux times = do | ||
| 35 | print "You have "; print times; printLn " guesses left"; | ||
| 36 | if shouldEnd times then do | ||
| 37 | print "My number was "; printLn secret | ||
| 38 | end else | ||
| 39 | aux (times - 1) | ||
| 40 | end in do | ||
| 41 | printLn "Welcome to the number guessing game"; | ||
| 42 | printLn "I will pick a random number between 1 and 100 (inclusive)"; | ||
| 43 | printLn " and you try to guess it."; | ||
| 44 | printLn "After each guess, I will tell you if my number was larger or smaller"; | ||
| 45 | printLn "Good Luck and Have Fun!"; | ||
| 46 | aux 10 | ||
| 47 | end | ||