Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module DomainTypes =
- type Coord = { x: int; y: int }
- type Entity =
- { move: Entity -> Entity
- coord: Coord }
- module SquareMover =
- open DomainTypes
- // Should this be an enum? Consts?
- let north = { Coord.x = 0; Coord.y = 1 }
- let east = { Coord.x = 1; Coord.y = 0 }
- let south = { Coord.x = 0; Coord.y = -1 }
- let west = { Coord.x = -1; Coord.y = 0 }
- // Should this be some sort of pattern match instead?
- let movementMap =
- Map [ (north, east)
- (east, south)
- (south, west)
- (west, north) ]
- type MovementState =
- { stepsInDirection: int
- direction: Coord }
- // How do I make `move` none recursive? How is this meant to be done without a class?
- let rec move movementState entity =
- let newMovementState =
- { stepsInDirection = (movementState.stepsInDirection + 1) % 5
- direction =
- if movementState.stepsInDirection = 4 then
- Map.find movementState.direction movementMap
- else
- movementState.direction }
- { entity with
- coord =
- { Coord.x = entity.coord.x + movementState.direction.x
- Coord.y = entity.coord.y + movementState.direction.y }
- move = move newMovementState }
- let createSquareMover coord =
- { Entity.move =
- move
- { stepsInDirection = 0
- direction = north }
- Entity.coord = coord }
- module Game =
- let maxY = 5
- let maxX = 5
- let display (entities: DomainTypes.Entity []) =
- let entitiesMap =
- entities
- |> Array.map (fun entity -> (entity.coord, entity))
- |> Map.ofArray
- printfn "--------------------------------"
- for y in 0..maxY do
- for x in 0..maxX do
- if
- Map.containsKey
- { DomainTypes.Coord.x = x
- DomainTypes.Coord.y = maxY - y }
- entitiesMap then
- printf "x"
- else
- printf " "
- printfn ""
- let rec loop stepsLeft (entities: DomainTypes.Entity []) =
- display entities
- if stepsLeft < 0 then
- 0
- else
- let newEntities = (entities |> Array.map (fun e -> e.move (e)))
- loop (stepsLeft - 1) newEntities
- open DomainTypes
- [<EntryPoint>]
- let main args =
- let maxSteps = 20
- let entities =
- [| SquareMover.createSquareMover { Coord.x = 0; Coord.y = 0 }
- SquareMover.createSquareMover { Coord.x = 3; Coord.y = 2 } |]
- Game.loop maxSteps entities
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement