Help needed on clojure

51 views
Skip to first unread message

JohnP

unread,
May 27, 2019, 11:43:26 AM5/27/19
to 4Clojure

A map of the tunnels will be stored in a simple text file. The map is used by the application to determine if the explorer can move in a certain direction. The map could look like this:
---#--###----
-#---#----##-

####-#-#-#-##
---#---#-#---
-#-####---##-
-#------#----
-############
------------@
Here, the
characters indicate that you are free to move in this direction. The # character indicates that you cannot move any further in this direction and you should go somewhere else. The @ character indicates the location of the treasure. In this case, it is in the bottom right corner, but it could be anywhere in the map. In your application, you will always begin searching in the top left corner of the map. So when you run your code, you might print something like the following to the screen:

I found the treasure :-) 

+++#--###--#- 

!#+++#+++-##- 

####+#+#+#!## 

+++#+++#+#!!! 

+#+####++!##! 

+#++++++#!!!! 

+############ 

++++++++++++@


In this case, we were successful. You will also see that the map has been updated to indicate how the walk was done. The + characters indicate the path that led to the treasure. The ! characters indicate the tunnels that were tried but did not to lead to a viable path. Note, for example, that after starting in the top left corner, the explorer tried to go down but that path was a dead end. So a ! was used to mark a bad path and then the explorer went to the right, which eventually lead to the treasure.


Let’s modify the input file and try another treasure hunt. 

---#--###--#@
-#---#----##-
####-#-#-#-##

---#---#-#--- 

-#-####---##- 

-#------#---- 

-############ 

-------------

