text
stringlengths
0
897
end if
print "...OK... doing big complex processing with " + inp + "..."
end while
```
Just imagine that line 8 in the example above is actually a couple dozen lines long, and you may see why bailing out at the top of the loop is better than putting all that code inside a giant `if` block. (Remember, the goal isn't to write code the computer can understand... it's to write code a *human* can understand,...
As a final example, here's a program that may actually be useful. It allows you to encode and decode messages in a cipher called "ROT-13." In this cipher, every letter in the alphabet is rotated 13 places, so A maps to N, B maps to O, etc. Because the alphabet is exactly 26 letters long, a 13-letter rotation also do...
{width:"75%"}
![](secretMessage.svg)
{caption:ROT-13 secret message encoder/decoder}
```miniscript
while true
msg = input("Enter your message: ")
if msg == "" then break
result = ""
for i in range(0, msg.len - 1)
c = msg[i] // gets the i'th letter of msg, starting at 0.
if c >= "A" and c <= "Z" then
letterNum = c.code - "A".code // A=0, B=1, etc.
letterNum = (letterNum + 13) % 26 // 1->14, 2->15...
result = result + char("A".code + letterNum) // 0->A...
else if c >= "a" and c <= "z" then
letterNum = c.code - "a".code // a=0, b=1, etc.
letterNum = (letterNum + 13) % 26 // 1->14, 2->15...
result = result + char("a".code + letterNum) // 0->a...
else
// not a letter; copy to the output as-is
result = result + c
end if
end for
print result
end while
```
Notice how there is a main `while` loop that runs forever, but we break out of it (on line 3) if the user enters an empty string. Then within the `for` loop that iterates over each character of the message, we have an `if`/`else if`/`else` block that checks for several possibilities: the character is an upper-case let...
Enter this code carefully, and confirm that it works by entering a message, and then copying the encoded message and pasting it back as your next input, which should produce the original message. Use this to send secret messages over email or social media! And then when you're done, just press Return without typing i...
{pageBreak}
A> **Chapter Review**
A> - You learned how to use `if`, with optional `else if` and `else` blocks, to make a program do different things depending on the data.
A> - You can use a single-line `if` or `if`/`else` for very short checks.
A> - You learned how to break out of a loop with `break`, or skip to the next iteration with `continue`.
{chapterHead: "Day 7: Three Small Games", startingPageNum:63}
{width: "50%"}
![](Chapter7.svg)
Q> The only way to learn a new programming language is by writing programs in it.
Q>— Dennis Ritchie (creator of C programming language)
A> **Chapter Objectives**
A> - Rest and recouperate.
A> - Enter and play with some simple-but-fun MiniScript games.
A> - Review and synthesize what you've learned so far.
If you've been doing one chapter a day, then it's been a week since you started on your journey to programming mastery. If you've been doing the chapters faster or slower than that, that's fine too. In any case, it's time to pause and let it sink in.
A great way to help it sink in is to type in some short programs. In our modern world of scrolling through social media posts and picking responses from suggestions made by AI, you will be tempted to skip this exercise. Don't! Typing in code is crucial to learning how to write your own code. It's like having an exp...
So in this chapter are three small games. Why games? First, games are fun. But more importantly, games like these are *easy*; they represent a world of simplified, well-defined rules that can be converted into code. With only six chapters behind us, our toolset is still a bit limited. We haven't learned about list...
## The Matchstick Game
First up is the matchstick game. This is a classic numbers game for two players. You start with a pile of matchsticks between you, and then take turns removing one, two, or three matchsticks from the pile. Whoever takes the last matchstick loses. The skill in this game is to think ahead and try to force your oppone...
Our implementation here pits you, the human player, against the computer! Here's the code — only 40 lines long. Type it in and take it for a spin. If you get any errors, compare your code carefully to the code below. If you are still getting errors, check the troubleshooting section at the end of this chapter.
{caption:Matchstick Game}
```miniscript
// Matchstick Game
qty = 15
while qty > 0
// Human player's turn.
// Print how many matchsticks there are.
if qty == 1 then
print "There is one matchstick left."
else
print "There are " + qty + " matchsticks."
end if
// Ask the player how many to take.
take = 0
while take < 1 or take > 3 or qty - take < 0
take = round(val(input("How many do you take? ")))
end while
// Update the quantity left, and check for game-over.
qty = qty - take
if qty == 0 then
print "You took the last matchstick. I win! :)"
break
end if
// Computer player's turn.
// Print how many matchsticks there are now.
if qty == 1 then
print "That leaves just one matchstick."