0

I'm making a little RL in haskell (with hscurses) and now, I write the code to make/print the dungeon.

The first thing I do is making a list/array with 'walls'
In python(v3) it would be like this:

def mk_list(x, y):        
    dungeon = [['#' for j in range(y)] for i in range(x)]

    return dungeon

And it would be printed like this:

import curses
def print_dungeon(window, x, y, dungeon):
    for i in range(x):
        for j in range(y):
        window.addstr(j, i, dungeon[x][y])
    window.refresh()

So my question is: How can I do this in haskell? I know there excist the module Data.Array but as I understand, they support only 2D arrays.
Also the array must be mutable because I must 'dig' the rooms and corridors in it later.

But my question is also that should I use arrays for it, or is a list better?

Thanks in advance!

3
  • "How should I design X?"-questions like this one are usually too broad to get good answers on this site. Also, you seem to have assumptions about how it'll work ("The array must be mutable") which I don't think are correct Commented Jul 8, 2016 at 14:21
  • 1
    Since you are asking this question, I recommend lists, even if they are inefficient. Commented Jul 8, 2016 at 14:46
  • I've personally done it with a map, using a pair (coordinates) as a key. It is simple, everything not in the map is a wall or blank stone, and they are easy to update and fold over. Commented Jul 8, 2016 at 16:05

1 Answer 1

2

Haskell supports n-dimensional arrays:

import Data.Array
import Data.Ix
import Control.Monad

main = do
  let myBounds = ((0,5,10),(7,8,12)) :: ((Int,Int,Int),(Int,Int,Int))
      threeDexample = array myBounds
                        [ (ijk, e) | ijk@(i,j,k) <- range myBounds,
                                     let e = i+j*10+k*100 ]

  forM_ (range myBounds) $ \ijk@(i,j,k) -> do
    putStrLn $ "value at " ++ show ijk ++ " = " ++ show (threeDexample ! ijk)

To mutate, either use Data.Array.MArray or use the (//) operation.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.