aboutsummaryrefslogtreecommitdiff
path: root/examples/guess-number.ts
blob: d08c616942b47dfaacce569c2f30a3ec7f1a06a9 (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
import {
  Print,
  PutString,
  Bind,
  Kind1,
  JsExpr,
  ReadLine,
  Do,
  PutStringLn,
} from '../stdlib'

export type main = [
  PutStringLn<'Guess a number between 0 & 9. You have 5 guesses'>,
  Bind<JsExpr<'Math.floor(Math.random() * 10)'>, StartGuessing>
]

interface AskForGuess<N extends number, Attempts extends 0[]>
  extends Kind1<string> {
  return: `${this['input']}` extends `${N}`
    ? PutStringLn<'Yay! You got it right!'>
    : Do<
        [
          PutString<'Wrong guess. Total attempts'>,
          Print<`${[...Attempts, 0] extends infer Ls extends 0[]
            ? Ls['length']
            : 0}/5`>,
          (StartGuessing<[...Attempts, 0]> & { input: N })['return']
        ]
      >
}

interface StartGuessing<Attempts extends 0[] = []> extends Kind1<number> {
  return: Attempts['length'] extends 5
    ? PutStringLn<'Max attempts exceeded. Game over!'>
    : Bind<
        Do<[PutString<'Your guess? '>, ReadLine]>,
        AskForGuess<this['input'], Attempts>
      >
}