summaryrefslogtreecommitdiff
path: root/dice.orang
blob: d8e52de512c74e16e74c60d30f43de62bda5b08d (plain) (blame)
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
def dice sides times =
  let once () = randInt 1 (sides + 1) in
  let aux acc times =
    if times ?= 0 then acc
    else aux (acc + once ()) (times - 1)
  in
  aux 0 times

def guessing_game () =
  let secret = randInt 1 101
  and guessOnce () = do
    print "Make your guess: ";
    let guess = readInt () in
    if secret > guess then do
      printLn "My number is larger than that...";
      false
    end else if secret < guess then do
      printLn "My number is smaller than that...";
      false
    end else
      true
  end
  and shouldEnd times =
    if guessOnce () then do
      printLn "Congratulations! You guessed right!";
      true
    end else if times ?= 1 then do
      printLn "That was your last guess...";
      true
    end else do
      printLn "Guess again...";
      false
    end
  and aux times = do
    print "You have "; print times; printLn " guesses left";
    if shouldEnd times then do
      print "My number was "; printLn secret
    end else
      aux (times - 1)
  end in do
    printLn "Welcome to the number guessing game";
    printLn "I will pick a random number between 1 and 100 (inclusive)";
    printLn " and you try to guess it.";
    printLn "After each guess, I will tell you if my number was larger or smaller";
    printLn "Good Luck and Have Fun!";
    aux 10
  end