import { Terminal } from "@effect/platform"
import type { PlatformError } from "@effect/platform/Error"
import { Effect, Option, Random } from "effect"
import { NodeRuntime, NodeTerminal } from "@effect/platform-node"
const secret = Random.nextIntBetween(1, 100)
const parseGuess = (input: string) => {
const n = parseInt(input, 10)
return isNaN(n) || n < 1 || n > 100 ? Option.none() : Option.some(n)
const display = (message: string) =>
Effect.gen(function* () {
const terminal = yield* Terminal.Terminal
yield* terminal.display(`${message}\n`)
const prompt = Effect.gen(function* () {
const terminal = yield* Terminal.Terminal
yield* terminal.display("Enter a guess: ")
return yield* terminal.readLine
const answer: Effect.Effect<
Terminal.QuitException | PlatformError,
> = Effect.gen(function* () {
const input = yield* prompt
const guess = parseGuess(input)
if (Option.isNone(guess)) {
yield* display("You must enter an integer from 1 to 100")
ok: Effect.Effect<A, E, R>,
ko: Effect.Effect<A, E, R>
Effect.gen(function* () {
yield* display("Too high")
} else if (guess < secret) {
yield* display("Too low")
const end = display("You guessed it!")
Terminal.QuitException | PlatformError,
Effect.gen(function* () {
const guess = yield* answer
Effect.suspend(() => loop(secret))
const game = Effect.gen(function* () {
`We have selected a random number between 1 and 100.
See if you can guess it in 10 turns or fewer.
We'll tell you if your guess was too high or too low.`
yield* loop(yield* secret)
NodeRuntime.runMain(game.pipe(Effect.provide(NodeTerminal.layer)))