Two Player
Home What is Logo? Getting Logo Logo Lessons Tic-Tac-Toe Downloads

 

Design
Programming
Initialize
Grid
Winning
Two Player
Single Player

Back Up Next

Mouse Click

How do we get logo to allow us to click on the box to place the piece there? This isn't straight forward, so I'll give you the definitions needed. The first word will be used by a MSWLogo word called "mouseon". It will call my word when someone presses the left mouse button, so we'll call it "mousepressed". We check to see if the mouse is in a valid box with no playing piece, and if so, will set a global variable "selection" to the box number.

Editor
to mousepressed
    ;
    ; hard stuff, look to see which grid item mouse is over,
    ; when it was pressed
    ;
    localmake "curpos mousepos
    localmake "x item 1 :curpos
    localmake "y item 2 :curpos
    localmake "suggest -1
    if (AND :x>-150 :x<-50 :y>-150 :y<-50) [make "suggest 0]
    if (AND :x>-50 :x<50 :y>-150 :y<-50) [make "suggest 1]
    if (AND :x>50 :x<150 :y>-150 :y<-50) [make "suggest 2]
    if (AND :x>-150 :x<-50 :y>-50 :y<50) [make "suggest 3]
    if (AND :x>-50 :x<50 :y>-50 :y<50) [make "suggest 4]
    if (AND :x>50 :x<150 :y>-50 :y<50) [make "suggest 5]
    if (AND :x>-150 :x<-50 :y>50 :y<150) [make "suggest 6]
    if (AND :x>-50 :x<50 :y>50 :y<150) [make "suggest 7]
    if (AND :x>50 :x<150 :y>50 :y<150) [make "suggest 8]
    if (not :suggest<0) [
        ;
        ; we don't want to do the test if :suggest is –1
        ; so we do this in the part of if that only gets
        ; executed when :suggest is >= 0.
        ;
        if (grid.empty :suggest) [
            make "selection :suggest
        ]
    ]
end

Here's the corresponding word that calls "mouseon".

Editor
to yourturn
    ;
    ; don't need to understand this more than "it works"
    ; waits until the mouse is pressed that selects a valid
    ; empty position
    ;
    make "selection -1
    mouseon [mousepressed] [] [] [] []
    while [:selection < 0] [wait 1]
    mouseoff
    place :selection
end

Try this:

Commander
init
yourturn
yourturn
yourturn

Yeah! Some interaction!

Two Player

We can now get a two-player game working with the following word that starts and runs the game:

Editor
to tictactoe2
    ;
    ; play tic tac toe (two player)
    ; we flip first to counteract the flip in the loop
    ;
    init
    flip
    do.while [flip yourturn] [canmove]
end

Try it out!

Commander
tictactoe2

Back Up Next

 

Comments to WebMaster