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.

TRY IT OUT 18-7<br />

Write a program that takes a pig latin word as input and shows its original<br />

English word.<br />

Fix My Spelling<br />

Now we’ll develop a game that displays misspelled words and asks the player<br />

<strong>to</strong> enter the correct spelling. The game creates misspelled words by inserting<br />

a random letter at a random position in an English word. There could<br />

be more than one correct spelling of misspelled simple words. For example,<br />

if the game displays mwall, either mall or wall could be correct. To keep the<br />

game simple, we’ll ignore that possibility and insist on a particular spelling<br />

for the correct answer.<br />

First, we select the word <strong>to</strong> be misspelled<br />

from a predefined array of words and save the<br />

selected word in a variable named strIn. We<br />

then pick a random character randChar <strong>to</strong> insert<br />

in<strong>to</strong> strIn. The insertion position charPos is a<br />

random number between 1 and the length of<br />

strIn. Figure 18-6 shows the process of generating<br />

the misspelled word hewlp.<br />

We first extract the substring from letter 1<br />

<strong>to</strong> the letter at position charPos – 1 and assign<br />

it <strong>to</strong> strOut (because charPos is 3, this makes<br />

strOut = "he"). We then append randChar <strong>to</strong><br />

strOut (this makes strOut = "hew"). We extract<br />

the substring from position charPos <strong>to</strong> the end<br />

strIn<br />

strOut<br />

h e l p<br />

h e w l p<br />

w l p<br />

randChar<br />

at charPos 3<br />

Figure 18-6: Illustrating the<br />

process of generating misspelled<br />

words<br />

("lp" in this case) and append it <strong>to</strong> strOut (this makes strOut = "hewlp").<br />

Listing 18-8 shows the complete program. Make sure you download and<br />

open FixMySpelling.sb from this chapter’s folder <strong>to</strong> get the full list of the<br />

words we wrote for this program.<br />

1 ' FixMySpelling.sb<br />

2 words = "1=mountain;2=valley;...;22=animation;" ' See file for full list<br />

3<br />

4 While ("True") ' Runs forever<br />

5 strIn = words[Math.GetRandomNumber(Array.GetItemCount(words))]<br />

6 randChar = Text.GetCharacter(96 + Math.GetRandomNumber(26))<br />

7 charPos = Math.GetRandomNumber(Text.GetLength(strIn))<br />

8<br />

9 strOut = Text.GetSubText(strIn, 1, charPos - 1)<br />

10 strOut = strOut + randChar<br />

11 strOut = strOut + Text.GetSubTextToEnd(strIn, charPos)<br />

12<br />

13 TextWindow.Write("Enter correct spelling for [" + strOut + "]: ")<br />

14 ans = TextWindow.Read()<br />

15 ans = Text.ConvertToLowerCase(ans)<br />

16 If (ans = strIn) Then<br />

Advanced Text Magic 277

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

Saved successfully!

Ooh no, something went wrong!