19.04.2017 Views

Learn to Program with Small Basic

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

To try out this concept, you’ll play Pepper Dare, an exciting game of<br />

chance, against the computer. When the game starts, the player is handed<br />

10 imaginary cards face down. One of those cards has a jalapeño pepper on<br />

it; the rest are blank. The player picks a card and hopes for a blank one. If<br />

the player picks the card <strong>with</strong> the jalapeño, the player has <strong>to</strong> eat a hot pepper<br />

and the computer wins! If the player doesn’t get the pepper card, the<br />

computer takes a turn. The game ends when either the player or computer<br />

eats the pepper and runs for a drink of water. Enter the main program in<br />

Listing 10-4 in<strong>to</strong> <strong>Small</strong> <strong>Basic</strong>. You’ll add the subroutines in a moment.<br />

1 ' PepperDare.sb<br />

2 player = 1 ' 1 for player, 2 for computer<br />

3 pepper = Math.GetRandomNumber(10) ' Which card has the pepper<br />

4<br />

5 Again:<br />

6 Pick() ' Updates the two variables: card and name<br />

7 If (card = pepper) Then<br />

8 TextWindow.Write("Hot tamale, it's a pepper! ")<br />

9 TextWindow.WriteLine(name + " wins!")<br />

10 TextWindow.WriteLine("")<br />

11 Else<br />

12 TextWindow.Write("The card is blank. ")<br />

13 TextWindow.WriteLine("You put it back in and shuffle the deck.")<br />

14 TextWindow.WriteLine("")<br />

15 player = 3 - player ' Switches the player<br />

16 Go<strong>to</strong> Again<br />

17 EndIf<br />

Listing 10-4: Setting up Pepper Dare<br />

The game starts by setting the player variable <strong>to</strong> 1 <strong>to</strong> give you the first<br />

turn (line 2). It then randomly picks 1 of the 10 cards <strong>to</strong> be the card that<br />

has the jalapeño pepper (line 3). Then it starts a loop (lines 5–17) <strong>to</strong> take<br />

turns. In each round, the game picks one card at random for the player (or<br />

the computer) by calling the Pick() subroutine (line 6). If the picked card<br />

has a pepper on it (line 7), the game displays the winner’s name (line 9),<br />

and the game ends because the program moves out of the If loop and<br />

jumps from line 10 <strong>to</strong> line 17, bypassing the Go<strong>to</strong> loop on line 16.<br />

Otherwise, it displays The card is blank. You put it back in and shuffle<br />

the deck. (lines 12–13) <strong>to</strong> indicate that the player (or the computer) picked<br />

a blank card. The game then switches <strong>to</strong> the next player (line 15) and goes<br />

back <strong>to</strong> start a new round (line 16). This is how the statement on line 15<br />

works: if player is 1 (you, the user), then 3 – 1 is 2 (switching <strong>to</strong> the computer’s<br />

turn), and if player is 2 (the computer), then 3 – 2 is 1 (switching<br />

back <strong>to</strong> the user’s turn).<br />

Next, you’ll add the Pick() subroutine in Listing 10-5 <strong>to</strong> the bot<strong>to</strong>m of<br />

your program.<br />

1 Sub Pick<br />

2 If (player = 1) Then<br />

3 name = "The computer"<br />

136 Chapter 10

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!