From e77c8b23c148ffaa5991da18c981e61db2a5e55c Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Mon, 1 Apr 2013 23:01:59 -0400 Subject: Initial commit Haskell version now has most of the functionality from the C++ version, except for color, and overworld generation is REALLY slow. --- Fallen/Point.hs | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 Fallen/Point.hs (limited to 'Fallen/Point.hs') diff --git a/Fallen/Point.hs b/Fallen/Point.hs new file mode 100644 index 0000000..321f3b0 --- /dev/null +++ b/Fallen/Point.hs @@ -0,0 +1,58 @@ +module Fallen.Point +( Point, + Direction(North, South, East, West), + directions, + distance, + stepInDirection, + opposite, + randomPoint, + dirToPoint +) where + import System.Random + + type Point = (Int, Int) + data Direction = North | South | East | West deriving (Show) + + -- directions :: [Direction] + directions = [North, South, East, West] + + -- distance :: Point -> Point -> Int + distance (x1, y1) (x2, y2) = ceiling . sqrt . fromIntegral $ (x1-x2)^2 + (y1-y2)^2 + + -- stepInDirection :: Point -> Direction -> Point + stepInDirection (x, y) dir = case dir of + North -> (x, y-1) + South -> (x, y+1) + East -> (x+1, y) + West -> (x-1, y) + + -- opposite :: Direction -> Direction + opposite dir = case dir of + North -> South + South -> North + East -> West + West -> East + + -- randomPoint :: Int -> Int -> Int -> Int -> IO Point + randomPoint minX minY maxX maxY rg = do + let (x,rg1) = randomR (minX, maxX) rg :: (Int,StdGen) + let (y,rg2) = randomR (minY, maxY) rg1 :: (Int,StdGen) + ((x,y),rg2) + + -- dirToPoint :: Point -> Point -> [Direction] + dirToPoint p1 p2 = horizDirToPoint p1 p2 ++ vertDirToPoint p1 p2 where + horizDirToPoint (x1,y1) (x2,y2) = case compare x1 x2 of + LT -> [East] + EQ -> [] + GT -> [West] + vertDirToPoint (x1,y1) (x2,y2) = case compare y1 y2 of + LT -> [North] + EQ -> [] + GT -> [South] + + instance Eq Direction where + North == North = True + South == South = True + East == East = True + West == West = True + _ == _ = False -- cgit 1.4.1