Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Tic Tac Toe Game Problem
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
compstudent




PostPosted: Sun Feb 06, 2011 4:39 pm   Post subject: Tic Tac Toe Game Problem

I am making a simple two player tic tac toe game. I am having problems with having the the game draw a X and O when they are supposed to. The game is not done yet - I still need to add syntax to check if players won. Just need someone to point our what I am doing wrong.

Thanks



Turing:


%TIC TAC TOE GAME

setscreen ("graphics:600;600")


procedure drawx (spotx, spoty : int)
    var x1 := spotx * 100
    var y1 := spoty * 100
    var x2 := (spotx * 100) + 100
    var y2 := (spoty * 100) + 100
    drawline (x1, y1, x2, y2, red)
    drawline (x1, y1 + 100, x2, y2 - 100, red)
end drawx

procedure drawo (midx, midy : int)
    var x1 := (midx * 100) + 50
    var y1 := (midy * 100) + 50
    var rad := 50

    drawoval (x1, y1, rad, rad, blue)
end drawo

procedure drawgrid
    drawline (100, 0, 100, 300, black)
    drawline (200, 0, 200, 300, black)
    drawline (0, 100, 300, 100, black)
    drawline (0, 200, 300, 200, black)
end drawgrid

procedure game
    var x, y, z : int
    var p1, p2 : int := 0
    loop
        mousewhere (x, y, z)
        if z = 1 and x < 100 and y < 100 then
            if p2 < p1 then
                drawo (0, 0)
                p2:=p2+1
            else
                drawx (0, 0)
                p1:=p1+1
            end if
        elsif z = 1 and x < 100 and y < 200 and y > 100 then
            if p2 < p1 then
                drawo (0, 1)
                p2:=p2+1
            else
                drawx (0, 1)
                p1:=p1+1
            end if
        elsif z = 1 and x < 100 and y < 300 and y > 200 then
            if p2 < p1 then
                drawo (0, 2)
                p2:=p2+1
            else
                drawx (0, 2)
                p1:=p1+1
            end if
        elsif z = 1 and x > 100 and x < 200 and y < 100 then
            if p2 < p1 then
                drawo (1, 0)
                p2:=p2+1
            else
                drawx (1, 0)
                p1:=p1+1
            end if
        elsif z = 1 and x > 100 and x < 200 and y > 100 and y < 200 then
            if p2 < p1 then
                drawo (1, 1)
                p2:=p2+1
            else
                drawx (1, 1)
                p1:=p1+1
            end if
        elsif z = 1 and x > 100 and x < 200 and y > 200 and y < 300 then
            if p2 < p1 then
                drawo (1, 2)
                p2:=p2+1
            else
                drawx (1, 2)
                p1:=p1+1
            end if
        elsif z = 1 and x > 200 and x < 300 and y < 100 then
            if p2 < p1 then
                drawo (2, 0)
                p2:=p2+1
            else
                drawx (2, 0)
                p1:=p1+1
            end if
        elsif z = 1 and x > 200 and x < 300 and y > 100 and y < 200 then
            if p2 < p1 then
                drawo (2, 1)
                p2:=p2+1
            else
                drawx (2, 1)
                p1:=p1+1
            end if
        elsif z = 1 and x > 200 and x < 300 and y > 200 and y < 300 then
            if p2 < p1 then
                drawo (2, 2)
                p2:=p2+1
            else
                drawx (2, 2)
                p1:=p1+1
            end if

        end if
       
       

    end loop



end game
drawgrid
game

Sponsor
Sponsor
Sponsor
sponsor
huskiesgoaler34




PostPosted: Sun Feb 06, 2011 5:05 pm   Post subject: Re: Tic Tac Toe Game Problem

I am assuming that, after trying to play your game, you are trying to not draw both an x or an o each time the box is clicked. Right now, both are drawn on top of eachother. Am I right?
TokenHerbz




PostPosted: Sun Feb 06, 2011 5:13 pm   Post subject: RE:Tic Tac Toe Game Problem

