investigating with logo. part three

4
Investigating with LOGO. Part Three Author(s): Trevor Fletcher Source: Mathematics in School, Vol. 22, No. 3 (May, 1993), pp. 6-8 Published by: The Mathematical Association Stable URL: http://www.jstor.org/stable/30214999 . Accessed: 08/04/2014 17:02 Your use of the JSTOR archive indicates your acceptance of the Terms & Conditions of Use, available at . http://www.jstor.org/page/info/about/policies/terms.jsp . JSTOR is a not-for-profit service that helps scholars, researchers, and students discover, use, and build upon a wide range of content in a trusted digital archive. We use information technology and tools to increase productivity and facilitate new forms of scholarship. For more information about JSTOR, please contact [email protected]. . The Mathematical Association is collaborating with JSTOR to digitize, preserve and extend access to Mathematics in School. http://www.jstor.org This content downloaded from 81.23.53.34 on Tue, 8 Apr 2014 17:02:05 PM All use subject to JSTOR Terms and Conditions

Upload: trevor-fletcher

Post on 23-Dec-2016

219 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Investigating with LOGO. Part Three

Investigating with LOGO. Part ThreeAuthor(s): Trevor FletcherSource: Mathematics in School, Vol. 22, No. 3 (May, 1993), pp. 6-8Published by: The Mathematical AssociationStable URL: http://www.jstor.org/stable/30214999 .

Accessed: 08/04/2014 17:02

Your use of the JSTOR archive indicates your acceptance of the Terms & Conditions of Use, available at .http://www.jstor.org/page/info/about/policies/terms.jsp

.JSTOR is a not-for-profit service that helps scholars, researchers, and students discover, use, and build upon a wide range ofcontent in a trusted digital archive. We use information technology and tools to increase productivity and facilitate new formsof scholarship. For more information about JSTOR, please contact [email protected].

.

The Mathematical Association is collaborating with JSTOR to digitize, preserve and extend access toMathematics in School.

http://www.jstor.org

This content downloaded from 81.23.53.34 on Tue, 8 Apr 2014 17:02:05 PMAll use subject to JSTOR Terms and Conditions

Page 2: Investigating with LOGO. Part Three

INVES TIGA TING WITH

logo

PART THREE by Trevor Fletcher

Probability Simulations The activities described in Richard English's article "Developing a feel for probability" (March 1992) provide opportunities for many types of investigation. He remarks that it is no easy task to compute the theoretical probabilities in some of the games considered. Computer simulation provides a half-way house between empirical investigation and theories which are too difficult.

Logo is a good language for such simulations. Consider, for example, the first game which English describes - Ups and Downs. There is a chart with 10 cells and moves are decided by tossing a coin. If the result is "heads" you move forward one space, and if it is "tails" you move forward two. The second cell is marked LOSE and the tenth is marked WIN. Arrows go from cell 5 to cell 3; from 4 to 7; from 6 to 10 (although you cannot in fact get on to this path); from 8 to 10; and from 9 to 2. We provide below the basic minimum for a simulation of the game, and we also provide a mechanism for playing a batch of games and counting the numbers won and lost. It is helpful to label the third cell A and the seventh cell B. Bearing in mind the transfers between cells which result from following the arrows it can be seen that the player moves between a limited number of "states" - START, LOSE, A, B, WIN.

We must first simulate the tossing of a coin. Logo provides a command RANDOM for such purposes. Go to the command line and experiment. If you say PRINT RANDOM 2, and repeat this a few times, you will see that sometimes the machine prints 0, and sometimes it prints 1. So we can define a procedure COIN,

TO COIN OP RANDOM 2 END

for use in other procedures later. Now we must specify the action to be taken in the five

"active" cells listed above. It is useful to print out the name of each cell as it is visited, so define

TO START PR " PR "START IF COIN=0 [LOSE] [A] END

When the game starts print a line space and print the word START. Then toss the coin, and if the result is 0 take this as "heads" and move on to the LOSE cell. If the result is not 0 (i.e. it is 1, which is "tails") move on to cell A. At cell A the action required is

TO A PR "A IF COIN=0 [B][A] END

Print A, toss the coin, and move as appropriate to B or back to A. At B the action is

6 Mathematics in School, May 1993

This content downloaded from 81.23.53.34 on Tue, 8 Apr 2014 17:02:05 PMAll use subject to JSTOR Terms and Conditions

Page 3: Investigating with LOGO. Part Three

UPS AND DOWNS

BUT IS IT A FAIR

GAME?

WIN

Place counter on start. Flick a coin

- Heads, move forward one space

- Tails, move forward two spaces.

Watch for the arrows.

Keep flicking until you START LOSE win or lose.

TO B PR "B IF COIN=0 [WIN][LOSE] END

Once again, print B to record where we are, toss the coin and move as appropriate. It remains to say what to do at the WIN and LOSE cells.

TO WIN MAKE "wins :wins+ PR [Game WON] END

