Maze generation: Difference between revisions
Content added Content deleted
No edit summary |
|||
Line 3,345: | Line 3,345: | ||
| | | | |
| | | | |
||
+---+---+---+---+---+---+---+---+---+---+---+ |
+---+---+---+---+---+---+---+---+---+---+---+ |
||
=={{header|Huginn}}== |
|||
<lang huginn>import Algorithms as algo; |
|||
import Mathematics as math; |
|||
import Terminal as term; |
|||
class Maze { |
|||
_rows = none; |
|||
_cols = none; |
|||
_data = none; |
|||
constructor( rows_, cols_ ) { |
|||
_rows = ( rows_ / 2 ) * 2 - 1; |
|||
_cols = ( cols_ / 2 ) * 2 - 1; |
|||
_data = [].resize( _rows + 2, [].resize( _cols + 2, false ) ); |
|||
x = 0; |
|||
y = 0; |
|||
path = []; |
|||
rng = math.Randomizer( math.Randomizer.DISTRIBUTION.DISCRETE, 0, integer( $2 ^ $63 - $1 ) ); |
|||
for ( _ : algo.range( _rows * _cols / 3 ) ) { |
|||
_data[y + 1][x + 1] = true; |
|||
while ( true ) { |
|||
n = neighbours( y, x ); |
|||
ns = size( n ); |
|||
if ( ns == 0 ) { |
|||
if ( size( path ) == 0 ) { |
|||
break; |
|||
} |
|||
y, x = path[-1]; |
|||
path.pop(); |
|||
continue; |
|||
} |
|||
oy, ox = ( y, x ); |
|||
y, x = n[rng.next() % ns]; |
|||
_data[(y + oy) / 2 + 1][(x + ox) / 2 + 1] = true; |
|||
path.push( ( y, x ) ); |
|||
break; |
|||
} |
|||
} |
|||
_data[0][1] = true; |
|||
_data[-1][-2] = true; |
|||
} |
|||
neighbours( y_, x_ ) { |
|||
n = []; |
|||
if ( ( x_ > 1 ) && ! _data[y_ + 1][x_ - 1] ) { |
|||
n.push( ( y_, x_ - 2 ) ); |
|||
} |
|||
if ( ( y_ > 1 ) && ! _data[y_ - 1][x_ + 1] ) { |
|||
n.push( ( y_ - 2, x_ ) ); |
|||
} |
|||
if ( ( x_ < ( _cols - 2 ) ) && ! _data[y_ + 1][x_ + 3] ) { |
|||
n.push( ( y_, x_ + 2 ) ); |
|||
} |
|||
if ( ( y_ < ( _rows - 2 ) ) && ! _data[y_ + 3][x_ + 1] ) { |
|||
n.push( ( y_ + 2, x_ ) ); |
|||
} |
|||
return ( n ); |
|||
} |
|||
to_string() { |
|||
s = ""; |
|||
for ( r : _data ) { |
|||
s += ∑( algo.map( r, @( b ) { b ? " " : "#"; } ) ); |
|||
s += "\n"; |
|||
} |
|||
return ( s ); |
|||
} |
|||
} |
|||
main() { |
|||
rows = term.lines() - 2; |
|||
cols = term.columns() - 1; |
|||
maze = Maze( rows, cols ); |
|||
print( "{}".format( maze ) ); |
|||
}</lang> |
|||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |