text stringlengths 0 897 |
|---|
The rest of the main loop flips the appropriate set of bits. There is an `if`/`else if` structure for the column (lines 28-40), and another one for the row (lines 41-53). Note that there is one bit that matches the column *and* the row — i.e., the one at the exact coordinates you enter. We don't want to flip that on... |
**Challenge:** The code given above gets the job done and exercises some important variations on the `if` statement. But there is another way to do it. Lines 28-53 could be replaced with nine lines, each checking for a particular row and column, like this: |
```miniscript |
if row == "A" or col == 0 then b0A = 1 - b0A |
``` |
Delete lines 28-53, and replace them with the line above, which handles position A0, and eight similar lines to handle the other eight positions. Verify that the game still works! |
## Pig Dice Game |
The last game for this chapter is another computer-versus-human game. This one involves rolling dice. On each player's turn, a six-sided die is rolled. If the player rolls a 1, their turn is over and they get no points for that turn. If they roll 2 to 6, they get that many points added to their total for the turn. ... |
In either case, play then passes to the other player. The first player to reach the goal (32 points in this version) wins the game. |
{width:"50%"} |
 |
{caption:Pig Dice Game} |
```miniscript |
// Pig Dice game |
human = 0 |
computer = 0 |
goal = 32 |
print "Let's play Pig to " + goal + " points!" |
while human < goal and computer < goal |
// Human player's turn |
print |
print "Your turn! (You: " + human + " Me: " + computer + ")" |
rollTotal = 0 |
choice = "R" |
while choice != "D" |
if choice == "R" then // roll! |
wait |
roll = ceil(rnd * 6) |
if roll == 1 then |
print "You roll a 1. No points for you!" |
rollTotal = 0 |
break |
end if |
rollTotal = rollTotal + roll |
print "Your roll: " + roll + ", total: " + rollTotal |
end if |
choice = input("[R]oll again, or [D]one? ") |
choice = choice.upper |
end while |
human = human + rollTotal |
print "You gain " + rollTotal + " points, and now have: " + human |
if human >= goal then break |
// Computer's turn |
print |
print "My turn! (You: " + human + " Me: " + computer + ")" |
rollTotal = 0 |
while true |
wait |
roll = ceil(rnd * 6) |
if roll == 1 then |
print "I roll a 1. Darn!" |
rollTotal = 0 |
break |
end if |
rollTotal = rollTotal + roll |
print "My roll: " + roll + ", total: " + rollTotal |
if rnd * 20 < rollTotal then |
print "That's enough for me." |
break |
end if |
print "I'll roll again." |
end while |
computer = computer + rollTotal |
print "I gain " + rollTotal + " points, and now have: " + computer |
end while |
print |
print "Final scores: You: " + human + " Me: " + computer |
if human > computer then |
print "You win. Well played!" |
else |
print "I win. Good game!" |
end if |
``` |
As usual, there's a main `while` loop here. Within that, we have a section of code for the human player, using another `while` loop to let the player roll as many times as they like until they stop or get a 1. Notice how the `break` statement on line 19 breaks out of this inner loop. |
Then we have a similar section of code for the computer player. The AI (artificial intelligence) here is on line 45: the computer decides to stop when `rnd * 20 < rollTotal`. The formula `rnd * 20` picks a random number between 0 and 20. So when the computer's total roll is rather small, it's likely to roll again. ... |
Play the game a few times and see how often you can win. It's not easy! |
**Challenge:** the rule used on line 45 is certainly not optimal; it always plays the same, regardless of whether it's ahead or behind. A smarter player might play more aggressively when behind, and more conservatively when ahead. Change the code so that it first picks a `target` value, equal to 10 if `computer > hum... |
## Troubleshooting |
If you got errors or incorrect behavior in any of the programs above, start by comparing your code carefully to what's in the book. Here are some common mistakes to watch out for: |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.