small medium large xlarge

Posts by Diego Villamil

Generic-user-small
11 Jan 2016, 01:31
Diego Villamil (5 posts)
(from Mazes for Programmers > Unicode box characters in displaying maze)

Dear Jamis and Mazers, I have really enjoyed following the book. I am one who falls in the category of non-rubyists and this has been a very enjoyable way to introduce me to that language.

I took the challenge of displaying the maze with these unicode box characters. Yet, I feel like my code can be improved code folding. I wanted to share it for anybody willing to take a look and point me in the right direction.

def to_s
    hrz  =  "\u2500"  # ─
    vrt  =  "\u2502"  # │
    crnr =  "\u2518"  # ┘
    nswe = ["", "\u2576", "\u2574", "\u2500",     #     ╶  ╴   ─
      "\u2577", "\u250C", "\u2510", "\u252C",     # ╷   ┌  ┐   ┬
      "\u2575", "\u2514", "\u2518", "\u2534",     # ╵   └  ┘   ┴
      "\u2502", "\u251C", "\u2524", "\u253C"]     # │   ├  ┤   ┼

    aux_n = Cell.new(-1,-1)
    aux_w = Cell.new(-1,-1)
    aux_d = Cell.new(-1,-1)
    aux_n.link(aux_d)
    aux_w.link(aux_d)

    out = ""
    each_row do |row|
      top = ""
      mid = ""
      btm = ""
      for cell_ in row.each do
        cell_n = cell_.north ? cell_.north : aux_n
        cell_w = cell_.west  ? cell_.west  : aux_w
        cell_d = cell_n.west ? cell_n.west : aux_d

        # Edges referenced from NW corner of cell_
        edge_n = !cell_n.linked?(cell_d)
        edge_s = !cell_w.linked?(cell_)
        edge_w = !cell_w.linked?(cell_d)
        edge_e = !cell_n.linked?(cell_)
        edges = [edge_n, edge_s, edge_w, edge_e]
        key = edges.inject(0){|n, b| 2*n + (b ?1:0)}   
        # The key is the integer corresponding to the binary from `edges`
        # to "open the lock" in nswe and get the right element. 

        top << nswe[key] + hrz*3
        mid << (edge_s ? " " : vrt) + " #{contents_of(cell_)} "

        if cell_.row == @rows-1
          # Edges referenced from SW corner of cell_
          edge_n = edge_s
          edge_s = false
          edge_w = !cell_.west.nil?
          edge_e = true
          edges = [edge_n, edge_s, edge_w, edge_e]
          key = edges.inject(0){|n, b| 2*n + (b ?1:0)}

          btm << nswe[key] + hrz*3
        end
      end

      # Edges referenced from NE corner of cell_
      edge_n = !cell_.north.nil?
      edge_s = true
      edge_w = !cell_n.linked?(cell_)
      edge_e = false
      edges = [edge_n, edge_s, edge_w, edge_e]
      key = edges.inject(0){|n, b| 2*n + (b ?1:0)}

      top << nswe[key] + "\n"
      mid << vrt + "\n"
      out << top + mid + btm
    end

    out << crnr + "\n"
  end

I liked coming up with the vector nswe and (I did get help on) accessing it via a binary representation of the corresponding edges north, south, west, east.

Thank you kindly. Any help will be much appreciated.

Generic-user-small
11 Jan 2016, 01:56
Diego Villamil (5 posts)
(from Mazes for Programmers > Define []= method for Grid)

Dear Mazers, I wrote another post for printing the grid with unicode box characters. My second attempt to achieve this is to extend the grid with a one cell frame of “phantom” cells around the grid. With this frame of phantom cells I’d only need to run the loop once and all the edge printing will be taken care of.
The question that I have is how to implement the assignment method []= in my code.

I upload it in Github, so anybody that’s interested in it can take a look.

I’m using ruby -I. _binary.rb to run the mazes with the unicode to_s. The original is now to_s0 and my other attempt is to_s1. The error happens in line 170 of grid.rb.

Thanks a bunch.

Generic-user-small
11 Jan 2016, 20:01
Diego Villamil (5 posts)
(from Mazes for Programmers > Unicode box characters in displaying maze)

Thanks for your hints. It was indeed tricky to use those unicode characters; and it has really brought up a lot of good tricks from Ruby. I also saw another post about not using compact and I wanted to thank you for keeping it simple.

The aux_cell was a derivation on how you had implemented cell = Cell.new(-1,-1) unless cell, which was nice to understand as well.

I did come up with the two iterators for cell_ in row ... end and row.each do |cell_| ... end. I must admit it took me a few times to understand the .each do form, but that’s not the reason why I went back to using the for one. The reason that I went back to it is because after the loop I needed one extra step with the cell_ variable, and thus needed to keep in the workspace. =S

Regards from Mexico City.

Generic-user-small
13 Jan 2016, 12:58
Diego Villamil (5 posts)
(from Mazes for Programmers > Define []= method for Grid)

Following the aux_ cells, I was able to wrap it up in one loop with no need of code-after-the-loop. This made a fake extension, but I didn’t use the method [ ]=. It’s actually more similar to my first post.

In the Github link above, there’s to_s and to_s0 methods corresponding to this box unicode, and the original from the book.

5 posts