-1

PHPで完全な迷路生成(タイルベースのバージョン)を作成するというこの古い記事を見た後、元のPHPコードをRubyに変換しようとしました。何度か試しても、どこを間違えているのかわかりません。

これが私のRubyコードです:

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

私がそれを実行すると、それは醜くて完璧ではない迷路を示しています:

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

これは、PHPの出力とは実際には異なります。

###################
#   # #           #
### # # ##### ### #
# # # #   # #   # #
# # # ### # ### ###
# # #   #     #   #
# # ### ##### ### #
# #   #         # #
# ### ########### #
# #   #         # #
# # ### ####### # #
# #     #   #   # #
# ####### # # ### #
#         #       #
###################
4

1 に答える 1

2

言語間でコードを翻訳するときは、意味の違いに注意してください。ここで気になるのは、Rubyの負のインデックスは、nullを返すのではなく、配列の最後からカウントバックすることです。迷路コードには、配列の最後から移動しているかどうx_posかを確認するための範囲チェックはありません。y_pos存在しない要素を要求された場合にnullを返す配列に依存するだけです。最終的な結果は、左右からの移動を許可し、無意味な結果を取得することです。また、ランダマイザーが正しくヒットすると、右と下でnilの例外が発生するため、それらもチェックする必要があります。

したがって、4つの方向チェックで、範囲コードを追加します。たとえば、これを回します:

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(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

同様に、y_posものとwidth

于 2012-10-14T22:42:47.527 に答える