1  using System;
   2  using System.IO;
   3  
   4  class Maze {
   5      public Maze(int numRows, int numCols) {
   6          C_ = numCols;  // X coordinate 
   7          R_ = numRows;  // Y coordinate
   8          horizontal_ = new bool[C_,   R_+1];
   9          vertical_   = new bool[C_+1, R_];
  10          }
  11  
  12      // ------------------- Write Maze in hex format -------------------
  13      public void write(TextWriter tw) {
  14          for (int y = R_-1; y>=0; --y) tw.WriteLine(getLine(y));
  15          }
  16  
  17      private string getLine(int y) {
  18          string result = "";           
  19          for(int x = 0; x<C_; ++x) result += getRoom(x, y);
  20          return result;
  21          }
  22  
  23      private char getRoom(int x, int y) {
  24          return ".123456789abcdef"[
  25                    Convert.ToInt32(horizontal_[x,   y+1])  // North
  26              + 2 * Convert.ToInt32(horizontal_[x,   y])    // South
  27              + 4 * Convert.ToInt32(vertical_  [x,   y])    // West
  28              + 8 * Convert.ToInt32(vertical_  [x+1, y])    // East
  29              ];
  30          }
  31  
  32      //=============== Extra Credit: Write maze graphically =================
  33  
  34      public void writeGraphically(TextWriter tw) {
  35          tw.WriteLine(getTopOfFirstRow());
  36          for (int y = R_ - 1; y >= 0; --y) tw.WriteLine(getGraphicalLine(y));
  37      }
  38  
  39      private string getTopOfFirstRow() {
  40          string result = "";
  41          for (int x = 0; x < C_; ++x) {result += " " + getHorizontalChar(x, R_);}
  42          return result;
  43          }
  44  
  45      private string getGraphicalLine(int y) {
  46          string result = "";
  47          for (int x = 0; x < C_; ++x) result += getVerticalChar(x, y) + getHorizontalChar(x, y);
  48          result += getVerticalChar(C_, y);
  49          return result;
  50      }
  51  
  52      private string getHorizontalChar(int x, int y) {return horizontal_[x, y]?" ":"_";}
  53      private string getVerticalChar  (int x, int y) {return vertical_  [x, y]?" ":"|";}
  54  
  55  
  56      // ======= Data Members ========
  57      public int R_ { get; private set; }
  58      public int C_ { get; private set; }
  59  
  60      // Paths are all blocked (false) by default:
  61      public bool[,] horizontal_ { get; set; }
  62      public bool[,] vertical_   { get; set; }
  63  }
  64