Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dhilst committed Sep 2, 2021
0 parents commit dd26681
Show file tree
Hide file tree
Showing 8 changed files with 308 additions and 0 deletions.
30 changes: 30 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Copyright Author name here (c) 2021

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.

* Neither the name of Author name here nor the names of other
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Langton Ant in Haskell
2 changes: 2 additions & 0 deletions Setup.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import Distribution.Simple
main = defaultMain
4 changes: 4 additions & 0 deletions hie.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
cradle:
stack:
- path: "./src/Main.hs"
component: "langtonant:exe:langtonant"
25 changes: 25 additions & 0 deletions langtonant.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: langtonant
version: 0.1.0.0
-- synopsis:
-- description:
homepage: https://github.com/dhilst/langtonant#readme
license: BSD3
license-file: LICENSE
author: Daniel Hilst Selli
maintainer: Daniel Hilst Selli
copyright: 2021 Author name here
category: Web
build-type: Simple
cabal-version: >=1.10
extra-source-files: README.md

executable langtonant
ghc-options: -threaded
hs-source-dirs: src
main-is: Main.hs
default-language: Haskell2010
build-depends: base >= 4.7 && < 5
, vty
, array
, process
, ansi-terminal
135 changes: 135 additions & 0 deletions src/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
module Main where

import Control.Concurrent
import Control.Monad
import Data.Array
import Data.Char
import Data.Function
import Debug.Trace
import System.Console.ANSI (clearScreen)
import qualified System.Process as SP

data Direction
= DUp
| DDown
| DLeft
| DRight
deriving (Show)

data Color
= Black
| White
deriving (Show)

type Pos = (Int, Int)

type Universe = Array Pos Color

data Ant =
Ant
{ pos :: Pos
, dir :: Direction
}
deriving (Show)

h = 50

w = 100

start = (h `div` 2, w `div` 2)

mkArray :: (Int, Int) -> Array (Int, Int) Color
mkArray (maxx, maxy) =
array
((0, 0), (maxy - 1, maxx - 1))
[((y, x), Black) | y <- [0 .. maxy - 1], x <- [0 .. maxx - 1]]

mkUniverse :: Universe
mkUniverse = mkArray (w, h)

mkAnt :: Ant
mkAnt =
let (y, x) = start
in Ant {pos = (y, x), dir = DUp}

-- sleep seconds
delay :: IO ()
delay =
let n = 3
in threadDelay (10000 * n)

-- clear terminal
clear :: IO ()
clear = do
_ <- SP.system "reset"
return ()

colorToChar :: Color -> Char
colorToChar Black = '.'
colorToChar White = '@'

printUniverse :: Universe -> IO ()
printUniverse m = do
forM_ (assocs m) $ \((y, x), cell) -> do
putChar . colorToChar $ cell
putChar '\n' & when (x == w - 1)

turnLeft :: Direction -> Direction
turnLeft DUp = DLeft
turnLeft DDown = DRight
turnLeft DLeft = DDown
turnLeft DRight = DUp

turnRight :: Direction -> Direction
turnRight DUp = DRight
turnRight DDown = DLeft
turnRight DLeft = DUp
turnRight DRight = DDown

turnAnt :: Color -> Direction -> Direction
turnAnt White dir = turnRight dir
turnAnt Black dir = turnLeft dir

flipColor :: Color -> Color
flipColor Black = White
flipColor White = Black

updateCell :: Pos -> Color -> Universe -> Universe
updateCell (y, x) color universe = universe // [((y, x), color)]

flipCell :: Pos -> Universe -> Universe
flipCell (y, x) universe =
let c = universe ! (y, x)
in updateCell (y, x) (flipColor c) universe

moveForward :: Direction -> (Int, Int) -> (Int, Int)
moveForward DLeft (y, x) = (y, (x - 1) `mod` w)
moveForward DRight (y, x) = (y, (x + 1) `mod` w)
moveForward DUp (y, x) = ((y - 1) `mod` h, x)
moveForward DDown (y, x) = ((y + 1) `mod` h, x)

moveAnt :: Color -> Ant -> Ant
moveAnt currentColor Ant {pos = (y, x), dir = dir} =
let newDir = turnAnt currentColor dir
(x', y') = moveForward newDir (y, x)
in Ant {pos = (x', y'), dir = newDir}

getCurrentCellColor :: Ant -> Universe -> Color
getCurrentCellColor Ant {pos = (y, x)} universe = universe ! (y, x)

stepSystem :: (Ant, Universe) -> (Ant, Universe)
stepSystem (ant@Ant {pos = pos}, universe) =
let currentCellColor = getCurrentCellColor ant universe
newAnt = moveAnt currentCellColor ant
in (newAnt, flipCell pos universe)

