Oto jest To mój pierwszy program w języku F #. Jeśli przegapiłem jakąś funkcję tego języka, powiadom mnie, gdy się uczę.
Oto moje przykładowe dane wejściowe
. . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . B . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . A . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . C . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . G . . . . .
. . . . . . . D . . . . . . . . . . . . . . . . .
. . . . . . . . F . . . . . . . . . . . . . . . .
. . . . . . . E . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . .
Oto wynik
. . . . . . . . . a b . . . . . . . b g . . . . .
. . . . . . . . . a b . B . . . b b b g . . . . .
. . . . . . . . . . a b . . . b c c c g . . . . .
. . . . . . . . A . . a b . b c . . c g . . . . .
. . . . . . . . . . . a b b c . . . c g . . . . .
a a a a a a a a . . . a b c . . C . c g . . . . .
d d d d d d d d a a a a b c . . . c g . . . . . .
. . . . . . . . d d d d b c . . c g . G . . . . .
. . . . . . . D d d d d d c . . c g . . . . . . .
d d d d d d d d f f f f f f c . c g . . . . . . .
e e e e e e e e e e e e e e c . c g . . . . . . .
. . . . . . . . . . . . . e c . c g . . . . . . .
. . . . . . . . . . . . . e c . c g . . . . . . .
. . . . . . . . . . . . . e c . c g . . . . . . .
Oto kod. Cieszyć się.
// The first thing that we need is some data.
let originalData = [
"........................."
"............B............"
"........................."
"........A................"
"........................."
"................C........"
"........................."
"...................G....."
".......D................."
"........F................"
".......E................."
"........................."
"........................."
"........................."
]
Teraz musimy przekonwertować te dane na tablicę dwuwymiarową, abyśmy mogli uzyskać do nich dostęp za pośrednictwem indeksatorów.
let dataMatrix =
originalData
|> List.map (fun st -> st.ToCharArray())
|> List.toArray
// We are going to need a concept of ownership for each
// cell.
type Owned =
| Unclaimed
| Owner of char
| Claimed of char
| Boundary of char
Stwórzmy macierz reprezentującą własność każdej komórki
let claims =
dataMatrix
|> Array.map (fun row ->
row
|> Array.map (function
| '.' -> Owned.Unclaimed
| ch -> Owned.Owner(ch))
)
Pozwól nam skorzystać z metody użyteczności, aby zobaczyć, co się stało.
let printIt () =
printfn ""
claims
|> Array.iter (fun row ->
row |> Array.iter (function
| Owned.Claimed(ch) -> printf " ."
| Owned.Owner(ch) -> printf " %c" ch
| Owned.Boundary(ch) -> printf " %c" ch
| _ -> printf " ." )
printfn "")
Stwórzmy zapis, który reprezentuje miejsce, w którym znajduje się konkretna litera.
type CapitalLocation = { X:int; Y:int; Letter:char }
Teraz chcemy znaleźć wszystkie wielkie litery.
let capitals =
dataMatrix
|> Array.mapi (fun y row ->
row
|> Array.mapi (fun x item ->
match item with
| '.' -> None
| _ -> Some({ X=x; Y=y; Letter=item }))
|> Array.choose id
|> Array.toList
)
|> Array.fold (fun acc item -> item @ acc) List.empty<CapitalLocation>
|> List.sortBy (fun item -> item.Letter)
Kiedy się poruszamy, potrzebujemy koncepcji kierunku.
type Direction =
| Left = 0
| Up = 1
| Right = 2
| Down = 3
// Function gets the coordinates of the adjacent cell.
let getCoordinates (x, y) direction =
match direction with
| Direction.Left -> x-1, y
| Direction.Up -> x, y-1
| Direction.Right -> x+1, y
| Direction.Down -> x, y+1
| _ -> (-1,-1) // TODO: Figure out how to best throw an error here.
W miarę przemieszczania się będziemy musieli wiedzieć o rozmiarze. Pomoże nam to monitorować, czy wychodzimy poza granice.
type Size = { Width:int; Height: int }
// Get the size of the matrix.
let size = {Width=originalData.Head.Length; Height=originalData.Length}
Aktywny wzorzec: pasuje do kryteriów danej komórki.
let (|OutOfBounds|UnclaimedCell|Claimed|Boundary|) (x,y) =
match (x,y) with
| _,_ when x < 0 || y < 0 -> OutOfBounds
| _,_ when x >= size.Width || y >= size.Height -> OutOfBounds
| _ ->
match claims.[y].[x] with
| Owned.Unclaimed -> UnclaimedCell(x,y)
| Owned.Claimed(ch) -> Claimed(x,y,ch)
| Owned.Boundary(ch) -> Boundary(x,y,ch)
| Owned.Owner(ch) -> Claimed(x,y,ch)
Teraz dochodzimy do podatku od mosiądzu. To twierdzi komórkę!
let claimCell letter (x, y) =
// Side effect: Change the value of the cell
(claims.[y].[x] <- Owned.Claimed (System.Char.ToLower letter)) |> ignore
Korzystając z aktywnego wzorca, przejmij tę komórkę, jeśli nie została odebrana, i zwróć współrzędne sąsiednich komórek.
let claimAndReturnAdjacentCells (letter, coordinates, direction) =
match coordinates with
| UnclaimedCell (x,y) ->
// Claim it and return the Owned object.
claimCell letter coordinates // meaningful side effect
// use Direction as int to allow math to be performed.
let directionInt = int direction;
Some(
// [counter-clockwise; forward; clockwise]
[(directionInt+3)%4; directionInt; (directionInt+1)%4]
|> List.map enum<Direction>
|> List.map (fun newDirection ->
(
letter,
getCoordinates coordinates newDirection,
newDirection
))
)
| Claimed(cx,cy,cch) when cch <> System.Char.ToLower letter->
// If we find a "Claimed" element that is not our letter, we have
// hit a boundary. Change "Claimed" to "Boundary" and return the
// element that led us to evaluating this element. It is also a
// boundary.
(claims.[cy].[cx] <- Owned.Boundary (System.Char.ToLower cch)) |> ignore
let reverseDirection = enum<Direction>(((int direction)+2)%4)
Some[(
cch,
getCoordinates (cx, cy) reverseDirection,
reverseDirection
)]
| _ -> None
Zaczynamy tworzyć listy tej torby danych, pozwól nam stworzyć typ, aby wszystko było bardziej zrozumiałe.
type CellClaimCriteria = (char * (int * int) * Direction)
Biorąc pod uwagę listę kryteriów zgłaszania roszczeń do komórek, przejdziemy do następnej listy, zwracając kolejne komórki, aby zgłosić roszczenia i powrócić do tej listy.
let rec claimCells (items:CellClaimCriteria list) =
items
|> List.fold (fun acc item ->
let results = claimAndReturnAdjacentCells item
if Option.isSome(results)
then (acc @ Option.get results)
else acc
) List.empty<CellClaimCriteria>
|> (fun l ->
match l with
| [] -> []
| _ -> claimCells l)
Dla każdego kapitału utwórz kryteria roszczenia w każdym kierunku, a następnie rekurencyjnie przeszukaj te komórki.
let claimCellsFromCapitalsOut ()=
capitals
|> List.fold (fun acc capital ->
let getCoordinates = getCoordinates (capital.X, capital.Y)
[Direction.Left; Direction.Up; Direction.Right; Direction.Down]
|> List.map (fun direction ->
(
capital.Letter,
getCoordinates direction,
direction
))
|> (fun items -> acc @ items)) List.empty<CellClaimCriteria>
|> claimCells
Każdy program wymaga głównego.
[<EntryPoint>]
let main args =
printIt()
claimCellsFromCapitalsOut()
printIt()
0