-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.fs
126 lines (111 loc) · 3.88 KB
/
Program.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
module Bowling
type Frame =
| Strike
| Spare of int
| Partial of int * int
| TenthFrame of int * int * int option
module Frame =
let private toInt =
function
| '-' -> 0
| 'X' -> 10
| c -> c |> System.Char.GetNumericValue |> int
let parse (s: string) =
List.ofSeq s
|> fun chrs ->
match chrs with
| 'X' :: [] -> Strike
| c :: '/' :: [] -> c |> toInt |> Spare
| x :: y :: [] -> Partial (toInt x, toInt y)
| xs -> failwithf "Invalid Frame: %A" xs
let parseTenth (xs: char seq) =
xs
|> List.ofSeq
|> function
| 'X' :: c2 :: '/' :: [] -> c2 |> toInt |> fun x2 -> TenthFrame (10,x2, Some (10 - x2))
| 'X' :: c1 :: c2 :: [] -> TenthFrame (10, toInt c1, Some(toInt c2))
| c1 :: '/' :: c2 :: [] -> c1 |> toInt |> fun x -> TenthFrame (x, 10 - x, c2 |> toInt |> Some)
| c1 :: c2 :: [] -> TenthFrame (toInt c1, toInt c2, None)
| _ -> failwithf "Invalid Tenth Frame: %A" xs
let calcTenth (x1, x2, bonus) = (bonus |> Option.defaultValue 0) + x1 + x2
let private rand = System.Random()
let random () =
match rand.Next 11 with
| 10 -> Strike
| x ->
match (x, rand.Next(11 - x)) with
| (x,y) when x+y=10 -> Spare x
| (x,y) -> Partial (x,y)
let randomTenth () : Frame =
( match rand.Next 11 with
| 10 ->
let y = rand.Next 11
let z =
if y = 10 then
11 |> rand.Next
else
(11 - y) |> rand.Next
(10,y,Some z)
| x ->
let y = rand.Next(11 - x)
if x + y = 10 then
(x,y, 11 |> rand.Next |> Some)
else
(x,y,None) )
|> TenthFrame
type Game = Frame list
module Game =
let parse (s: string) : Game =
s.Split(' ')
|> Array.mapi (fun i s' ->
if i < 9 then
Frame.parse s'
else
Frame.parseTenth s')
|> List.ofArray
let private (|StrikeThrown|_|) (game: Game) =
match game with
| Strike :: tail ->
let frameScore =
match tail with
| Spare _ :: _ -> 20
| Strike :: Strike :: _ -> 30
| Strike :: Spare x :: _
| Strike :: Partial (x,_) :: _
| Strike :: TenthFrame (x,_,_) :: _ -> 20 + x
| Partial (x,y) :: _
| TenthFrame (x,y,_) :: [] -> 10 + x + y
| _ -> failwithf "Invalid remaining frames: %A" game
Some (frameScore, tail)
| _ -> None
let private (|SpareThrown|_|) (game: Game) =
match game with
| Spare _ :: tail ->
let frameScore =
match tail with
| Strike :: _ -> 20
| Spare x :: _
| TenthFrame (x,_,_) :: []
| Partial (x,_) :: _ -> 10 + x
| _ -> failwithf "Invalid remaining frames: %A" game
Some (frameScore, tail)
| _ -> None
let rec private calc (game: Game) (score: int) =
match game with
| StrikeThrown (frameScore, tail) -> score + frameScore |> calc tail
| SpareThrown (frameScore, tail) -> score + frameScore |> calc tail
| Partial (x,y) :: tail -> score + x + y |> calc tail
| TenthFrame (x1,x2,bonus) :: [] -> score + Frame.calcTenth (x1,x2,bonus)
| [] -> score
| _ -> failwithf "Invalid remaining frames: %A" game
let calculate (game: Game) = calc game 0
let display (game: Game, score: int) =
game |> List.iter (printfn "%A")
score |> printfn "-----------------\n\rScore: %i\r\n******************\r\n"
let random _ : Game = List.init 10 (function | 9 -> Frame.randomTenth () | _ -> Frame.random ())
[ "X X X X X X X X X XXX"
"9- 9- 9- 9- 9- 9- 9- 9- 9- 9-"
"5/ 5/ 5/ 5/ 5/ 5/ 5/ 5/ 5/ 5/5" ]
|> List.map Game.parse
|> List.append (List.init 30 Game.random)
|> List.iter (fun game -> (game, Game.calculate game) |> Game.display)