TO LOSE MAKE "losses:losses + 1 PR [Game LOST] END

The variables called "wins and "losses have still to be set up. We need a procedure to start a batch of trials, to start up the wins and losses counters and to print the results at the finish.

TO TRY :times MAKE "wins 0 MAKE "losses 0 REPEAT:times [START] PR "PR (SE :times "games :wins "wins :losses "losses) END

Now if we command TRY 10, ten games take place and the numbers won and lost are printed out at the end. If it is new to you, note how conveniently Logo can be made to print out sentences containing the values of variables and explanatory text. It will be found that roughly three

quarters of the games are lost, and it is instructive to try to explain this.

It is interesting to investigate how many moves games last. It is not difficult to insert another counter to do this. You have a choice of method. You can use the MAKE idea used with "wins and "losses (these are global variables), or you may introduce a counter into the defining lines of the appropriate procedures and see that it is updated as you move from procedure to procedure. Such a variable is called a local variable; and the use of "score below provides an example.

The techniques employed above have wider applications. Instead of throwing a coin one can move between procedures by providing a set of choices and having the user input a single key instruction; in this way one can construct adventure games and develop a variety of programming activities.

The Two Dice Difference Game The fourth game which English describes involves move- ment right and left along a chart, with the move decided by throwing two dice. If the difference between the two numbers showing on the dice is 0, 1 or 2 a move is made to the left, and if the difference is 3, 4 or 5 a move is made to the right.

The two dice may be simulated by defining

TO ROLL_ DICE OP ABS ((RANDOM 6)-(RANDOM 6)) END

ABS means absolute value, and since most Logos do not contain this function it is necessary to give the definition

TO ABS :number IF :number>0 [OP :number][OP -:number] END

Check these definitions by typing PR ROLL_DICE a

Mathematics in School, May 1993 7

This content downloaded from 81.23.53.34 on Tue, 8 Apr 2014 17:02:05 PMAll use subject to JSTOR Terms and Conditions

Page 4: Investigating with LOGO. Part Three

Roll two dice

Work out difference

0, 1 or 2 move ONE 3, 4 or 5 move ONE

space this way. space this way.

Repeat until you win or lose.

WIN I

LOSE

few times, and see that you get numbers in the range 0, 1, 2, 3, 4, 5.

The START cell is in the middle, there are three cells and then the WIN position to the left; and three cells and then the LOSE position to the right. If we work as before we would need nine procedures for the nine locations. But this time there are no transfer arrows to complicate matters so it is simpler just to keep a score. Score 1 for a step in the WIN direction, and -1 for a step in the LOSE direction.

The WIN and LOSE procedures can be exactly as in the last game; and the procedure at all of the other seven cells can be covered by the one definition

TO MOVE :score PR :score IF:score=4 [WIN STOP] IF :score= -4 [LOSE STOP] IF ROLL_DICE<3 [MOVE :score+l][MOVE :score- 1 END

Note how the values of the local variable :score are updated as things proceed. To simulate a game simply type in MOVE 0. (You can experiment with starting with other scores if you wish.) In this game you win more often than you lose. You can run a batch of games and count the wins and losses with the same procedure TRY as before, except that you must have MOVE 0 instead of START. The use of the instruction STOP in the MOVE procedure is a tricky matter. It really is needed - try to see why.

It may help to have the machine pause until a key is pressed after each game of a batch. PAUSE is already defined in some versions of Logo, and it does not do quite what is wanted here, but we can define HOLD as follows

TO HOLD MAKE "ZZ RC END

This really looks like computer gobble-de-gook! The point is that RC means "read character" and the computer waits for some key on the keyboard to be pressed. Something has to be done with the character which is entered, so the variable "ZZ is used to receive this value - it is not used for anything else. (In BASIC the instruction A= GET can be used for exactly the same purpose.) You can now alter the "repeat" line in the TRY procedure to read

REPEAT :times [MOVE 0 HOLD]

It is well worth while to try to illustrate the development of the game by introducing some turtle graphics. The essential idea is to move the turtle right and left across the screen according to the current score. The necessary turtle commands have to be inserted in the procedures which we have already.

The TRY procedure needs to have something like HOME RT 90 PU put inside the repeat loop so that the turtle starts at the centre of the screen, facing right with the pen up. Alter a line of the MOVE procedure to read

IF ROLL_ DICE<3 [BK 150 MOVE :score+ 1 ][FD 150 MOVE :score- 1]

Now as the game proceeds the turtle skips back and forth and comes to rest in the WIN or LOSE position as the case may be. On modern microcomputers the movement may be faster than you want. There are various ways of slowing it down, and there are many other ways in which this simple display can be developed. Try, for example, the effect of SETPENTYPE 3, working with the pen down (if you have this command in your version of the language). A good investigation frequently starts off more than it finishes!

8 Mathematics in School, May 1993

This content downloaded from 81.23.53.34 on Tue, 8 Apr 2014 17:02:05 PMAll use subject to JSTOR Terms and Conditions