Monday, November 25, 2013

Simpleton Geek Teaches Computer Programming Book 2 Ch 7B


7.2 Number Menu + Wait for INPUT
The teacher was showing some menu on the board. It was simply a screenshot with numbered items.
SAMPLE PROGRAM
1. HELLO
2. CAT
3. DOG
4. END
CHOICE?
“So, this is what you have to do. You have already know how to do 'Press (Enter) to continue. Now you're going to do something about it. It's time for you to learn how to do GOTO! In fact, we're going to do something different. In addition to plain GOTO, we'll also do IF-GOTO, and ON GOTO! Actually, I was going to show you how to print on the lower screen, but since you asked about GOTO, may as well do it now! Gya ha ha ha!” Nancy was very embarrassed about it.
“Here's something you should know.” The teacher continued. “RESTORE can take a string as label! So, you can do this easily.”
CH$[0]=”HELLO”
CH$[1]=”CAT”
CH$[2]=”DOG”
CH$[3]=”END”
INPUT C:C=C-1
PRINT CH$[C]
“Very important to note that we're doing 'C-1' calculation in there. That's because array starts at zero, but menu starts at 1! Off by one is a very common mistake, and you need to ALWAYS watch for it! Depending on the situation, you may want to set 'END' as 0, but let's leave it for now. But we want more than that! So, given the right DATA statements, we can actually do this:
INPUT C:C=C-1
RESTORE “@”+CH$[C]
READ X$
and so on... (Appendix B)
“That will make it very convenient to write the program. There's another way to do it as well.
INPUT C
ON C GOTO @END,@HELLO,@CAT,@DOG,@END
“You see that in this case, I simply repeated the '@END' twice. You will also notice that I did not decrement the value of C at all. This is an example of the proper way to use GOTO can clarify the code. You need to understand when to use GOTO, and when NOT to use goto. In this case, using ON-GOTO clarifies the program tremendously, so you are well advised to use it in this case. What if you're using a computer language that do not have GOTO? You probably have to use IF-LADDER:
IF C==1 GOTO @HELLO
IF C==2 GOTO @CAT
IF C==3 GOTO @DOG
IF C==4 GOTO @END
“As you can see, there's a lot of repetition here. I suppose if you're going to be stubborn about it, you can do so. However, as far as readability goes, putting it into one line would have been preferable than scrolling down the screen too much. Scrolling down take time and energy. Therefore, I say that computer languages that does not allow you to put multiple commands in one line is not a well-designed language. Python, for example, for all their readability concern, doesn't allow you to do that. There are several other languages as well.”
“One more thing. The fact that strings can be label, means that you can do this:”
INPUT C:C=C-1
GOTO CH$[C]
“Which makes it very convenient. You can simply put the choices into DATA statements. Then as you read them into the array, you can display them on screen. Something like this:
RESTORE @MENU:CLS
READ CH$[0]:?”1”,CH$[0]
READ CH$[1]:?”2”,CH$[1]
READ CH$[2]:?”3”,CH$[2]
READ CH$[3]:?”4”,CH$[3]
“That way you need to type things only once. Which makes it very convenient to do. Let's do a simple template. Let's make a simple interactive story where you get to be the hero. Make it so that each step, you can make up to 4 choices to make. It's show and tell time. How are you going to do it?”
“We'll have the hero fight a dragon and rescue the princess!” said a student.
“Oh, yeah? We'll have the hero fight a dragon and rescue the PRINCE!” said a female student.
“Ha! The prince don't need any rescuing! Only the princess!”
“We don't need rescuing, either!”
“Hold it, right there!” said the teacher. “We're not into stories, yet. We're still discussing program structure. What is wrong with you guys? Are you not paying attention to the lesson plans? Either you give me program structure in the next 5 minutes, or I'll start whacking heads until you do!” All the students coiled in terror!
Finally, Peter said, “There is a screen of text at the top, followed by 4 choices at the bottom.”
“Right you are! Is that so hard to do?” The teacher wrote:
- 1 line of title
- 10 lines of text
- 4 lines of menu
“What's next?” asked the teacher.
“Then using ON-GOTO, we display another line of text.”
“ON-GOTO is implementation specific, but I get the idea. Here's the completed pseudocode:”
1. INIT
- TITLE$,TEXT$[10]
- PAGE$[4],MENU$[4]
- Choice (CH), P$
2. RESTORE-DATA
3. CLS:DISPLAY SCREEN
- 1 line of title
- 10 lines of text
- 4 lines of menu
4. INPUT CHoice
5. Set P$ according to CHoice
6. If not done, GOTO 2
“Is that simple enough? Now, it's very important that you follow this structure, so let's learn something else: REMark (REM) command. This command actually doesn't do anything. All it does is to ignore whatever is typed after this command. So, you can give yourself a little note for the program. So put that pseudocode in your code as comment. One more thing: Tick mark is a shorthand for REM, so you can use that as well.
REM This is just a comment.
'So is this. Tick mark is REM.
“Now I want you to write a simple story with branching. Each page will have a title, up to 10 line of text, and up to 4 menu choices. So, I want you to write it like this in your journal:”
Page 1: Page id
Title: Title string
Text: up to 10 lines
Menu0: Page id-Menu string
Menu1: Page id-Menu string
Menu2: Page id-Menu string
Menu3: Page id-Menu string
“Remember that each line is only up to 32 characters, so budget your lines accordingly. Also, you can leave blank lines if you wish. You can also leave blank menu items. Simply leave Menu string blank, and set the Page id to current Page-id. Alright? Let's get to it!”
The students get all worked up. They're busy writing stories in their journal. An example is shown in Appendix (C). After a while, all the students have gotten at least one page filled.
“Good.” The teacher said. “Now, what I want you to do is to write a bunch of DATA statements. Use the Page-id as label for the  DATA segment. Like this:”
@INTRO
REM TITLE
DATA “THE STORY OF WINDELL”
REM TEXT
DATA “ONCE UPON A TIME, THERE WAS A”
DATA “LITTLE RAT BY THE NAME OF”
DATA “WINDELL.”
DATA “ “
DATA “SHE WAS HUNGRY AND IN SEARCH OF”
DATA “CHEESE. WHERE DO YOU THINK”
DATA “SHE SHOULD GO?”
DATA “ “
DATA “ “
DATA “ “
REM MENU: PAGEID - MENU TEXT
DATA “KITCH”,”TO THE KITCHEN”
DATA “BEDRO”,”TO THE BEDROOM”
DATA “BADEND”,”STAY HOME”
DATA “INTRO”,” “
“I want you to write all these for homework, every page that you have, written in this form. And that's it! You already know how to save the program, but I don't want you to save anything. Just the DATA statements. Save it into another file, called 'STORY' or something like that. Tomorrow, we'll be doing the coding. Got it?”
The next day, everybody was ready with their story. However, the teacher simply led them to the implementation of the pseudocode. “Alright, everybody. Set your timer!”
The first thing we want to do is the initialization. Now, we already have the pseudocode, and of course, those are noted in the REM statements already right?” The students gulped. They didn't do it that way. “Never mind! Just do it right now and quick! You're on a timer! Gya ha ha ha!”
REM INIT
CLEAR:TITLE$=””
DIM TEXT$[10]
DIM PAGE$[4]
DIM MENU$[4]
CH=0:P$=”INTRO”
REM RESTORE-DATA
@SHOWPAGE
RESTORE “@”+P$
READ TITLE$
READ TEXT$[0]:READ TEXT$[1]:READ TEXT$[2]
READ TEXT$[3]:READ TEXT$[4]:READ TEXT$[5]
READ TEXT$[6]:READ TEXT$[7]:READ TEXT$[8]
READ TEXT$[9]
READ PAGE$[0]:READ MENU$[0]
READ PAGE$[1]:READ MENU$[1]
READ PAGE$[2]:READ MENU$[2]
READ PAGE$[3]:READ MENU$[3]

