Sha256: 62b34fa6eba9c262efa8f7c9415c7e40654e2370ed9087aedf7c36fb5bdc432f

Contents?: true

Size: 1.69 KB

Versions: 228

Compression:

Stored size: 1.69 KB

Contents

module Bowling

let map2 f opt1 opt2 = 
    match opt1, opt2 with
    | Some x, Some y -> Some (f x y)
    | _ -> None

let numberOfFrames = 10
let maximumFrameScore = 10
let minimumFrameScore = 0

let newGame = Some []

let validatePins pins = 
    if pins < minimumFrameScore || pins > maximumFrameScore then 
        None 
    else 
        Some pins

let isStrike pins = pins = maximumFrameScore
let isSpare pins1 pins2 = pins1 + pins2 = maximumFrameScore

let roll pins rolls = map2 (fun rolls pins -> rolls @ [pins]) rolls (validatePins pins)

let rec scoreRolls totalScore frame rolls = 
    let isLastFrame = frame = numberOfFrames
    let gameFinished = frame = numberOfFrames + 1

    let scoreStrike remainder = 
        match remainder with
        | x::y::zs when isLastFrame ->
            if x + y > 10 && x <> 10 then None
            else scoreRolls (totalScore + 10 + x + y) (frame + 1) zs
        | x::y::zs ->
            scoreRolls (totalScore + 10 + x + y) (frame + 1) (x::y::zs)
        | _ ->
            None

    let scoreSpare x y remainder = 
        match remainder with 
        | z::zs->
            scoreRolls (totalScore + x + y + z) (frame + 1) (if isLastFrame then zs else z::zs)
        | _ ->
            None
            
    let scoreNormal x y remainder =
        match validatePins (x + y) with
        | Some z -> scoreRolls (totalScore + z) (frame + 1) remainder
        | None -> None

    match rolls with
    | [] -> if gameFinished then Some totalScore else None
    | x::xs when isStrike x -> scoreStrike xs        
    | x::y::ys when isSpare x y -> scoreSpare x y ys        
    | x::y::zs -> scoreNormal x y zs       
    | _ -> None
    
let score = Option.bind (scoreRolls 0 1)

Version data entries

228 entries across 228 versions & 1 rubygems

Version Path
trackler-2.1.0.4 tracks/fsharp/exercises/bowling/Example.fs
trackler-2.1.0.3 tracks/fsharp/exercises/bowling/Example.fs
trackler-2.1.0.2 tracks/fsharp/exercises/bowling/Example.fs
trackler-2.1.0.1 tracks/fsharp/exercises/bowling/Example.fs
trackler-2.1.0.0 tracks/fsharp/exercises/bowling/Example.fs
trackler-2.0.8.55 tracks/fsharp/exercises/bowling/Example.fs
trackler-2.0.8.54 tracks/fsharp/exercises/bowling/Example.fs
trackler-2.0.8.53 tracks/fsharp/exercises/bowling/Example.fs
trackler-2.0.8.52 tracks/fsharp/exercises/bowling/Example.fs
trackler-2.0.8.51 tracks/fsharp/exercises/bowling/Example.fs
trackler-2.0.8.50 tracks/fsharp/exercises/bowling/Example.fs
trackler-2.0.8.49 tracks/fsharp/exercises/bowling/Example.fs
trackler-2.0.8.48 tracks/fsharp/exercises/bowling/Example.fs
trackler-2.0.8.47 tracks/fsharp/exercises/bowling/Example.fs
trackler-2.0.8.46 tracks/fsharp/exercises/bowling/Example.fs
trackler-2.0.8.45 tracks/fsharp/exercises/bowling/Example.fs
trackler-2.0.8.44 tracks/fsharp/exercises/bowling/Example.fs
trackler-2.0.8.43 tracks/fsharp/exercises/bowling/Example.fs
trackler-2.0.8.42 tracks/fsharp/exercises/bowling/Example.fs
trackler-2.0.8.41 tracks/fsharp/exercises/bowling/Example.fs