Question

This is in CLojure, first is my code:

;cell structure
(defstruct cell :x :y :highland :lowland :obstacle :ammo :tank)

;demension
(def dim 0)

(defn setdim [num]
  (def dim num))

;create world
(defn creatworld []
  (apply vector
         (map (fn [_]
                (apply vector (map (fn [_] (struct cell))
                                   (range dim))))
              (range dim)))) 

;initiate coordinate for structure in vector of vector
;this is not working
(defn inicoor [world]
  (map
   #(assoc % :x i :y j)
   world))

(defn inicoor [world]
  (dorun (for [i (range 0 dim)]
           (dorun (for [j (range 0 dim)] 
                    (map  
                    # (assoc (nth (nth world i) j) :x i :y j)))))))

So, what I'm doing is I try to create a 2d world using a 2d vector of structure. After create world, I wish to initiate the x y coordinate to be the actual coordinate, as I tried in the last function. However, since clojure is variable immutable, it's not going to change the value... And it also not return a 2d vector of new data... Then I tried to use map... but I'm really new to clojure... didn't work after few try...

Can anyone tell me how to do it? Thanks a lot...

add: The target structure is like this:

[  00 10 20 30 40 50   ]  (this is the first vector)
   01 11 21 31 41 51
   02 12 22 32 42 52
   03 13 23 33 43 53
   04 14 24 34 44 54
   05 15 25 35 45 55

That's why I used a nest loop at first... The most common way to do this in java...

Was it helpful?

Solution

With normal maps you could say:

(for [y (range 8) x (range 8)] {:x x :y y})

to get the cells with their coordinates in one big list

to update them (assuming you have a fn update-cell [cell]:

 (map update-cell cells)

or if you had some data to update them with:

(map update-cell cells (repeat data))
  • that data could be stuff like {:t the-time :dt ms-since-;last-update :etc other-stuff}

If you want them in a 2d grid, then you could do:

(partition  8 (for [y (range 8) x (range 8)] {:x x :y y}))

or, say you had a (defn make-cell [x y] {:x x :y y}), you could make them:

(map (fn [y] (map (fn [x] (make-cell x y)) (range width))) (range height))

then to update them:

(map (partial map update-cell) cells)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top