Remarks: Maze Walker

From Maze

(Difference between revisions)
Jump to: navigation, search
Line 16:Line 16:
There are, however, significant problems with writing the program this way. For one thing, the program is long; the programmer must engage in the repetitive task of writing the same line over and over again (changing the line numbers). Computers are good at repetition; whenever a program involves tedious and repetitive steps, programmers look for ways to make the computer do the repetitive work. More significantly, this version of the program is very hard to change. To create a longer sequence of 600 symbols, 200 more lines must be added to the program. To create a shorter sequence of 100 symbols, 300 lines must be deleted from the program. To create a pattern of two different symbols, or to change the relative frequency with which the two symbols appear, the number 205.5 must be changed in every line. Clearly the best way to write this program is to write the PRINT line once and have the computer execute the line 400 times. The program must somehow count to 400, so that it can keep track of how many times it has executed the PRINT line and stop after 400 times. The following program does this, introducing the FOR NEXT construct in BASIC.
There are, however, significant problems with writing the program this way. For one thing, the program is long; the programmer must engage in the repetitive task of writing the same line over and over again (changing the line numbers). Computers are good at repetition; whenever a program involves tedious and repetitive steps, programmers look for ways to make the computer do the repetitive work. More significantly, this version of the program is very hard to change. To create a longer sequence of 600 symbols, 200 more lines must be added to the program. To create a shorter sequence of 100 symbols, 300 lines must be deleted from the program. To create a pattern of two different symbols, or to change the relative frequency with which the two symbols appear, the number 205.5 must be changed in every line. Clearly the best way to write this program is to write the PRINT line once and have the computer execute the line 400 times. The program must somehow count to 400, so that it can keep track of how many times it has executed the PRINT line and stop after 400 times. The following program does this, introducing the FOR NEXT construct in BASIC.
 +
<pre>
5 FOR X = 1 to 400
5 FOR X = 1 to 400
10 PRINT CHR$(205.5 + RND(1));
10 PRINT CHR$(205.5 + RND(1));
15 NEXT X
15 NEXT X
 +
