Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8669929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:35:40+00:00 2026-06-12T18:35:40+00:00

After see this old article of making a perfect maze generation (tile based version)

  • 0

After see this old article of making a perfect maze generation (tile based version) in PHP I tried to translate the original PHP code to Ruby. After a few tries, I can’t see where I’m making a mistake.

Here’s my Ruby code:

maze_width = 9
maze_height = 7
moves = []
width = 2*maze_width +1
height = 2*maze_height +1
maze = Hash.new{|h, k| h[k] = []}

for x in 0..height-1
    for y in 0..width-1
        maze[x][y] = 1
    end
end

x_pos = 1
y_pos = 1
maze[x_pos][y_pos] = 0
moves.push(y_pos + (x_pos * width))
while(!moves.empty?)
    possible_directions = ""
    # puts "x_pos: #{x_pos} y_pos: #{y_pos}"
    if(maze[x_pos+2][y_pos]== 1 and x_pos+2!=0 and x_pos+2!=height-1)
        possible_directions += "S"
    end
    if(maze[x_pos-2][y_pos]== 1 and x_pos-2!=0 and x_pos-2!=height-1)
        possible_directions += "N"
    end
    if(maze[x_pos][y_pos-2]== 1 and y_pos-2!=0 and y_pos-2!=width-1)
        possible_directions += "W"
    end
    if(maze[x_pos][y_pos+2]== 1 and y_pos+2!=0 and y_pos+2!=width-1)
        possible_directions += "E"
    end
    if(!possible_directions.empty?)
        move = rand(possible_directions.length)
        case(possible_directions[move].chr)
            when "N":
                maze[x_pos-2][y_pos] = 0;
                maze[x_pos-1][y_pos] = 0;
                x_pos -= 2;
            when "S":
                maze[x_pos+2][y_pos] = 0;
                maze[x_pos+1][y_pos] = 0;
                x_pos += 2;
            when "W":
                maze[x_pos][y_pos-2] = 0;
                maze[x_pos][y_pos-1] = 0;
                y_pos -= 2;
            when "E":
                maze[x_pos][y_pos+2] = 0;
                maze[x_pos][y_pos+1] = 0;
                y_pos += 2;
        end
        moves.push(y_pos+(x_pos*width))

    else 
        back = moves.pop
        x_pos = (back/width).floor
        y_pos = back%width
    end
end
# draw the maze

for x in 0..height-1
    for y in 0..width-1
        print((maze[x][y] == 1) ? "#" : " ")
    end
    puts
end

If I execute that, it shows a ugly and non perfect maze:

###################
# #               #
#    #         #   
#                  
##       #   #     
#     #         #  
#          # #     
        #          
#              #   
  #       #        
##       #   #     
    # #     #      
#        #         
              #    
###################

This is really different from the PHP output:

###################
#   # #           #
### # # ##### ### #
# # # #   # #   # #
# # # ### # ### ###
# # #   #     #   #
# # ### ##### ### #
# #   #         # #
# ### ########### #
# #   #         # #
# # ### ####### # #
# #     #   #   # #
# ####### # # ### #
#         #       #
###################
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T18:35:41+00:00Added an answer on June 12, 2026 at 6:35 pm

    When translating code between languages, beware of nit-picky semantic differences. The one that’s biting you here is that negative indexes in Ruby count back from the end of the array, rather than returning null. The maze code doesn’t have any range checks to see if x_pos or y_pos are moving off the end of the array; it just relies on the array returning null if asked for nonexistent elements. The end result is that you’re allowing moves off the left and top and getting nonsensical results.
    You’ll also get nil exceptions going off the right and bottom if the randomizer hits right, so you want checks on those, too.

    So, on the four directional checks, add some range code. e.g, turn this:

    if(maze[x_pos+2][y_pos]== 1 and x_pos+2!=0 and x_pos+2!=height-1)
        possible_directions += "S"
    end
    if(maze[x_pos-2][y_pos]== 1 and x_pos-2!=0 and x_pos-2!=height-1)
        possible_directions += "N"
    end
    

    into this:

    if(x_pos+2 < height and maze[x_pos+2][y_pos]== 1 and x_pos+2!=0 and x_pos+2!=height-1)
        possible_directions += "S"
    end
    if(x_pos-2 > 0 and maze[x_pos-2][y_pos]== 1 and x_pos-2!=0 and x_pos-2!=height-1)
        possible_directions += "N"
    end
    

    and likewise for the y_pos ones and width.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After about 90 seconds I see this error in my apache error log. I'm
Update: After some more reading I see that this problem is totally general, you
How can I resolved this error that I see in Gradle after upgrading to
OLD QUESTION, SEE BELOW FOR THE UPDATED VERSION My development environment is not the
After trying to make a while(bool) loop and it failing because I couldn't see
After running the following SQL statements, you will see that, MySQL has automatically created
After some desperate assistance regarding prototype.js on Magento. Please see the below product page
After having my last question answered , I have never see the preventDefault(); function
Solution: API 11 is needed see answer below! Easy Question: After downloading a File
I wrote a simple test program for opencv to see if it's working after

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.