if you add a delay(60) in the loop you'll see that both arn't drawn the same time, however you need to make sure that the grid is "clear" before you can "draw" on it, so that you cant draw on it again...

you could use a boolean for that.
huskiesgoaler34




PostPosted: Sun Feb 06, 2011 5:15 pm   Post subject: Re: Tic Tac Toe Game Problem

What I would do is set an x or an o to each player. So player1 is an o and player 2 is an x. If player one clicks a box, it draws a o. If player two clicks a box, then an x is drawn.
huskiesgoaler34




PostPosted: Sun Feb 06, 2011 5:16 pm   Post subject: Re: Tic Tac Toe Game Problem

Maybe stick to token's idea. His is easier. Very Happy
compstudent




PostPosted: Sun Feb 06, 2011 5:23 pm   Post subject: Re: Tic Tac Toe Game Problem

I'm not quite sure I completely understand tokens idea. I have put in the delay and noticed the difference. Now I just need to make sure that another symbol isn't drawn if the box is already taken.

Thanks for the replies.
compstudent




PostPosted: Sun Feb 06, 2011 5:26 pm   Post subject: Re: Tic Tac Toe Game Problem

Do you mean like a multi-dimensional array of boolean, and assign the variable true for the corresponding gird spot when it is drawn on.
compstudent




PostPosted: Sun Feb 06, 2011 6:05 pm   Post subject: Re: Tic Tac Toe Game Problem

I have changed it and added variables, just for two places on the board so far. Now for when the first player clicks a spot it draws the correct symbol then, when clicked again nothing happens which is good. However when you click the next box, both symbols show up.

I'm not sure whats wrong.

Turing:

procedure game
    var turn : boolean := true
    var x, y, z : int
    var p1, p2 : int := 0
    var board : array 0 .. 2, 0 .. 2 of char
    var full : array 0 .. 2, 0 .. 2 of boolean := init (false, false, false, false, false, false, false, false, false)
    loop
        mousewhere (x, y, z)
        if z = 1 and x < 100 and y < 100 and full (0, 0) = false then
            if turn = false then
                drawo (0, 0)
             
                turn := true
                full (0, 0) := true
                board (0, 0) := "o"
            end if
            if turn = true then
                drawx (0, 0)
               
                turn := false
                full (0, 0) := true
                board (0, 0) := "x"
            end if
        elsif z = 1 and x < 100 and y < 200 and y > 100 and full (0, 1) = false then
            if turn = false then
                drawo (0, 1)
                turn := true
                full (0, 1) := true
                board (0, 1) := "o"
                end if
            if turn = true then
                drawx (0, 1)
                turn := false
                full (0, 1) := true
                board (0, 1) := "x"
            end if

Sponsor
Sponsor
Sponsor
sponsor
huskiesgoaler34




PostPosted: Sun Feb 06, 2011 6:20 pm   Post subject: Re: Tic Tac Toe Game Problem

compstudent @ Sun Feb 06, 2011 6:05 pm wrote:
I have changed it and added variables, just for two places on the board so far. Now for when the first player clicks a spot it draws the correct symbol then, when clicked again nothing happens which is good. However when you click the next box, both symbols show up.

I'm not sure whats wrong.

Turing:

procedure game
    var turn : boolean := true
    var x, y, z : int
    var p1, p2 : int := 0
    var board : array 0 .. 2, 0 .. 2 of char
    var full : array 0 .. 2, 0 .. 2 of boolean := init (false, false, false, false, false, false, false, false, false)
    loop
        mousewhere (x, y, z)
        if z = 1 and x < 100 and y < 100 and full (0, 0) = false then
            if turn = false then
                drawo (0, 0)
             
                turn := true
                full (0, 0) := true
                board (0, 0) := "o"
            end if
           % if turn = true then
              %  drawx (0, 0)
               
                %turn := false
                %full (0, 0) := true
                %board (0, 0) := "x"
            %end if
        elsif z = 1 and x < 100 and y < 200 and y > 100 and full (0, 1) = false then
            if turn = false then
                drawo (0, 1)
                turn := true
                full (0, 1) := true
                board (0, 1) := "o"
                end if
            if turn = true then
                drawx (0, 1)
                turn := false
                full (0, 1) := true
                board (0, 1) := "x"
            end if