</pre>
The purpose of the FOR NEXT construct is to repeatedly execute lines (to loop over them) a fixed number of times. The lines to loop over are placed between the line with the FOR statement and the line with NEXT statement. In this case there is only one line between FOR and NEXT - the PRINT line that randomly prints one of two symbols to the screen. The FOR statement specifies a starting and ending number to count between, as well as a variable to hold the count. In this case, the count starts at 1 and ends at 400 (thus looping 400 times), and specifies a variable named X to store the count. [Not sure if we'll need to go into the concept of a variable in more detail - hopefully the concept of a variable has been introduced elsewhere]. The NEXT statement increments the counting variable, in this case, X. If the value of the count variable (X) is less than or equal to the maximum count specified in the FOR statement, control returns to the line after the FOR (in this case line 10 with the PRINT statement) and execution continues, looping one more time over the lines between FOR and NEXT. Otherwise, if the value of the count variable (X) is greater than the maximum count, execution continues on the line after the NEXT statement - the loop is done. In this case, since there is no program line after the NEXT statement, the program terminates. By utilizing the FOR NEXT looping construct, this very short program produces the desired rectangular pattern of 400 symbols, producing the first screen pattern that can be stably contemplated as a maze. [Reference screen image showing 10 line maze pattern followed by READY. and blinking cursor.]
The purpose of the FOR NEXT construct is to repeatedly execute lines (to loop over them) a fixed number of times. The lines to loop over are placed between the line with the FOR statement and the line with NEXT statement. In this case there is only one line between FOR and NEXT - the PRINT line that randomly prints one of two symbols to the screen. The FOR statement specifies a starting and ending number to count between, as well as a variable to hold the count. In this case, the count starts at 1 and ends at 400 (thus looping 400 times), and specifies a variable named X to store the count. [Not sure if we'll need to go into the concept of a variable in more detail - hopefully the concept of a variable has been introduced elsewhere]. The NEXT statement increments the counting variable, in this case, X. If the value of the count variable (X) is less than or equal to the maximum count specified in the FOR statement, control returns to the line after the FOR (in this case line 10 with the PRINT statement) and execution continues, looping one more time over the lines between FOR and NEXT. Otherwise, if the value of the count variable (X) is greater than the maximum count, execution continues on the line after the NEXT statement - the loop is done. In this case, since there is no program line after the NEXT statement, the program terminates. By utilizing the FOR NEXT looping construct, this very short program produces the desired rectangular pattern of 400 symbols, producing the first screen pattern that can be stably contemplated as a maze. [Reference screen image showing 10 line maze pattern followed by READY. and blinking cursor.]
Line 28:Line 30:
This program loops from 1 to 10, doing nothing but maintaining the count through each iteration:
This program loops from 1 to 10, doing nothing but maintaining the count through each iteration:
 +
<pre>
10 FOR X = 1 TO 10
10 FOR X = 1 TO 10
20 NEXT X
20 NEXT X
 +
</pre>
This is the equivalent BASIC program where the initialization, incrementing and testing of the count variable is made explicit, without using the FOR and NEXT statements:
This is the equivalent BASIC program where the initialization, incrementing and testing of the count variable is made explicit, without using the FOR and NEXT statements:
 +
<pre>
10 LET X = 1
10 LET X = 1
20 IF X > 10 THEN GOTO 50
20 IF X > 10 THEN GOTO 50
Line 38:Line 43:
40 GOTO 20
40 GOTO 20
50 END
50 END
 +
</pre>
The rectangular maze, though now a stabilized pattern, still does not appear on the screen by itself. The space above the maze contains whatever characters were already on the screen above the prompt where the RUN command was typed, while the space below the maze contains the READY prompt [reference figure above showing the 10 line rectangular maze]. Clearing the screen and moving the active print location to the top of the screen before printing the maze would eliminate stray characters appearing above the maze. The C64 keyboard has a special key, named CLR, which clears the screen. By printing this special CLR character within our program, we can clear the screen before printing the maze characters. [If the chapter on the C64 and BASIC don't talk about control characters, say more about control characters here]. There are two ways to refer to any keyboard character within a program: either by pushing the appropriate keyboard key within quotes, or using the character code with the CHR$ function. The control characters all have a visual counterpart for use within quoted strings. The CLR key can thus be referred to within a program by either "[CLR symbol]" or by CHR$(147). Printing this character clears the screen.
The rectangular maze, though now a stabilized pattern, still does not appear on the screen by itself. The space above the maze contains whatever characters were already on the screen above the prompt where the RUN command was typed, while the space below the maze contains the READY prompt [reference figure above showing the 10 line rectangular maze]. Clearing the screen and moving the active print location to the top of the screen before printing the maze would eliminate stray characters appearing above the maze. The C64 keyboard has a special key, named CLR, which clears the screen. By printing this special CLR character within our program, we can clear the screen before printing the maze characters. [If the chapter on the C64 and BASIC don't talk about control characters, say more about control characters here]. There are two ways to refer to any keyboard character within a program: either by pushing the appropriate keyboard key within quotes, or using the character code with the CHR$ function. The control characters all have a visual counterpart for use within quoted strings. The CLR key can thus be referred to within a program by either "[CLR symbol]" or by CHR$(147). Printing this character clears the screen.
Line 43:Line 49:
The program:
The program:
 +
<pre>
1 PRINT CHR$(147);
1 PRINT CHR$(147);
5 FOR X = 1 TO 400
5 FOR X = 1 TO 400
10 PRINT CHR$(205.5 + RND(1));
10 PRINT CHR$(205.5 + RND(1));
15 NEXT X
15 NEXT X
 +
</pre>
now generates a rectangular maze, 10 lines high, with the READY prompt appearing below the maze. The semicolon on the first line tells the PRINT statement not to start a new line after printing the CLR character. Without the semicolon, there would be one blank line above the maze, resulting from the PRINT on line 1 starting a new line. The READY prompt is generated by BASIC when a program terminates. In our case, after the FOR and NEXT statements increment X to 401, program execution continues past line 15, terminating the program and returning control of the C64 to the BASIC operating environment. In order to prevent the READY prompt from being printed, control must not return to BASIC; the maze program must execute forever. The original 10-print program already demonstrated how to use a GOTO statement to make a program loop forever. Now, however, we don't want the maze program to keep on printing forever, but rather to do nothing forever, so as not to return control to the BASIC operating environment and the resulting printing of the READY prompt. This is accomplished by including a line at the end of the program with a GOTO statement that loops to itself.
now generates a rectangular maze, 10 lines high, with the READY prompt appearing below the maze. The semicolon on the first line tells the PRINT statement not to start a new line after printing the CLR character. Without the semicolon, there would be one blank line above the maze, resulting from the PRINT on line 1 starting a new line. The READY prompt is generated by BASIC when a program terminates. In our case, after the FOR and NEXT statements increment X to 401, program execution continues past line 15, terminating the program and returning control of the C64 to the BASIC operating environment. In order to prevent the READY prompt from being printed, control must not return to BASIC; the maze program must execute forever. The original 10-print program already demonstrated how to use a GOTO statement to make a program loop forever. Now, however, we don't want the maze program to keep on printing forever, but rather to do nothing forever, so as not to return control to the BASIC operating environment and the resulting printing of the READY prompt. This is accomplished by including a line at the end of the program with a GOTO statement that loops to itself.
 +
<pre>
1 PRINT CHR$(147);
1 PRINT CHR$(147);
5 FOR X = 1 TO 400
5 FOR X = 1 TO 400
Line 55:Line 64:
15 NEXT X
15 NEXT X
20 GOTO 20
20 GOTO 20
 +
</pre>
This finally results in the generation of a pristine rectangular maze sitting on a screen by itself [reference figure depicting clean maze]. The next step is to fill the entire screen with a stabilized maze pattern.
This finally results in the generation of a pristine rectangular maze sitting on a screen by itself [reference figure depicting clean maze]. The next step is to fill the entire screen with a stabilized maze pattern.

Revision as of 13:00, 22 September 2010

View this revision of Remarks: Maze Walker.

Personal tools