text
stringlengths
0
897
print "I guess I have to take it. You win!"
break
else
print "That leaves " + qty + " matchsticks."
end if
// Calculate how many the computer should take.
take = (qty - 1) % 4
if take == 0 then take = 3
if rnd < 0.2 then take = ceil(rnd * 3) // occasional mistake
print "I'll take " + take + "."
// Update the quantity.
qty = qty - take
if qty == 0 then print "Oops. You win!"
end while
```
Most of this code should be pretty clear to you by now. There's a main `while` loop, as in most interactive programs. This begins by printing out the current state of the game on lines 6-10. Then we ask (with `input`) how many matchsticks you want to take. Notice how we use a little `while` loop around that to keep...
The second half of the main loop does the computer's turn. It begins by again printing the current state. Then on lines 33-35, we do a bit of mathematical wizardry to figure out how many matchsticks the computer should take. Let's pause and study how that works.
The computer's goal is to leave only 1 matchstick, forcing the player to take it. So if there are 2 sticks left, the computer should take 1; if there are 3 it should take 2, etc. In general, if `qty` (the number of matchsticks left) is a small number, the computer should take `qty - 1`. That explains the first part ...
This works up to 4, since 4 - 3 is 1, a valid number of matchsticks to take. What if there's more than that? Well, if there are 5 matchsticks left, then the computer is out of luck. No matter how many it takes, the human can take all but one of the remainder. But this works both ways; if the computer can stick the ...
Whenever you have a sequence that repeats every so-many numbers, it's a hint to use the mod (`%`) operator. Mod is the remainder after division. In this case, we want the remainder after dividing by 4. Let's work through an example: if `qty = 6`, then `qty - 1` is 5, and `5 % 4`, the remainder after dividing 5 by 4,...
The only hitch in this formula is that sometimes the remainder after dividing by 4 is 0. This is true when `qty - 1` is 4, 8, 12, etc. Taking 0 sticks is not a legal move. These are the no-win situations for the computer; it doesn't really matter what we do in this case, but to keep the game moving along, we have th...
If all this math is still unclear, don't worry about it. It's not the programming that's tricky; it's the math! And eventually, programming will make math easier. For now it's OK to just take these formulas on authority and enjoy the game.
Finally, note that the computer would play a perfect game if not for line 35. That generates a random number between 0 and 1, and if this is less than 0.2 (i.e., 20% of the time), it makes a random move instead of the ideal move. This causes the computer to make occasional mistakes, giving the poor human a chance to ...
**Challenge:** Change line 34 so that when the computer is in a tight spot, instead of always taking 3 matchsticks, it randomly takes 1, 2, or 3. This makes the game less predictable and more interesting for a good human player.
## The Bit-Flip Game
Next up is a fun little logic puzzle. Nine bits are arranged in three rows and three columns. You can think of these as coins, or Othello pieces that are black on one side and white on the other, or cards that are face up or down. But since we're doing this on a computer, they're just bits, each with a value of 0 or...
The bits start out all 0, and your goal is to change them all to 1. On each move, you pick a row and column. All the bits that match your row or column are flipped: 1 becomes 0, and 0 becomes 1. Can you find a sequence of moves that gets all the bits to be 1?
{caption:Bit-Flip Game}
```miniscript
b0A = 0 // bit in column 0, row A
b0B = 0 // bit in column 0, row B
b0C = 0 // (etc.)
b1A = 0
b1B = 0
b1C = 0
b2A = 0
b2B = 0
b2C = 0
print "Can you flip the all bits to 1?"
print
while true
print "Bit: "
print " A: " + b0A + " " + b1A + " " + b2A
print " B: " + b0B + " " + b1B + " " + b2B
print " C: " + b0C + " " + b1C + " " + b2C
print " - - -"
print " 0 1 2"
print
if b0A + b0B + b0C + b1A + b1B + b1C + b2A + b2B + b2C == 9 then
print "You win!"
break
end if
cmd = input("Which row and column to flip (e.g. A0)? ").upper
if cmd.len != 2 then continue
row = cmd[0]
col = val(cmd[1])
if col == 0 then
b0A = 1 - b0A
b0B = 1 - b0B
b0C = 1 - b0C
else if col == 1 then
b1A = 1 - b1A
b1B = 1 - b1B
b1C = 1 - b1C
else if col == 2 then
b2A = 1 - b2A
b2B = 1 - b2B
b2C = 1 - b2C
end if
if row == "A" then
if col != 0 then b0A = 1 - b0A
if col != 1 then b1A = 1 - b1A
if col != 2 then b2A = 1 - b2A
else if row == "B" then
if col != 0 then b0B = 1 - b0B
if col != 1 then b1B = 1 - b1B
if col != 2 then b2B = 1 - b2B
else if row == "C" then
if col != 0 then b0C = 1 - b0C
if col != 1 then b1C = 1 - b1C
if col != 2 then b2C = 1 - b2C
end if
end while
```
The logic in this game is straightforward. Notice that our main loop in this program is using `while true`, with a `break` when the game is over (line 22). We get the user's input on line 24, and if it's not 2 characters long, use `continue` to jump back to the top of the loop.