Rock-Paper-Scissors


The most important part of writing any program is to understand what the program needs to do. If you don't understand how the program is going to work, you cannot succeed at telling the computer how the program needs to work. Understanding it means telling somebody -- perhaps yourself, but it's better to tell somebody else -- what the program is going to do.

In English (or Spanish or Chinese, whatever language you think in).

Then after you know what the computer must do, you have a shot at describing -- still in English or whatever -- how it will do that. Write it down. On paper with a pencil is good, but if you think better that way, you can use your computer's note pad. Paper is better, because you can draw pictures if that helps. Whatever helps you to understand what the computer needs to do.

I can't emphasize this too much, programming is all about telling the computer what it needs to do, and once you have it in English, all the rest is just translating. Those of us who know two languages know how hard that can be, but that is what programming is all about, saying in English what the computer must do, then saying the same thing in the computer language. Then you are done. Really. When you accurately told the computer what you want it to do, then it will do that.

It helps if you remember that the computer only knows how to do five different things (sequence, iteration, conditionals, variable values, and I/O). Some of these have multiple ways to spell them, perhaps to make some forms of them easier to use, but there are only these five. You describe your program in English, then every part of your description that is not one of these five, you break it down into more detail, until every detail is one of these five. Then you say the same thing using words and punctuation that the computer understands and you are done. That's all you will ever do as a programmer. Learn how to spell these five things in that programming language -- today that is English -- learn what programs other people have already written that you can use (we will get to that in a later session), and then just do it.

So, let's get started.

Did you ever play Rock-Paper-Scissors? It's a two-person hand-motion game, where each person stands there facing the other, and they both synchronize by pounding their fists on the flat of the other open hand three times together, except the third time they open the fist into one of three shapes, flat open like a sheet of paper, or two fingers extended like a pair of scissors, or else leave it in a fist representing a rock. They compare their two hands according to the rules: "Rock breaks scissors, scissors cut paper, and paper covers rock," to determine who won that round. The idea of synchronization is so that neither person has time to see what the other person will play before they must choose their own play, so it's fair. There is no guaranteed winning strategy.

Computers don't have hands they can form into fists and other shapes, and although your computer probably has a camera to see the other player, programming visual recognition in a camera image is very hard, so if we are going to program this computer to play Rock-Paper-Scissors today, it will be using text input and output instead of real hands and vision. The computer will provide the timing by printing out "One... Two... Three" on the screen, then the human player will type in a single letter "R" or "P" or "S" for their choice, and the computer will print out its own choice and make the comparison. And then you repeat the sequence for the next round.

Do you think you can write a program to do that? Of course you can! The first thing to do is write down (in English, on a piece of paper) what you think the computer must do to play the game, one step each line. Then turn the page.

<<Previous | ToC | Next >>

[2021 June 16a]