Maze generation: Difference between revisions
Content added Content deleted
(→Quality Breadth-First: Replace ad-hoc scramble function with language built-in.) |
No edit summary |
||
Line 4,186: | Line 4,186: | ||
# # # # # # |
# # # # # # |
||
#################################</pre> |
#################################</pre> |
||
=={{header|M2000 Interpreter}}== |
|||
{{trans|BASIC}} |
|||
For Next is not the same as basic. In M2000 always a loop perform once. Step converted to absolute value if start<>end. To go down we have to place start>end. If start=end then the value after the loop is equal to start+step and here step used as is (with no conversion to absolute value). |
|||
We can use the for loop as in basic using a software switch: see Help Switch from console. |
|||
Here we have positive steps so we can translate nicely from Basic without the use of the switch. |
|||
Variables with % in name as last character are like integers, but inside can be any numeric type, so width%=40 is internal a double, width$=40@ is internal a decimal. When we assign a number then this number rounded to integer, where w%=1.5 is 2 and w%=-1.5 is -2. If w%=1 then the statement w%/=2 not changed the w% (internal go to 0.5 so rounded to 1). |
|||
We can use integers, say a long, so a statement Long a=1 make a=1 and a/=2 set a to 0. |
|||
INT((currentx% + oldx%) / 2) return a double, because has 2 as double so we get (integer+integer)/double or integer/double or double. Int(0.5) return. |
|||
<lang M2000 Interpreter> |
|||
Module Maze { |
|||
width% = 40 |
|||
height% = 20 |
|||
DIM maze$(0 to width%,0 to height%) |
|||
FOR x% = 0 TO width% |
|||
FOR y% = 0 TO height% |
|||
maze$(x%, y%) = "#" |
|||
NEXT y% |
|||
NEXT x% |
|||
currentx% = INT(RND * (width% - 1)) |
|||
currenty% = INT(RND * (height% - 1)) |
|||
IF currentx% MOD 2 = 0 THEN currentx%++ |
|||
IF currenty% MOD 2 = 0 THEN currenty%++ |
|||
maze$(currentx%, currenty%) = " " |
|||
done% = 0 |
|||
WHILE done% = 0 { |
|||
FOR i% = 0 TO 99 |
|||
oldx% = currentx% |
|||
oldy% = currenty% |
|||
SELECT CASE INT(RND * 4) |
|||
CASE 0 |
|||
IF currentx% + 2 < width% THEN currentx%+=2 |
|||
CASE 1 |
|||
IF currenty% + 2 < height% THEN currenty%+=2 |
|||
CASE 2 |
|||
IF currentx% - 2 > 0 THEN currentx%-=2 |
|||
CASE 3 |
|||
IF currenty% - 2 > 0 THEN currenty%-=2 |
|||
END SELECT |
|||
IF maze$(currentx%, currenty%) = "#" Then { |
|||
maze$(currentx%, currenty%) = " " |
|||
maze$(INT((currentx% + oldx%) / 2), ((currenty% + oldy%) / 2)) = " " |
|||
} |
|||
NEXT i% |
|||
done% = 1 |
|||
FOR x% = 1 TO width% - 1 STEP 2 |
|||
FOR y% = 1 TO height% - 1 STEP 2 |
|||
IF maze$(x%, y%) = "#" THEN done% = 0 |
|||
NEXT y% |
|||
NEXT x% |
|||
} |
|||
FOR y% = 0 TO height% |
|||
FOR x% = 0 TO width% |
|||
PRINT maze$(x%, y%); |
|||
NEXT x% |
|||
PRINT |
|||
NEXT y% |
|||
} |
|||
Maze |
|||
</lang> |
|||
=={{header|Mathematica}}== |
=={{header|Mathematica}}== |