Uh oh, I could not find the treasure :-( 

!!!#!!###!!#@
!#!!!#!!!!##-
####!#!#!#!##

!!!#!!!#!#!!! 

!#!####!!!##! 

!#!!!!!!#!!!! 

!############ 

!!!!!!!!!!!!!

In this case, there was no way to get to the treasure. The ! characters indicate that we tried almost every possible pathway but eventually had to give up (there is one – location that we couldn’t reach). Please help, anyone!



Eric Clack

unread,
May 28, 2019, 8:59:31 AM5/28/19
to 4Clojure
Hi John, 
Looks like an interesting problem. 

I can't tell from your email whether you are struggling with some aspect of Clojure, or struggling with the correct algorithm to solve the problem, or perhaps both! Could you tell us what you've tried, with code examples, and then we might be able to suggest something?

Best regards, 
-Eric.

Ubor Ufuoma

unread,
May 28, 2019, 10:45:33 AM5/28/19
to 4clo...@googlegroups.com
Hi Eric, thanks for the comment. Thing is I solved problem using Java but need code in clojure as I'm an amature in clojure. Please find Java code below:



import java.util.ArrayList;

import java.util.Scanner;

import java.io.File;

import java.io.IOException;


public class Maze

{

private static char[][] maze;

private static int startrow, startcol, finishrow, finishcol;

private static ArrayList<String> mazeBuffer;


public static void initializeMaze(String fileName)

{

System.out.println("This is my challenge ");

}


public static void printMaze(char[][] maze)

{

for (char[] row: maze)

{

for (char c: row)

System.out.print(c);

System.out.println();

}

System.out.println();


}


public static void main (String[] args)

{

char[][] maze = {

{'S','-','-','#','-','-','#','#','#','-','-','-','-'},

{'-','#','-','-','-','#','-','-','-','-','#','#','-'},

{'#','#','#','#','-','#','-','#','-','#','-','#','#'},

{'-','-','-','#','-','-','-','#','-','#','-','-','-'},

{'-','#','-','#','#','#','#','-','-','-','#','#','-'},

{'-','#','-','-','-','-','-','-','#','-','-','-','-'},

{'-','#','#','#','#','#','#','#','#','#','#','#','#'},

{'-','-','-','-','-','-','-','-','-','-','-','-','@'}

};

startrow = startcol = 0;

finishrow = finishcol = -1;

initializeMaze("simplemaze.dat");

printMaze(maze);

if (solveMaze(startrow, startcol, maze)) {

System.out.println("Woo hoo, I found a solution ");

System.out.println(" ");

printMaze(maze);

}

else {

System.out.println("Unsolvable.");

printMaze(maze);

}

}


public static boolean solveMaze(int r, int c, char[][] maze) {

//check for boundaries

if(r < 0 || c < 0 || r >= maze.length || c >= maze[0].length)

return false;

//base case, did i reach the finish?

if(maze[r][c] == '@')

return true;

//check if I'm at a valid point

if(maze[r][c] != '-' && maze[r][c] != 'S')

//I have hit a wall, not a valid solution

return false;

//must be on a path, may be the right path

//leave a bread crumb

maze[r][c] = '!';

//check above

if(solveMaze(r-1,c, maze)) {

maze[r][c] = '+';

return true;

}

//check below

if(solveMaze(r+1,c, maze)) {

maze[r][c] = '+';

return true;

}

//check left

if(solveMaze(r,c-1, maze)) {

maze[r][c] = '+';

return true;

}

//check right

if(solveMaze(r,c+1, maze)) {

maze[r][c] = '+';

return true;

}

//if I reach this point...

return false;

}


}


--
You received this message because you are subscribed to the Google Groups "4Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to 4clojure+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/4clojure/4a64a4f4-67f0-4477-bf12-b45a4d666ef4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Eric Clack

unread,
May 28, 2019, 11:39:58 AM5/28/19
to 4Clojure
Hi John, 
So what have you tried in Clojure?
To unsubscribe from this group and stop receiving emails from it, send an email to 4clo...@googlegroups.com.

Ubor Ufuoma

unread,
May 28, 2019, 11:57:24 AM5/28/19
to 4clo...@googlegroups.com
Hi Eric,

I am trying to convert the java code to clojure code but getting lots of errors.

Please note: in the clojure code I am actually reading input from a text file

INPUT (file name "map.txt")
---#--###----
-#---#----##-
####-#-#-#-##
---#---#-#---
-#-####---##-
-#------#----
-############
------------@

(require '[clojure.string :as str])

(defn loadFile []
(def treasureMap (slurp "map.txt"))
(def mapVector (str/split treasureMap #"\n"))
(def hold (clojure.string/split-lines treasureMap))
(def b (to-array-2d [mapVector]))
)

(defn loadFile2 []
(with-open [rdr (clojure.java.io/reader "map.txt")]
(reduce conj [] (line-seq rdr)))
)

(defn welcomeMessage []
(def message "This is my new challenge :")
)

(defn get2dArray []
(def kk (loadFile2))
(def vectorArray (to-array-2d kk))
)

(defn validateMap [m]
(def head (count (str (m 0))))
(def valid true)
(println head)
(loop [y (- (count m) 1)]
;(println y)
(when (>= y 1)
(def check (= head (count (str (m y)))))
(if ( = check false)
(def valid false))
(recur (- y 1))))
(= valid true)
)

(defn outside_map []
)

(defn is_goal []

)

(defn not_open []

)

(defn solveMaze [r c m]
(def tt true)
(if (or (or (or (< r 0) (< c 0)) (>= r (count m))) (>= c (count (str (m 0)))))
(def che false)
(= tt che)
)

(if (= (get-in m[r c]) "@")
(= tt true)
)

(if (and (not= (get-in m[r c]) -) (not= (get-in m[r c]) S))
(def che false)
(= tt che)
)
(def temp_map (assoc-in (get-in m[r c]) "!"))

(if (= (solveMaze (dec r) c temp_map) true)
(def temp_map (assoc-in (get-in m[r c]) "+"))
(= tt true)
)

(if (= (solveMaze (inc r) c temp_map) true)
(def temp_map (assoc-in (get-in m[r c]) "+"))
(= tt true)
)

(if (= (solveMaze r (dec c) temp_map) true)
(def temp_map (assoc-in (get-in m[r c]) "+"))
(= tt true)
)

(if (= (solveMaze r (inc c) temp_map) true)
(def temp_map (assoc-in (get-in m[r c]) "+"))
(= tt true)
)
(= tt false)
)

(defn main [x y m]

(if (= (solveMaze x y m) true)
(println "I found a solution ")
(println "Unsolvable")
)
)

(println "")
(welcomeMessage)
(println message)
(println "")
(loadFile)
(println treasureMap)
(get2dArray)

(println (get-in mapVector[7 12]))

(main 0 0 kk)


To unsubscribe from this group and stop receiving emails from it, send an email to 4clojure+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/4clojure/f5c7d86d-e488-49b4-8107-71da57447b98%40googlegroups.com.

Ubor Ufuoma

unread,
May 28, 2019, 12:07:58 PM5/28/19
to 4clo...@googlegroups.com
One difficulty I am having is comparing if, @ equals @. Is there any clojure function to compare two symbols? for example 
(= @ @)

Eric Clack

unread,
May 29, 2019, 5:13:08 AM5/29/19
to 4Clojure
Hi John, 
Stack Overflow is a great place to find answers to these questions, e.g. https://stackoverflow.com/questions/13188140/clojure-equality-of-symbols

I would also recommend, if you are new to Clojure, that you read this book (free online) https://www.braveclojure.com/clojure-for-the-brave-and-true/

This is also a great site for short examples: https://clojuredocs.org/

Hope this helps,
-Eric. 
Reply all
Reply to author
Forward
0 new messages