Advertisement
Guest User

Untitled

a guest
Jul 9th, 2022
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 2.78 KB | None | 0 0
  1. module DomainTypes =
  2.     type Coord = { x: int; y: int }
  3.  
  4.     type Entity =
  5.         { move: Entity -> Entity
  6.           coord: Coord }
  7.  
  8. module SquareMover =
  9.     open DomainTypes
  10.  
  11.     // Should this be an enum? Consts?
  12.     let north = { Coord.x = 0; Coord.y = 1 }
  13.     let east = { Coord.x = 1; Coord.y = 0 }
  14.     let south = { Coord.x = 0; Coord.y = -1 }
  15.     let west = { Coord.x = -1; Coord.y = 0 }
  16.  
  17.     // Should this be some sort of pattern match instead?
  18.     let movementMap =
  19.         Map [ (north, east)
  20.               (east, south)
  21.               (south, west)
  22.               (west, north) ]
  23.  
  24.     type MovementState =
  25.         { stepsInDirection: int
  26.           direction: Coord }
  27.  
  28.     // How do I make `move` none recursive? How is this meant to be done without a class?
  29.     let rec move movementState entity =
  30.         let newMovementState =
  31.             { stepsInDirection = (movementState.stepsInDirection + 1) % 5
  32.               direction =
  33.                 if movementState.stepsInDirection = 4 then
  34.                     Map.find movementState.direction movementMap
  35.                 else
  36.                     movementState.direction }
  37.  
  38.         { entity with
  39.             coord =
  40.                 { Coord.x = entity.coord.x + movementState.direction.x
  41.                   Coord.y = entity.coord.y + movementState.direction.y }
  42.             move = move newMovementState }
  43.  
  44.     let createSquareMover coord =
  45.         { Entity.move =
  46.             move
  47.                 { stepsInDirection = 0
  48.                   direction = north }
  49.           Entity.coord = coord }
  50.  
  51.  
  52. module Game =
  53.     let maxY = 5
  54.     let maxX = 5
  55.  
  56.     let display (entities: DomainTypes.Entity []) =
  57.         let entitiesMap =
  58.             entities
  59.             |> Array.map (fun entity -> (entity.coord, entity))
  60.             |> Map.ofArray
  61.  
  62.         printfn "--------------------------------"
  63.  
  64.         for y in 0..maxY do
  65.             for x in 0..maxX do
  66.                 if
  67.                     Map.containsKey
  68.                         { DomainTypes.Coord.x = x
  69.                           DomainTypes.Coord.y = maxY - y }
  70.                         entitiesMap then
  71.                     printf "x"
  72.                 else
  73.                     printf " "
  74.  
  75.             printfn ""
  76.  
  77.     let rec loop stepsLeft (entities: DomainTypes.Entity []) =
  78.         display entities
  79.  
  80.         if stepsLeft < 0 then
  81.             0
  82.         else
  83.             let newEntities = (entities |> Array.map (fun e -> e.move (e)))
  84.             loop (stepsLeft - 1) newEntities
  85.  
  86. open DomainTypes
  87.  
  88. [<EntryPoint>]
  89. let main args =
  90.     let maxSteps = 20
  91.  
  92.     let entities =
  93.         [| SquareMover.createSquareMover { Coord.x = 0; Coord.y = 0 }
  94.            SquareMover.createSquareMover { Coord.x = 3; Coord.y = 2 } |]
  95.  
  96.     Game.loop maxSteps entities
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement