Doğa Koçak
Doğa Koçak

Reputation: 63

Maze problem solution is not working with Racket Lang

#lang racket

(define (buildMaze rows)
 (cond
((null? rows) null)
(else (cons (buildMaze (cdr rows))
 (car rows)))))

(define sampleMaze (buildMaze
'(("S" "-" "-" "-" "-")
 ("E" "-" "-" "-" "-")
 ("E" "E" "E" "E" "E")
 ("-" "-" "-" "-" "E")
 ("-" "-" "-" "-" "F"))
)
 )

(define (getHeight maze)
 (if (null? maze)
  0
  (+ 1 (getHeight (car maze)))))


(define (getWidth row)
 (if (null? row)
   0
   (+ (if (list? (car row)) 0 1)
     (getWidth (cdr row)))))

(define (getChar cell col)
 (cond
  ((string? cell) cell)
  ((pair? cell) (getLetter cell 0 col)) ; Adjust the recursive call to use getLetter
  (else -1)))

(define (getLetter maze row col)
 (cond
  ((and (<= 0 row (- (getHeight maze) 1))
     (<= 0 col (- (getWidth (car maze)) 1)))
   (getChar (list-ref maze row) col))
  (else -1)))

(display "Height: ")
(display (getHeight sampleMaze))
(newline)

(display "Width: ")
(display (getWidth sampleMaze))
(newline)

(display "Letter at (0, 0): ")
(display (getLetter sampleMaze 0 0))
(newline)

(display "Letter at (1, 0): ")
(display (getLetter sampleMaze 1 0))
(newline)

(display "Letter at (1, 1): ")
(display (getLetter sampleMaze 1 1))
(newline)

(display "Letter at (4, 4): ")
(display (getLetter sampleMaze 4 4))
(newline)

this code is returning

Letter at (0, 0): -1
Letter at (1, 0): S
Letter at (1, 1): S
Letter at (4, 4): -

but it should be

(getLetter sampleMaze 0 0) → should return S 
(getLetter sampleMaze 1 0) → should return E 
(getLetter sampleMaze 1 1) → should return - 
(getLetter sampleMaze 4 4) → should return F

Function "getLetter" which takes a maze, a row number and a column number. Then it returns the letter from the maze on the corresponding location [row, column]

and also

Define a function "solveMaze" which takes a maze and returns the solution for the maze. (solveMaze sampleMaze) → should return (D D R R R R D D)

how can i do

I tried all of AI tools and Racket libraries but i could not do this.

Upvotes: 0

Views: 68

Answers (1)

molbdnilo
molbdnilo

Reputation: 66459

It's difficult to guess the point of the getLetter/getChar pair, but overall you're doing too much and not using the available list functions.

I made the following changes and got the expected output:

(define sampleMaze
'(("S" "-" "-" "-" "-")
 ("E" "-" "-" "-" "-")
 ("E" "E" "E" "E" "E")
 ("-" "-" "-" "-" "E")
 ("-" "-" "-" "-" "F"))
)

(define (getHeight maze)
   (length maze))

(define (getWidth maze)
   (length (car maze)))
  
(define (getLetter maze row col)
    (if (and (< row (getHeight maze)) (< col (getWidth maze)))
          (list-ref (list-ref maze row) col)
         -1))

Upvotes: 0

Related Questions