Advertisement
Guest User

Untitled

a guest
Jul 21st, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.36 KB | None | 0 0
  1. // Learn more about F# at http://fsharp.org
  2.  
  3. open System
  4.  
  5. type Order =
  6. | North
  7. | South
  8. | East
  9. | West
  10. | Start
  11. | CutEngine
  12.  
  13. type MineFieldTerrain =
  14. | Wall
  15. | Field
  16. | Mine
  17. | Robot
  18.  
  19. type minefield = MineFieldTerrain List List
  20.  
  21. type position = int*int
  22.  
  23. let CharToOptionMineFieldTerrain = function
  24. | '+' -> Some Wall
  25. | '0' -> Some Field
  26. | '*' -> Some Mine
  27. | 'M' -> Some Robot
  28. | _ -> None
  29.  
  30. let charToOptionOrder = function
  31. | 'N' -> Some North
  32. | 'S' -> Some South
  33. | 'E' -> Some East
  34. | 'O' -> Some West
  35. | 'I' -> Some Start
  36. | '-' -> Some CutEngine
  37. | _ -> None
  38.  
  39. let mineFieldToChar = function
  40. | Wall -> '+'
  41. | Field -> '0'
  42. | Mine -> '*'
  43. | Robot -> 'M'
  44.  
  45. let addTuple (a:position) (b:position) =
  46. ((fst a + fst b),(snd a + snd b))
  47.  
  48. let replaceListValue i value lis =
  49. lis |> List.mapi(fun j x -> if i = j then value else x)
  50.  
  51. let printGrid (mfield:minefield) =
  52. mfield |> List.fold( (fun acc x -> acc + (x |> List.fold(fun acc2 y -> acc2 + (mineFieldToChar y |> Char.ToString) ) "") + Environment.NewLine )) "" |>
  53. printfn "%s"
  54.  
  55. let redrawField (mField:minefield) (currentPosition:position) (nextPosition:position) =
  56. let newField = mField |> List.mapi(fun i x -> match i with
  57. | v when v = (fst currentPosition) -> replaceListValue (snd currentPosition) Field x
  58. | _ -> x
  59. ) |> List.mapi(fun i x -> match i with
  60. | v when v = (fst nextPosition) -> replaceListValue (snd nextPosition) Robot x
  61. | _ -> x )
  62.  
  63. printGrid newField
  64.  
  65.  
  66. let findStartPoistion (mfield:minefield) =
  67. let first = mfield |> List.findIndex(fun x -> x |> List.exists(fun y-> y = Robot))
  68. let second = mfield |> List.item first |> List.findIndex(fun y-> y = Robot)
  69. (first,second)
  70.  
  71. let clearMineField (mField:minefield) =
  72. mField |> List.map(fun x -> x |> List.map(fun y -> if y = Robot then Field else y))
  73.  
  74. let executeMoves (mfield:minefield) (moves:position list) =
  75. let startposition = mfield |> findStartPoistion
  76. let newField = mfield |> clearMineField
  77. let rec proccessMove (mField:minefield) (moves:position list) (currentPosition:position) =
  78. match (moves |> List.tryHead) with
  79. | None -> match (snd currentPosition) = ((mField |> List.item (fst currentPosition) |> List.length) - 1) with
  80. | true -> printfn "Escaped"
  81. | false -> printfn "Alive but lost"
  82. | Some moveDirection -> let nextPosition = addTuple currentPosition moveDirection
  83. let nextPositionTerrain = mField |> List.item (fst nextPosition) |> List.item (snd nextPosition)
  84. match nextPositionTerrain with
  85. | Wall -> proccessMove mField (moves |> List.tail) currentPosition
  86. | Field -> redrawField mField currentPosition nextPosition
  87. proccessMove mField (moves |> List.tail) nextPosition
  88. | Mine -> printfn "RIP bot"
  89. | _ -> proccessMove mField (moves |> List.tail) currentPosition
  90.  
  91. proccessMove (newField:minefield) (moves:position list) startposition
  92.  
  93.  
  94. let createMoves (orders:Order list) =
  95.  
  96. let rec translateOrders (orders:Order list) (moves:position list) engine =
  97. match orders |> List.tryHead with
  98. | None -> moves |> List.rev
  99. | Some currentOrder ->
  100. match currentOrder with
  101. | North when engine -> translateOrders (orders |> List.tail) ((-1,0)::moves) engine
  102. | South when engine -> translateOrders (orders |> List.tail) ((1,0)::moves) engine
  103. | East when engine -> translateOrders (orders |> List.tail) ((0,1)::moves) engine
  104. | West when engine -> translateOrders (orders |> List.tail) ((0,-1)::moves) engine
  105. | Start when not engine -> translateOrders (orders |> List.tail) moves true
  106. | CutEngine when engine -> translateOrders (orders |> List.tail) moves false
  107. | _ -> translateOrders (orders |> List.tail) moves engine
  108. translateOrders orders [] false
  109.  
  110.  
  111. ///Reads all the valid orders
  112.  
  113. let readOrders s =
  114. s |> Seq.choose( fun x -> charToOptionOrder x) |> Seq.toList
  115.  
  116.  
  117. let createMinefield () =
  118. let rec readeMineField (reader:unit ->string) (field:minefield) =
  119. match reader() with
  120. | null -> field |> List.rev // Since the recusrion makes the list appear out of order
  121. | nchar -> readeMineField reader ((nchar |> Seq.choose(fun x-> (CharToOptionMineFieldTerrain x)) |> Seq.toList) :: field)
  122. readeMineField Console.ReadLine []
  123.  
  124.  
  125. [<EntryPoint>]
  126. let main argv =
  127. printfn "Enter your minefield"
  128. let mine = (createMinefield())
  129. printfn "Enter your Orders"
  130. let moves = (readOrders (Console.ReadLine())) |> createMoves
  131. printfn "moves %A" moves
  132. executeMoves mine moves
  133. 0 // return an integer exit code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement