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

export type main = [
  PutStringLn<"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!">
    : Then<[
      Print<`Wrong guess. Total attempts: ${
        [...Attempts, 0] extends infer Ls extends 0[] ? Ls['length'] : 0
      }`>,
      (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<
      Then<[
        PutString<"Your guess? ">,
        ReadLine
      ]>,
      AskForGuess<this['input'], Attempts>
    >
}