runSystem :: (Ant, Universe) -> Int -> IO ()
runSystem system@(ant, universe) step = do
clearScreen
printUniverse universe
print $ "Step: " ++ show step
delay
runSystem (stepSystem system) (step + 1)

main :: IO ()
main = runSystem (mkAnt, mkUniverse) 0
71 changes: 71 additions & 0 deletions stack.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# This file was automatically generated by 'stack init'
#
# Some commonly used options have been documented as comments in this file.
# For advanced use and comprehensive documentation of the format, please see:
# https://docs.haskellstack.org/en/stable/yaml_configuration/

# Resolver to choose a 'specific' stackage snapshot or a compiler version.
# A snapshot resolver dictates the compiler version and the set of packages
# to be used for project dependencies. For example:
#
# resolver: lts-3.5
# resolver: nightly-2015-09-21
# resolver: ghc-7.10.2
#
# The location of a snapshot can be provided as a file or url. Stack assumes
# a snapshot provided as a file might change, whereas a url resource does not.
#
# resolver: ./custom-snapshot.yaml
# resolver: https://example.com/snapshots/2018-01-01.yaml
resolver: lts-18.6

# User packages to be built.
# Various formats can be used as shown in the example below.
#
# packages:
# - some-directory
# - https://example.com/foo/bar/baz-0.0.2.tar.gz
# subdirs:
# - auto-update
# - wai
packages:
- .

# Dependency packages to be pulled from upstream that are not in the resolver.
# These entries can reference officially published versions as well as
# forks / in-progress versions pinned to a git hash. For example:
#
extra-deps:
- vty-5.33
- array-0.5.4.0
- process-1.6.9.0
- ansi-terminal-0.11
# - acme-missiles-0.3
# - git: https://github.com/commercialhaskell/stack.git
# commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a
#
# extra-deps: []

# Override default flag values for local packages and extra-deps
# flags: {}

# Extra package databases containing global packages
# extra-package-dbs: []

# Control whether we use the GHC we find on the path
# system-ghc: true
#
# Require a specific version of stack, using version ranges
# require-stack-version: -any # Default
# require-stack-version: ">=2.7"
#
# Override the architecture used by stack, especially useful on Windows
# arch: i386
# arch: x86_64
#
# Extra directories used by stack for building
# extra-include-dirs: [/path/to/dir]
# extra-lib-dirs: [/path/to/dir]
#
# Allow a newer minor version of GHC than the snapshot specifies
# compiler-check: newer-minor
40 changes: 40 additions & 0 deletions stack.yaml.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# This file was autogenerated by Stack.
# You should not edit this file by hand.
# For more information, please see the documentation at:
# https://docs.haskellstack.org/en/stable/lock_files

packages:
- completed:
hackage: vty-5.33@sha256:8da1652d64e24c9e011384628de340b2877f0148de7ef57baf66cd2e9a35c6c6,22294
pantry-tree:
size: 5393
sha256: f83f9245266d28f8521227cf551d6498a4ad7b22f1ac98c5992c287e6fc06d08
original:
hackage: vty-5.33
- completed:
hackage: array-0.5.4.0@sha256:2b2425e01d592c0e66594c008405ea4c6f6e4d929daeddabbd81be9bb49ebffa,1588
pantry-tree:
size: 1135
sha256: e17dfafde2f226674cffd56b1f445c332b6e5855860f79dedd9de0b6916e7ff0
original:
hackage: array-0.5.4.0
- completed:
hackage: process-1.6.9.0@sha256:372de796fea9d40e6d02269d5df466c9b8d641f145fad3c1add59db7baeea19e,2576
pantry-tree:
size: 1211
sha256: 9d21df864535dcc65e11fd108c4678fdadf2d53bb9ffd1fda142369c797f31c1
original:
hackage: process-1.6.9.0
- completed:
hackage: ansi-terminal-0.11@sha256:97470250c92aae14c4c810d7f664c532995ba8910e2ad797b29f22ad0d2d0194,3307
pantry-tree:
size: 1461
sha256: 4098c4b8bb503fcf0730aedc8247a479d3e22fd1fe4c6b4b2b2b5da30bdfb713
original:
hackage: ansi-terminal-0.11
snapshots:
- completed:
size: 587113
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/18/6.yaml
sha256: f74c482d7c93739ecf3abfbc0f2dea1c20a2dfb2462c689846ed55a9653b66f7
original: lts-18.6

0 comments on commit dd26681

Please sign in to comment.