0votos

Kata CodeBreaker en F#

por josejuan hace 6 años

Otra.

Consigue descifrar el código de seguridad para acceder al super laboratorio científico del doctor Maligno. No te será fácil, pero seguro que será divertido!

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
open System 
 
let getSymbols () = 
    printfn "Enter symbols:" 
    Console.ReadLine().Split [|' '|] 
 
let getNumber msg = 
    printfn msg 
    Int32.Parse(Console.ReadLine()) 
 
let CompareHands key attempt = 
    let zp = Seq.zip key attempt |> Seq.groupBy String.Equals |> dict 
    let oC = if zp.ContainsKey true then Seq.length zp.[true] else 0 
    let kC = if zp.ContainsKey false 
               then Seq.length 
                      [for (kk, kc) in Seq.countBy fst zp.[false] do 
                       for (ak, ac) in Seq.countBy snd zp.[false] do 
                          if kk.Equals ak then 
                              yield Math.Min(kc, ac)] 
               else 0 
    (oC, kC) 
 
let doGame maxplays symbols key = 
  let mutable turn = 0 
  let mutable playing = true 
  while playing do 
    turn <- turn + 1 
    printf "Turn %d of %d\n" turn maxplays 
    let (oC, kC) = CompareHands key (getSymbols ()) 
    printf ">>>>>>>>>>>>>>> (%d/%d)\n" oC kC 
    playing <- oC <> Seq.length key && turn < maxplays 
  turn < maxplays 
 
let main = 
  let symbols = getSymbols () 
  let holes = getNumber "Enter nº of holes:" 
  let rnd = new Random() 
  let key = [| for _ in [1..holes] -> symbols.[rnd.Next(symbols.Length)] |] 
  do 
    let result = doGame (getNumber "Enter max attempts:") symbols key 
    printfn "You %s" (if result then "Win" else "Lose") 
 
do main 

Comenta la solución

Tienes que identificarte para poder publicar tu comentario.