I think I found the problem. The commented part was the problem. After you click one box, it drew the correct symbol but when you clicked another box, it draws both symbols. Now, I made it so that when one of the boxes is clicked, the x appears. When you click another box, a circle appears.

Note: It only works for the first two boxes on the first column.
compstudent




PostPosted: Sun Feb 06, 2011 6:27 pm   Post subject: Re: Tic Tac Toe Game Problem

I notice that after you click a box an x will first appear, then the next box an O. But if i click the lower left box an x will appear, then if i click the one directly above it, both appear.
compstudent




PostPosted: Sun Feb 06, 2011 7:00 pm   Post subject: Re: Tic Tac Toe Game Problem

Nevermind - I have fixed that problem now. Just needed to add

Turing:

and full(0,0)=false then


to each of the embedded if statements. Thanks for the help, I'll probably be back with more problems.
RandomLetters




PostPosted: Sun Feb 06, 2011 7:44 pm   Post subject: RE:Tic Tac Toe Game Problem

I think it would help a lot if you looked into else and elsif statements for the inner conditional instead of adding more conditions.

(edit for clarity)
compstudent




PostPosted: Sun Feb 06, 2011 8:02 pm   Post subject: Re: Tic Tac Toe Game Problem

I have another problem. I have worked it out so that each symbol is drawn when it is supposed to be and so that there aren't two in the same square. Now my problem is that there seems to be a delay in drawing them. For example if I click on it will draw. Next one nothing. Next one I click both show up: the one before where I clicked and the new one. I noticed though, if I click pause after one doesn't show up, it suddenly appears.

I am not sure what is the problem.

Turing:


%TIC TAC TOE GAME

setscreen ("graphics:300;310")
var turn : boolean := true
var x, y, z : int
var p1, p2 : int := 0
var board : array 0 .. 2, 0 .. 2 of char
var full : array 0 .. 2, 0 .. 2 of boolean := init (false, false, false, false, false, false, false, false, false)
var name1, name2 : string

procedure drawx (spotx, spoty : int)
    var x1 := spotx * 100
    var y1 := spoty * 100
    var x2 := (spotx * 100) + 100
    var y2 := (spoty * 100) + 100
    drawline (x1, y1, x2, y2, red)
    drawline (x1, y1 + 100, x2, y2 - 100, red)
end drawx

procedure start
    put "Enter player ones name"
    get name1
    put "Enter player twos name"
    get name2
    cls
    put name1, " is x."
    put name2, " is o."
    put name1, " starts."
    put "To play click on the square you want to put your symbol"
    locate (10, 10)
    put "The game starts in: "
    delay (3000)
    cls
    locate (10, 20)
    put "5"
    delay (1000)
    cls
    locate (10, 20)
    put "4"
    delay (1000)
    cls
    locate (10, 20)
    put "3"
    delay (1000)
    cls
    locate (10, 20)
    put "2"
    delay (1000)
    cls
    locate (10, 20)
    put "1"
    delay (1000)
    cls
    locate (10, 20)
    put "Go"
    delay (500)
    cls
end start


procedure drawo (midx, midy : int)
    var x1 := (midx * 100) + 50
    var y1 := (midy * 100) + 50
    var rad := 50

    drawoval (x1, y1, rad, rad, blue)
end drawo

procedure drawgrid
    drawline (100, 0, 100, 300, black)
    drawline (200, 0, 200, 300, black)
    drawline (0, 100, 300, 100, black)
    drawline (0, 200, 300, 200, black)
end drawgrid

