19.04.2017 Views

Learn to Program with Small Basic

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

To create this program, we’ll use an array named ball <strong>to</strong> hold the<br />

numbers of the 10 balls (ball[1] = 1, ball[2] = 2, …, ball[10] = 10). Then<br />

the program selects a random number between 1 and 10 <strong>to</strong> pick a ball. For<br />

example, if it picks number 2, it sets ball[2] = 0 <strong>to</strong> indicate that the second<br />

ball has been selected and is no longer available. Then it selects another<br />

random number. Let’s say that the second number is also 2. First, the program<br />

checks ball[2]. Because it’s 0, it knows that ball[2] has already been<br />

selected (you can’t take the same ball out of the bag twice!), and it picks<br />

another random number. It continues this until it selects five different<br />

random numbers. The complete program is shown in Listing 15-2.<br />

1 ' RandomSelect.sb<br />

2 For N = 1 To 10 ' Puts the 10 balls in an array<br />

3 ball[N] = N<br />

4 EndFor<br />

5<br />

6 For N = 1 To 5 ' Loops <strong>to</strong> select 5 balls<br />

7 idx = Math.GetRandomNumber(10) ' Gets random ball number<br />

8 While (ball[idx] = 0) ' Ball already selected<br />

9 idx = Math.GetRandomNumber(10) ' Gets another number<br />

10 EndWhile<br />

11<br />

12 TextWindow.Write(ball[idx] + ", ") ' Displays selected ball<br />

13 ball[idx] = 0 ' Marks it out (taken)<br />

14 EndFor<br />

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

Listing 15-2: Randomly selecting five different balls<br />

The program starts by setting ball[1] = 1, ball[2] = 2, . . . , ball[10] = 10<br />

in a For loop (lines 2–4). It then begins a loop <strong>to</strong> select the five balls (line 6).<br />

In each iteration of the loop, it picks a random number, idx, between 1 and 10<br />

(line 7). A While loop continually sets idx until ball[idx] is not 0 (lines 8–10).<br />

After selecting a unique ball number, the program displays the number<br />

(line 12), and then it marks that ball as selected by setting its array element<br />

<strong>to</strong> 0 (line 13) so it doesn’t try <strong>to</strong> select that number again. Here’s a sample<br />

run of this program:<br />

5, 9, 10, 1, 2,<br />

Run the program <strong>to</strong> see which numbers you get!<br />

A Magic 8 Ball<br />

In this example, we’ll write a program that simulates a Magic 8 Ball game.<br />

A user asks a yes or no question, and the computer answers. Of course, it’s<br />

just for fun, so don’t use it <strong>to</strong> make important decisions like choosing your<br />

spouse or house! The complete program is shown in Listing 15-3.<br />

1 ' Magic8Ball.sb<br />

2 ans[1] = "It is certain. Like really, really certain."<br />

Grouping Data in One-Dimensional Arrays 217

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

Saved successfully!

Ooh no, something went wrong!