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!

  • "How should I design X?"-questions like this one are usually [too broad](http://stackoverflow.com/help/closed-questions) 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 – Benjamin Hodgson Jul 08 '16 at 14:21
  • 1
    Since you are asking this question, I recommend lists, even if they are inefficient. – Alec Jul 08 '16 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. – David McHealy Jul 08 '16 at 16:05

1 Answers1

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.

ErikR
  • 51,541
  • 9
  • 73
  • 124