Skip to content

Commit

Permalink
Write a very minimal game for guessing numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxWilson committed Mar 4, 2024
1 parent 6e04842 commit 6f68631
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
29 changes: 23 additions & 6 deletions scratch/GuessNumber.fsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
#load "SketchUI.fsx"
open SketchUI

let init = ()
let update msg model = model
let r = System.Random()
type Model = {
truth: int
guesses: string list
wins: int
}
with static member fresh() = { truth = r.Next(1, 10); guesses = []; wins = 0 }
let init _ = Model.fresh()
let update msg model =
match msg with
| n when model.truth = n ->
let model = { Model.fresh() with wins = model.wins + 1 }
printfn "You guessed it! {n} is correct. You've now won %d times!" model.wins
model
| n ->
let feedback = if n < model.truth then $"{n}: Higher!" else $"{n}: Lower!"
{ model with guesses = model.guesses @ [feedback] }
let view model =
"Hello, world! Notice that there's no dispatch argument"
let send: unit -> unit = connect init update view
$"You've won {model.wins} times. What you know about this next number: {model.guesses}"
let send = connect init update view

send()
send 10
send 5
send 3
send 4
5 changes: 4 additions & 1 deletion scratch/SketchUI.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
#endif

let connect init update view =
let dispatch = notImpl
let mutable current = init()
let dispatch msg =
current <- update msg current
printfn "\n%s" (view current)
dispatch

0 comments on commit 6f68631

Please sign in to comment.