REM DISPLAY SCREEN
CLS:?TITLE$:?
? TEXT$[0]:? TEXT$[1]:? TEXT$[2]
? TEXT$[3]:? TEXT$[4]:? TEXT$[5]
? TEXT$[6]:? TEXT$[7]:? TEXT$[8]
? TEXT$[9]:?:?
? “1”,MENU$[0]
? “2”,MENU$[1]
? “3”,MENU$[2]
? “4”,MENU$[3]

REM INPUT Choice$
INPUT CH:CH=CH-1
P$=PAGE$[CH]

REM If not done, GOTO 2
GOTO @SHOWPAGE
“Okay! And done! That's less than 10 minutes! That what you get from using Copy and Paste option from the menu. What's that? You don't know where it is? It's on the lower right option. Use the arrow to set it to copy and paste. You didn't know that? What a loser! Can't even read the help menu! Gya ha ha ha!”
Once the students are all finished, the teacher then have them saved the program. “Make sure that you do not overwrite your homework! Save it under a different name! Got it? Good. Now I want you to stay in the RUN mode, and type APPEND storyfile. Hit Enter. Go to the EDIT mode. What do you see?”
The students did just that, and “Hey! I got my data at the end of the program!” “Me, too!”
The teacher laughed. “Now you see why DATA statements are traditionally located at the end of the program. That's because simply by appending a different file, you can have a different data immediately. It makes it very convenient to have a different story with just one program.”
Peter raised his hand, “Is this the way to do it? Normally, we just select the file from the menu. Does that mean the APPEND command works inside the EDITOR?”
“Not in Petit Computer case. That's a good question, by the way. Commands inside the program depends on the fact that you do not change the program while inside it. So, there is that liability involved there. Normally, you do not store the data in DATA statements.”
“You don't? But it's so convenient!”
“Actually, not at all. It's very inconvenient to do so. Much more convenient to just load the data off a file, which Petit Computer do not have. So, an alternative must be sought. There are several different alternatives.
“However, there is another command: EXEC which will execute another program. Now, you understand why you need to clear the memory before you allocate them via DIM. Once the memory has been allocated, it stays in the memory until explicitly cleared.
“Thus, you can simply EXEC a program which contains the DATA statements, and load the data into memory, then EXEC the actual program to process the data. By splitting the process into two different programs, you don't have to do APPEND each time you run it. You simply run the data loading program, which in turn runs the real program later on.”
Peter nodded his understanding.
“So, there you go. By the way, we did our program in about 10 minutes. How long does it take you to enter the data?”
“It took me one hour.”
“Only one hour? It took me three!”
“It only took me 2 hours”
“Me, too!”
The teacher laughed. “That's actually normal.”
“But teacher,” said Nancy, “That's totally backward! Shouldn't writing the computer program the most difficult task of all?”
“No, of course, not. Why do you say that?”
“Because writing computer program is so difficult and you need to debug them and test them for a long time!” Other students nodded their heads.
“Are you kidding me? Have you forgotten what I said in the beginning already?” asked the teacher. Seeing their puzzled facial expressions, it is obvious that they did.
“See, here, computer programming is one of the easiest thing you can do. Of course, the key is that IF you know what you're doing. Since most people don't know what they're doing, of course it's going to be difficult!
“Now look at this pseudocode. Do you see anything difficult about this at all?” All the students shook their heads. “See? Once you know what to do, it becomes the easiest thing to do. A routine matter. No trouble at all.”
The students were unconvinced. All their experience showed that computer programming is mind-numbingly difficult. Yet, they didn't have too much trouble at all.
“Do you wonder why you have no trouble at all? Think of it this way: How long does it take you to translate from English to a foreign language?”
“Which foreign language?” asked a student.
“The one you know well, of course. Whatever it is. So, how long does it take you?”
“Well, usually I need to write it down, then translate it, then read it. So about a minute or two?”
“Do you know that United Nation interpreters do it in real time? As soon as the other person speaks, the interpreter says it in foreign language. Immediately. Without a delay.”
“But how is that possible?”
“From experience. Just as you memorize multiplication table, you memorize grammar and vocabulary. Once you have it, no effort is expended. It's all automatic.
“Guess what? Computer programming is merely the translation from English (Pseudocode) into Computerese (Petit Computer). Once you know what's going on, it should be effortless, and it was.”
The students cannot argue with that. Indeed, the process was quick and effortless.
“Now let's see what we have here. Are you guys up to your stories? Swap them with your neighbor and play!”



No comments:

Post a Comment