Site hosted by Angelfire.com: Build your free website today!

Tic Tac Toe

Introduction

This activity is primarely a work about buttons. It allow kids work with positions an coordinates too.

It could also be a larger project; First adding instructions to detect a line of three, then building up a strategy for the computer to allow him to play against human.

Building the playground.

The playground consist of nine buttons. The global variable player contains the mark of the next player (X or O). Each button when pressed , displays the content of the variable player then changes the mark in the variable player.

Load the file interface.log and type interface in the Commander.
A window apears. Click on button.
Fill the blanks. Use s1 for the name of square 1 .The input box Display is for what will be written in the button let it blank. The input box Action is the command which will be executed when the button is pressed ; write : playat "s1.

Now you have to show where you want the button. Press the button selection and select a sqare in the window Mswlogo Screen (as usual click and drag).

Press the button Create Your button will be created and in the Commander the corresponding command is written.

For the other buttons, correct this line and execute it. You have to change the button's name, the button's name in the action box and the position of the button. The four numbers in the command are the rectangle of the button ( X, Y, widh and height). For button s2, add X to width and use the result as new X. Do the same with s3. For s4 correct s1 changing Y (substract the height from Y). Go on using s2 to get s5, s3 to get s6 and build the last row from the second row.

Open the editor to define the procedure init. Init will create the buttons next time we will use the program. Copy all the commands that create buttons in init. Then add make "player "X as last line of the procedure. Select this line and use test in the menu of the editor, this will create the gobal variable player containing a X.

Define playat :


to playat :sq
buttonupdate :sq :player
ifelse :player = "X [make "player "O][make "player "X]
end

Define new that clear all the buttons.

to new
repeat 9[buttonupdate word "s repcount " ]
end

Using the button window, create a button newto start a new game.

Upgrade

If you play on a square already occupied by the opponent, you can change the mark to yours. To prevent this, in the procedure new put the empty word in the global variables s1 s2 s3 s4 ... s9 and put the mark of the player in the corresponding variable when a square is used.

The updated procedure new becomes:

to new
repeat 9[buttonupdate word "s repcount " make word "s repcount "]
end

And the procedure playat :


to playat :sq
if not emptyp thing :sq[messagebox "Error se :sq [already used]stop]
buttonupdate :sq :player
make :sq :player
ifelse :player = "X [make "player "O][make "player "X]
end

Email: dreyfuss-p-a@bal.ge-dip.etat-ge.ch