How to go about implementing a problem solution as a program
From Wikiversity
|
A common misconception newcomers have to programming is that when a programmer approaches a problem, he just sits down at a computer and starts typing out straight code. (Well, ok, some progammers do do this, but it's a bad idea for all but very simple cases as it leads to messy, difficult to maintain code). Here's an approach to programming problems that has served me well over the years. Let's take an example... something not too complicated... hmm... Ok, let's say you want to make the computer play the Hangman word game. Here's a good way to go from idea to finished program: 1. State the problem and figure out in more detail what you need to accomplish. You want the computer to play hangman. In more detail, what is that going to require? Well let's think... - We'll need some way for the user to guess a letter (by hitting a key on the keyboard). - We should draw the hanging man somehow... we can represent him by simple characters like so: o -|- /| - We should display the correct guesses as well as blanks, something like this: H_MB_RG_R Ok, that gives us a rough idea of what we need to accomplish. |
|
2. Go from your requirements to an English language procedure. (of course you can replace English with your native language :) Alright, we have some idea of what we need to do, now how can we do it? Well let's go through our requirments one by one, and start programming in English. We need to wait until the user types a letter on the keyboard. Then we'll check if the letter they guessed is one of the letters in the mystery word. Then we draw the mystery word. And then we draw the man. And then we loop back to the begining and start over. We'll stop looping if the mystery word is fully revealed, or the man gets fully hanged.
Anyway where was I... ah yes. |
|
3. Go from English to pseudo-code. Now, I think there might be an actual formal version of pseudo code, but for these purposes I just mean "sort of" code. Let's turn our English into something closer to our programming language. I'll use the English version of the algorithm as comments for the psuedo code. This will make the code easier to understand both for other programmers and for ourself should we come back to this code at some point down the road. Note that "//" before a line indicates a comment.
while the game is not over
//We need to wait until the user types a letter on the keyboard.
typedLetter = waitForLetterFromKeyboard()
//Then we'll check if the letter they guessed is one of the letters in the mystery word.
checkMysteryWord( typedLetter )
//Then we draw the mystery word.
printMysteryWord()
//And then we draw the man.
drawMan()
//Stop looping if the mystery word is fully revealed, or the man gets fully hanged.
if( mysteryWordRevealed() )
Tell user, "You win!", then quit
else if( manFullyHanged() )
Tell user, "You lose :(", then tell them what the word was, then quit.
|
|
4. Alrighty! Now we're 70% of the way there. Time to take our pseudo-code and implement it in our favorite programming language. |