{demonstrates random walk in 2 dimensions. Step 3} uses crt; const dot = '.'; type row_type = 1..15; grid = array[row_type, row_type] of char; procedure initialize( var board : grid ); var row, col : row_type; begin for row := 1 to 15 do for col := 1 to 15 do board[ row, col ] := dot end; procedure print( board : grid ); var row, col : row_type; begin clrscr; for row := 1 to 15 do begin for col := 1 to 15 do write( board[ row, col ] ); writeln end; end; procedure plot( inc: boolean; var z : integer); {inrements z, where z corresponds to either x or y} begin if inc then z := z + 1 else z := z - 1 end; procedure generate( var board : grid; var x, y : integer ); {m determines whether x or y is incremented or decremented} const T = true; F = false; var m : 0..3; begin m := random( 4 ); case m of 0 : plot( T, x); 1 : plot( F, x); 2 : plot( T, y); 3 : plot( F, y); end; board[ x, y ] := '*' end; procedure start( var board : grid; var x, y : integer ); begin x := 8; y := 8; board[ x, y ] := '*'; end; function continue : boolean; var ans : char; valid : boolean; begin repeat writeln('Do you want to continue? (Y/N)'); ans := upcase( readkey ); valid := ans in [ 'Y', 'N']; until valid; continue := ans = 'Y' end; var board : grid; x, y : integer; begin initialize( board ); randomize; start( board, x, y ); print( board ); while continue do begin generate( board, x, y ); print( board ) end end.