diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/guess-number.ts | 36 |
1 files changed, 27 insertions, 9 deletions
diff --git a/examples/guess-number.ts b/examples/guess-number.ts index aca3915..8f5d535 100644 --- a/examples/guess-number.ts +++ b/examples/guess-number.ts @@ -1,15 +1,33 @@ -import { Print, PutString, Bind, Kind1, JsExpr, ReadLine, Seq } from '../src/stdlib' +import { Print, PutString, Bind, Kind1, JsExpr, ReadLine, Then, PutStringLn } from '../src/stdlib' -export type main = Bind< - JsExpr<"Math.floor(Math.random() * 10)">, - StartGuessing -> +export type main = [ + PutStringLn<"You have 5 guesses">, + Bind< + JsExpr<"Math.floor(Math.random() * 10)">, + StartGuessing + > +] -interface AskForGuess<N extends number> extends Kind1<string> { - return: this['input'] extends `${N}` ? Print<"Yaya"> : Print<"naaah"> +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 extends Kind1<number> { - return: Bind<Seq<[PutString<"Your guess? ">, ReadLine]>, AskForGuess<this['input']>> +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> + > } |