procedure game

    loop
        mousewhere (x, y, z)
        if z = 1 and x < 100 and y < 100 and full (0, 0) = false then
            if turn = false and full (0, 0) = false then
                drawo (0, 0)

                turn := true
                full (0, 0) := true
                board (0, 0) := "o"
            end if
            if turn = true and full (0, 0) = false then
                drawx (0, 0)

                turn := false
                full (0, 0) := true
                board (0, 0) := "x"
            end if

        elsif z = 1 and x < 100 and y < 200 and y > 100 and full (0, 1) = false then
            if turn = false and full (0, 1) = false then
                drawo (0, 1)
                turn := true
                full (0, 1) := true
                board (0, 1) := "o"
            end if
            if turn = true and full (0, 1) = false then
                drawx (0, 1)
                turn := false
                full (0, 1) := true
                board (0, 1) := "x"
            end if
        elsif z = 1 and x < 100 and y < 300 and y > 200 and full (0, 2) = false then
            if turn = false and full (0, 2) = false then
                drawo (0, 2)
                turn := true
                full (0, 2) := true
                board (0, 2) := "o"
            end if
            if turn = true and full (0, 2) = false then
                drawx (0, 2)
                turn := false
                full (0, 2) := true
                board (0, 2) := "x"
            end if
        elsif z = 1 and x > 100 and x < 200 and y < 100 then
            if turn = false and full (1, 0) = false then
                drawo (1, 0)
                turn := true
                full (1, 0) := true
                board (1, 0) := "o"
            end if
            if turn = true and full (1, 0) = false then
                drawx (1, 0)
                turn := false
                full (1, 0) := true
                board (1, 0) := "x"
            end if
        elsif z = 1 and x > 100 and x < 200 and y > 100 and y < 200 then
            if turn = false and full (1, 1) = false then
                drawo (1, 1)
                turn := true
                full (1, 1) := true
                board (1, 1) := "o"
            end if
            if turn = true and full (1, 1) = false then
                drawx (1, 1)
                turn := false
                full (1, 1) := true
                board (1, 1) := "x"
            end if
        elsif z = 1 and x > 100 and x < 200 and y > 200 and y < 300 then
            if turn = false and full (1, 2) = false then
                drawo (1, 2)
                turn := true
                full (1, 2) := true
                board (1, 2) := "o"
            end if
            if turn = true and full (1, 2) = false then
                drawx (1, 2)
                turn := false
                full (1, 2) := true
                board (1, 2) := "x"
            end if
        elsif z = 1 and x > 200 and x < 300 and y < 100 then
            if turn = false and full (2, 0) = false then
                drawo (2, 0)
                turn := true
                full (2, 0) := true
                board (2, 0) := "o"
            end if
            if turn = true and full (2, 0) = false then
                drawx (2, 0)
                turn := false
                full (2, 0) := true
                board (2, 0) := "x"
            end if
        elsif z = 1 and x > 200 and x < 300 and y > 100 and y < 200 then
            if turn = false and full (2, 1) = false then
                drawo (2, 1)
                turn := true
                full (2, 1) := true
                board (2, 1) := "o"
            end if
            if turn = true and full (2, 1) = false then
                drawx (2, 1)
                turn := false
                full (2, 1) := true
                board (2, 1) := "x"
            end if
        elsif z = 1 and x > 200 and x < 300 and y > 200 and y < 300 then
            if turn = false and full (2, 2) = false then
                drawo (2, 2)
                turn := true
                full (2, 2) := true
                board (2, 2) := "o"
            end if
            if turn = true and full (2, 2) = false then
                drawx (2, 2)
                turn := false
                full (2, 2) := true
                board (2, 2) := "x"
            end if

        end if



    end loop



end game
drawgrid
game


Note: Not complete. Yet to add prompts and checking to see if a player won.
huskiesgoaler34




PostPosted: Sun Feb 06, 2011 8:20 pm   Post subject: Re: Tic Tac Toe Game Problem

It works perfectly for me. I don't see a problem. The correct symbol is drawn without any delays. No bugs. I have tested it out and it works fine.
compstudent




PostPosted: Sun Feb 06, 2011 8:32 pm   Post subject: Re: Tic Tac Toe Game Problem

Thanks, I guess it is just my computer, since I just tried it on another and it worked fine.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 29 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: