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.2.1.22 tracks/fsharp/exercises/bowling/Example.fs
trackler-2.2.1.21 tracks/fsharp/exercises/bowling/Example.fs
trackler-2.2.1.20 tracks/fsharp/exercises/bowling/Example.fs
trackler-2.2.1.19 tracks/fsharp/exercises/bowling/Example.fs
trackler-2.2.1.18 tracks/fsharp/exercises/bowling/Example.fs
trackler-2.2.1.17 tracks/fsharp/exercises/bowling/Example.fs
trackler-2.2.1.16 tracks/fsharp/exercises/bowling/Example.fs
trackler-2.2.1.15 tracks/fsharp/exercises/bowling/Example.fs
trackler-2.2.1.14 tracks/fsharp/exercises/bowling/Example.fs
trackler-2.2.1.13 tracks/fsharp/exercises/bowling/Example.fs
trackler-2.2.1.12 tracks/fsharp/exercises/bowling/Example.fs
trackler-2.2.1.11 tracks/fsharp/exercises/bowling/Example.fs
trackler-2.2.1.10 tracks/fsharp/exercises/bowling/Example.fs
trackler-2.2.1.9 tracks/fsharp/exercises/bowling/Example.fs
trackler-2.2.1.8 tracks/fsharp/exercises/bowling/Example.fs
trackler-2.2.1.7 tracks/fsharp/exercises/bowling/Example.fs
trackler-2.2.1.6 tracks/fsharp/exercises/bowling/Example.fs
trackler-2.2.1.5 tracks/fsharp/exercises/bowling/Example.fs
trackler-2.2.1.4 tracks/fsharp/exercises/bowling/Example.fs
trackler-2.2.1.3 tracks/fsharp/exercises/bowling/Example.fs