How to store .txt in a matrix ?

Hello, I have to create a function to read a maze from a .txt file.

My function is:

readMap <- function(path) {
  con = file(path, "r")
  while ( TRUE ) {
    line = readLines(con, n = 1)
    if ( length(line) == 0 ) {
      break
    }
    print(line)
  }
  
  close(con)
}

To read my maze:

readMap("~path")

And it gives me the maze line by line as:

[1] "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
[1] "s            x  x             x"
[1] "xxxxx   x  x x  x   x    x xxxx"
[1] "x       x  x x      xxxxxx    x"
[1] "x xxxxxxx  x xxxxx  x    xxxx x"
[1] "x       x                     x"
[1] "xxxxxxxxxxxxxxxxxxxxxxxxxx    x"
[1] "xxx        x                  x"
[1] "x   x  xxx x xxxxxxxxxxxxxxxxxx"
[1] "xxxxx  xxx x x                x"
[1] "x            x xxx xxxxxxxx xxx"
[1] "x xxxxxx  xxxx  xx x  x   x x x"
[1] "x               xx    x   x x x"
[1] "x  xxxxxx   xxxxxx xxxx       x"
[1] "x  x        x      x      x xxx"
[1] "x  x xxxx   x xxxxxxxxxx xx   x"
[1] "e xx        x      x      x   x"
[1] "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

How I can create the matrix with this output line by line? (x, spaces, e and s)

Do you need a 1 column matrix or a matrix with however many characters the longest row has?

I need a matrix (18x31) which will reproduce the maze exactly

Do you know how to split a string like this or any of the others into a vector of length 31?

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.