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 Previous  1, 2
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
huskiesgoaler34




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

Your welcome. Post the code when your done with determining the winner and ending the game.
Sponsor
Sponsor
Sponsor
sponsor
TokenHerbz




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

you could use a 2D array for your "grid" to, and types are good at storing "multiple data" though i'd use a class myself.
compstudent




PostPosted: Mon Feb 07, 2011 7:15 pm   Post subject: Re: Tic Tac Toe Game Problem

Here is the game now almost complete. The problems I am having now are that I need it to stop when a player one - you will notice it doesn't, and then to allow the players to play again. If I make a play again procedure i have to put it above the game procedure so that the game can recognize it, but then I cannot call the game procedure in the play again one.

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
var timoe:int:=0
var again:string:="Yes"

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


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 check
%Checks veritcal
    if board (0, 0) = board (0, 1) and board (0, 1) = board (0, 2) and full(0,0) = true and full(0,1) = true and full(0,2) = true then
        if turn = false then
            put "Congradulations", name1, "you won!"
        elsif turn = true then
            put "Congradulations", name2, "you won!"
        end if
    end if
    if board (1, 0) = board (1, 1) and board (1, 1) = board (1, 2) and full(1,0) = true and full(1,1) = true and full(1,2) = true then
        if turn = false then
            put "Congradulations", name1, "you won!"
        elsif turn = true then
            put "Congradulations", name2, "you won!"
        end if
    end if
    if board (2, 0) = board (2, 1) and board (2, 1) = board (2, 2) and full(2,0) = true and full(2,1) = true and full(2,2) = true then
        if turn = false then
            put "Congradulations", name1, "you won!"
        elsif turn = true then
            put "Congradulations", name2, "you won!"
        end if
    end if
    %Checks Horizontal
    if board (0, 0) = board (1, 0) and board (1, 0) = board (2, 0) and full(0,0) = true and full(1,0) = true and full(2,0) = true then
        if turn = false then
            put "Congradulations", name1, "you won!"
        elsif turn = true then
            put "Congradulations", name2, "you won!"
        end if
    end if
    if board (0, 1) = board (1, 1) and board (1, 1) = board (2, 1) and full(0,1) = true and full(1,1) = true and full(2,1) = true then
        if turn = false then
            put "Congradulations", name1, "you won!"
        elsif turn = true then
            put "Congradulations", name2, "you won!"
        end if
    end if
    if board (0, 2) = board (1, 2) and board (1, 2) = board (2, 2) and full(0,2) = true and full(1,2) = true and full(2,2) = true then
        if turn = false then
            put "Congradulations", name1, "you won!"
        elsif turn = true then
            put "Congradulations", name2, "you won!"
        end if
    end if
    %checks diagonal
    if board(0,0) = board(1,1) and board(1,1) = board(2,2) and full(0,0) = true and full(1,1) = true and full(2,2) = true then
            if turn = false then
            put "Congradulations", name1, "you won!"
        elsif turn = true then
            put "Congradulations", name2, "you won!"
        end if
    end if
    if board(0,2) = board(1,1) and board(1,1) = board (2,0) and full(0,2) = true and full(1,1) = true and full(2,0) = true then
            if turn = false then
            put "Congradulations", name1, "you won!"
        elsif turn = true then
            put "Congradulations", name2, "you won!"
        end if
    end if
end check

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"
                check
            end if
            if turn = true and full (0, 0) = false then
                drawx (0, 0)

                turn := false
                full (0, 0) := true
                board (0, 0) := "x"
                check
            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"
                check
            end if
            if turn = true and full (0, 1) = false then
                drawx (0, 1)
                turn := false
                full (0, 1) := true
                board (0, 1) := "x"
                check
            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"
                check
            end if
            if turn = true and full (0, 2) = false then
                drawx (0, 2)
                turn := false
                full (0, 2) := true
                board (0, 2) := "x"
                check
            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"
                check
            end if
            if turn = true and full (1, 0) = false then
                drawx (1, 0)
                turn := false
                full (1, 0) := true
                board (1, 0) := "x"
                check
            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"
                check
            end if
            if turn = true and full (1, 1) = false then
                drawx (1, 1)
                turn := false
                full (1, 1) := true
                board (1, 1) := "x"
                check
            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"
                check
            end if
            if turn = true and full (1, 2) = false then
                drawx (1, 2)
                turn := false
                full (1, 2) := true
                board (1, 2) := "x"
                check
            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"
                check
            end if
            if turn = true and full (2, 0) = false then
                drawx (2, 0)
                turn := false
                full (2, 0) := true
                board (2, 0) := "x"
                check
            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"
                check
            end if
            if turn = true and full (2, 1) = false then
                drawx (2, 1)
                turn := false
                full (2, 1) := true
                board (2, 1) := "x"
                check
            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"
                check
            end if
            if turn = true and full (2, 2) = false then
                drawx (2, 2)
                turn := false
                full (2, 2) := true
                board (2, 2) := "x"
                check
            end if

        end if



    end loop



end game
drawgrid
game
ProgrammingFun




PostPosted: Mon Feb 07, 2011 9:47 pm   Post subject: RE:Tic Tac Toe Game Problem

Have you heard of forward procedure?
It enables you to do the following:

Turing:

forward procedure B

proc A
     put "Hello"
     %Call Procedure B
end A

proc B
    put "Bye"
end B


It was something like the above...I do not completely remember...
DemonWasp




PostPosted: Tue Feb 08, 2011 8:08 am   Post subject: RE:Tic Tac Toe Game Problem

Even if you do resolve the circular reference as ProgrammingFun says, you'll eventually run into stack overflow problems that way. Better to have a replay loop that exits if the user doesn't want to replay the game, and loops if they do.
TokenHerbz




PostPosted: Tue Feb 08, 2011 10:08 am   Post subject: RE:Tic Tac Toe Game Problem

I Think the Best way for you (least work) to fix this problem would be to add a boolean Value to check for when the game is won.

So in your procedure Check ( I would rework this so its not so repetitive, And make this into a function. If a player has won it turns this and use that in your game loop to exit the look when this occurs..

After if you want, you could even reset your variables after the loop still inside the game loop.

Create another loop to cycle threw your two procedures to keep the game going.
TokenHerbz




PostPosted: Tue Feb 08, 2011 10:39 am   Post subject: RE:Tic Tac Toe Game Problem

I went ahead and Did some re-working of your code to give you an example of some things your doing wrong. This is to help you better understand what to look for and how to problem solve things for the rest of your game and/or other programs your making.

your OLD CODE:
Turing:

procedure check
%Checks veritcal
    if board (0, 0) = board (0, 1) and board (0, 1) = board (0, 2) and full(0,0) = true and full(0,1) = true and full(0,2) = true then
        if turn = false then
            put "Congradulations", name1, "you won!"
        elsif turn = true then
            put "Congradulations", name2, "you won!"
        end if
    end if
    if board (1, 0) = board (1, 1) and board (1, 1) = board (1, 2) and full(1,0) = true and full(1,1) = true and full(1,2) = true then
        if turn = false then
            put "Congradulations", name1, "you won!"
        elsif turn = true then
            put "Congradulations", name2, "you won!"
        end if
    end if
    if board (2, 0) = board (2, 1) and board (2, 1) = board (2, 2) and full(2,0) = true and full(2,1) = true and full(2,2) = true then
        if turn = false then
            put "Congradulations", name1, "you won!"
        elsif turn = true then
            put "Congradulations", name2, "you won!"
        end if
    end if
    %Checks Horizontal
    if board (0, 0) = board (1, 0) and board (1, 0) = board (2, 0) and full(0,0) = true and full(1,0) = true and full(2,0) = true then
        if turn = false then
            put "Congradulations", name1, "you won!"
        elsif turn = true then
            put "Congradulations", name2, "you won!"
        end if
    end if
    if board (0, 1) = board (1, 1) and board (1, 1) = board (2, 1) and full(0,1) = true and full(1,1) = true and full(2,1) = true then
        if turn = false then
            put "Congradulations", name1, "you won!"
        elsif turn = true then
            put "Congradulations", name2, "you won!"
        end if
    end if
    if board (0, 2) = board (1, 2) and board (1, 2) = board (2, 2) and full(0,2) = true and full(1,2) = true and full(2,2) = true then
        if turn = false then
            put "Congradulations", name1, "you won!"
        elsif turn = true then
            put "Congradulations", name2, "you won!"
        end if
    end if
    %checks diagonal
    if board(0,0) = board(1,1) and board(1,1) = board(2,2) and full(0,0) = true and full(1,1) = true and full(2,2) = true then
            if turn = false then
            put "Congradulations", name1, "you won!"
        elsif turn = true then
            put "Congradulations", name2, "you won!"
        end if
    end if
    if board(0,2) = board(1,1) and board(1,1) = board (2,0) and full(0,2) = true and full(1,1) = true and full(2,0) = true then
            if turn = false then
            put "Congradulations", name1, "you won!"
        elsif turn = true then
            put "Congradulations", name2, "you won!"
        end if
    end if
end check


The purpose of this check is obviously to see if the player has won the game. It's called like 16 times or so in the main loop, Which is absolutely not required. As you can see in your game loop we go to a first "if" statement. This goes step by step down the list until the end. You have "check" in every sub if statement inside that main one. All that is required is the check is in before the end if on the main if statement, Hell It doesn't even need to be inside this if statement at all. But if you wanted it inside there, it would have a correct place.

The thing about programming is even you retype a ton of your code, and the game still seems to function correctly, It's Bad to do it for several reasons, the main being it's hard to change around, and/or fix bugs. Very time consuming.

So First example to solving your question, Is how do we make this "check" work? Well I went ahead and redid that procedure into a Function, (which returns a value) and renamed this "Win_Game". Here is the source:

Turing:

fcn Game_Won : boolean
    if board (0, 0) = board (0, 1) and board (0, 1) = board (0, 2) and full (0, 0) = true and full (0, 1) = true and full (0, 2) = true or %%Vertical check
            board (1, 0) = board (1, 1) and board (1, 1) = board (1, 2) and full (1, 0) = true and full (1, 1) = true and full (1, 2) = true or
            board (2, 0) = board (2, 1) and board (2, 1) = board (2, 2) and full (2, 0) = true and full (2, 1) = true and full (2, 2) = true or
            board (0, 0) = board (1, 0) and board (1, 0) = board (2, 0) and full (0, 0) = true and full (1, 0) = true and full (2, 0) = true or %%Horizontal check
            board (0, 1) = board (1, 1) and board (1, 1) = board (2, 1) and full (0, 1) = true and full (1, 1) = true and full (2, 1) = true or
            board (0, 2) = board (1, 2) and board (1, 2) = board (2, 2) and full (0, 2) = true and full (1, 2) = true and full (2, 2) = true or
            board (0, 0) = board (1, 1) and board (1, 1) = board (2, 2) and full (0, 0) = true and full (1, 1) = true and full (2, 2) = true or %%Diagnal check
            board (0, 2) = board (1, 1) and board (1, 1) = board (2, 0) and full (0, 2) = true and full (1, 1) = true and full (2, 0) = true then
        if turn = false then
            put "Congradulations ", name1, " you won!"
        elsif turn = true then
            put "Congradulations ", name2, " you won!"
        end if
        Input.Pause %%to show the players who won
        result true
    end if
    result false
end Game_Won


How I used this is just as important, I don't call this more then "ONCE" in the main loop, Here lets take a look and understand it!
Turing:

exit when Game_Won = true %%exit the game loop

So what i did was create a function that returns a true/false depending on circumstances, this case being if a player has won or not. I put this check right at the very end of the game procedure so how it goes is "start game -> player moves -> check if win -> if win exit else continue -> other player moves -> rinse and repeat.

Now when the Player does win, and we exit the game proc loop We are still in the procedure, so why not reset variables and clear the screen so we may play again!!
Turing:

 %%Reset variables here if you want to
    for r : 0 .. 2
        for c : 0 .. 2
            board (r, c) := ""
            full (r, c) := false
        end for
    end for
    cls %%Clear the screen


I should also mention i've changed your Bored array to string, init values of blank ""x9 times.
Turing:

var board : array 0 .. 2, 0 .. 2 of string := init ("","","","","","","","","")


Now what have we fixed? We made it possible to check if the game has won or not, and leave the game according to that (FIXED function). We reset variables and clear the screen so may play again!! Awesome!!! Now for the final thing, we loop our procs!! and we are ready for endless tic tac toe!

Turing:

loop
    drawgrid
    game
end loop


*********************
*********************
So Now that you have your game working, YOUR not DONE yet... Oh noes, no no no!!

I show'd you something in your check proc, how to minimize all this "re-do-re-write codes"...

What you need to do now, Is re-write your proc game to minimize all that repeat codes. Just check how its supposed to work and when you fix/clean that up, post it here for some BITS!

Good luck !!! PS: After you touch your code up, you can add more features to your game very easily. Cheers!
compstudent




PostPosted: Tue Feb 08, 2011 1:53 pm   Post subject: Re: Tic Tac Toe Game Problem

Thank you very much for you lengthy reply. It was VERY helpful.
Here is the game and i think it is finished.

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
var timoe : int := 0
var again : string := "Yes"
var done : boolean := false


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 check
    %Checks veritcal
    if board (0, 0) = board (0, 1) and board (0, 1) = board (0, 2) and full (0, 0) = true and full (0, 1) = true and full (0, 2) = true then
        if turn = false then
            put "Congradulations", name1, "you won!"
        elsif turn = true then
            put "Congradulations", name2, "you won!"
        end if
        done := true

    end if
    if board (1, 0) = board (1, 1) and board (1, 1) = board (1, 2) and full (1, 0) = true and full (1, 1) = true and full (1, 2) = true then
        if turn = false then
            put "Congradulations", name1, "you won!"
        elsif turn = true then
            put "Congradulations", name2, "you won!"
        end if
        done := true
    end if
    if board (2, 0) = board (2, 1) and board (2, 1) = board (2, 2) and full (2, 0) = true and full (2, 1) = true and full (2, 2) = true then
        if turn = false then
            put "Congradulations", name1, "you won!"
        elsif turn = true then
            put "Congradulations", name2, "you won!"
        end if
        done := true
    end if
    %Checks Horizontal
    if board (0, 0) = board (1, 0) and board (1, 0) = board (2, 0) and full (0, 0) = true and full (1, 0) = true and full (2, 0) = true then
        if turn = false then
            put "Congradulations", name1, "you won!"
        elsif turn = true then
            put "Congradulations", name2, "you won!"
        end if
        done := true
    end if
    if board (0, 1) = board (1, 1) and board (1, 1) = board (2, 1) and full (0, 1) = true and full (1, 1) = true and full (2, 1) = true then
        if turn = false then
            put "Congradulations", name1, "you won!"
        elsif turn = true then
            put "Congradulations", name2, "you won!"
        end if
        done := true
    end if

    if board (0, 2) = board (1, 2) and board (1, 2) = board (2, 2) and full (0, 2) = true and full (1, 2) = true and full (2, 2) = true then
        if turn = false then
            put "Congradulations", name1, "you won!"
        elsif turn = true then
            put "Congradulations", name2, "you won!"
        end if
        done := true
    end if

    %checks diagonal
    if board (0, 0) = board (1, 1) and board (1, 1) = board (2, 2) and full (0, 0) = true and full (1, 1) = true and full (2, 2) = true then
        if turn = false then
            put "Congradulations", name1, "you won!"
        elsif turn = true then
            put "Congradulations", name2, "you won!"
        end if
        done := true
    end if

    if board (0, 2) = board (1, 1) and board (1, 1) = board (2, 0) and full (0, 2) = true and full (1, 1) = true and full (2, 0) = true then
        if turn = false then
            put "Congradulations", name1, "you won!"
        elsif turn = true then
            put "Congradulations", name2, "you won!"
        end if
        done := true
    end if

end check

fcn Game_Draw : boolean
    if full (0, 0) = full (0, 1) and full (0, 1) = full (0, 2) and full (0, 2) = full (1, 0) and full (1, 0) = full (1, 1) and full (1, 1) = full (1, 2) and full (1, 2) = full (2, 0) and
            full (2, 0) = full (2, 1) and full (2, 1) = full (2, 2) and full (2, 2) = true then
        put "The Game is a Draw!"
        result true

    else
        result false
    end if
end Game_Draw


fcn Game_Won : boolean
    if board (0, 0) = board (0, 1) and board (0, 1) = board (0, 2) and full (0, 0) = true and full (0, 1) = true and full (0, 2) = true or %%Vertical check
            board (1, 0) = board (1, 1) and board (1, 1) = board (1, 2) and full (1, 0) = true and full (1, 1) = true and full (1, 2) = true or
            board (2, 0) = board (2, 1) and board (2, 1) = board (2, 2) and full (2, 0) = true and full (2, 1) = true and full (2, 2) = true or
            board (0, 0) = board (1, 0) and board (1, 0) = board (2, 0) and full (0, 0) = true and full (1, 0) = true and full (2, 0) = true or %%Horizontal check
            board (0, 1) = board (1, 1) and board (1, 1) = board (2, 1) and full (0, 1) = true and full (1, 1) = true and full (2, 1) = true or
            board (0, 2) = board (1, 2) and board (1, 2) = board (2, 2) and full (0, 2) = true and full (1, 2) = true and full (2, 2) = true or
            board (0, 0) = board (1, 1) and board (1, 1) = board (2, 2) and full (0, 0) = true and full (1, 1) = true and full (2, 2) = true or %%Diagnal check
            board (0, 2) = board (1, 1) and board (1, 1) = board (2, 0) and full (0, 2) = true and full (1, 1) = true and full (2, 0) = true then
        if turn = false then
            put "Congradulations ", name1, " you won!"
        elsif turn = true then
            put "Congradulations ", name2, " you won!"
        end if
        Input.Pause %%to show the players who won
        result true
    end if
    result false
end Game_Won


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

        exit when Game_Won = true or Game_Draw = true

    end loop



end game
loop
    start
    drawgrid
    game
    cls
    if Game_Won = true or Game_Draw = true then
        for a : 0 .. 2
            for b : 0 .. 2
                full (a, b) := false
                board (a, b) := " "
            end for
        end for
    end if
    put "Would you like to play again? Yes or No"
    get again
    exit when again not= "Yes"
end loop

I am now looking into adding an AI.


Thanks Again!
Sponsor
Sponsor
Sponsor
sponsor
TokenHerbz




PostPosted: Tue Feb 08, 2011 3:59 pm   Post subject: RE:Tic Tac Toe Game Problem

No no no!! All wrong!! lol...

Please Re-read my post and try to understand what Iv'e done and try to fix your code to respond the knowledge in that post!

If you don't even try I won't be of assistance anymore!! Read it carefully!
TerranceN




PostPosted: Tue Feb 08, 2011 5:49 pm   Post subject: Re: Tic Tac Toe Game Problem

@TokenHerbz: What did he do wrong? He is no longer using the check procedure (even though it is still in his code), and he can restart his game, the two main points of your post.

Anyway, I would recommend breaking that large if statement into smaller functions such as HorizontalCheck, VerticalCheck, DiagonalCheck, to make that complex boolean statement MUCH easier to read and understand.

The section of code where you handle the mouse clicking on a grid square could be refactored a lot. Instead of having the code to check the turn and whether the grid square is full in each block of code for each grid square, find the coordinates of the grid square clicked on and save them in variables, which you can use to check if the grid square is full in, like this

Turing:

var clickedTileX, clickedTileY : int := -1

% figure out what tiles were clicked and store them in the variables above, then

if (full(clickedTileX, clickedTileY)) then
    full(clickedTileX, clickedTileY) := true
       
    if (turn) then
        drawx(clickedTileX, clickedTileY)
        board(clickedTileX, clickedTileY) := "x"
    else
        drawo(clickedTileX, clickedTileY)
        board(clickedTileX, clickedTileY) := "o"
    end if
    turn := ~turn
end if


Also, the method you use to find the clicked tile could be improved. The way you are currently doing it is what I would call the "Button" method. You treat everything as a button, with defined bounds you check when you click. This will work of course, but it will mean a more complicated way of managing all those buttons, or a lot of typing. Fortunately, since this is a grid with no spaces in between grid squares, we can just use integer division (the 'div' command in Turing) to divide the x-value of some point by the x-size of the grid squares, to find the x index of the tile that contains the point. The same thing can be done with Y values. Here is an example:

Turing:

type GridPoint:
    record
        x : int
        y : int
    end record
%

const gridTilesX := 3
const gridTilesY := 3

var selectedTile : GridPoint
selectedTile.x := -1
selectedTile.y := -1

var gridTileSizeX, gridTileSizeY := 100
var grid : array 0..gridTilesX-1 of array 0..gridTilesY-1 of int
var mouseX, mouseY, mouseButton : int := 0

fcn GetTileContainingPoint(pointX : int, pointY : int) : GridPoint
    var selected : GridPoint

    if ((pointX > 0 and pointX < maxx)
    and (pointY > 0 and pointY < maxy)) then
        selected.x := pointX div gridTileSizeX
        selected.y := pointY div gridTileSizeY
    else
        selected.x := -1
        selected.y := -1
    end if
   
    result selected
end GetTileContainingPoint

var winID : int := Window.Open(
    "graphics:"
    + intstr(gridTilesX * gridTileSizeX)
    + ";"
    + intstr(gridTilesY * gridTileSizeY)
    +",offscreenonly")

loop
    Mouse.Where(mouseX, mouseY, mouseButton)
   
    if (mouseButton > 0) then
        exit
    end if

    selectedTile := GetTileContainingPoint(mouseX, mouseY)

    cls
   
    for i : 0..gridTilesX-1
        for j : 0..gridTilesY-1
            var topLeftX : int := i * gridTileSizeX
            var topLeftY : int := j * gridTileSizeY
           
            if (i = selectedTile.x and j = selectedTile.y) then
                Draw.FillBox(
                    topLeftX,
                    topLeftY,
                    topLeftX + gridTileSizeX,
                    topLeftY + gridTileSizeY,
                    yellow)
            else
                Draw.FillBox(
                    topLeftX,
                    topLeftY,
                    topLeftX + gridTileSizeX,
                    topLeftY + gridTileSizeY,
                    black)
            end if
        end for
    end for
   
    Window.Update(winID)
   
    Time.DelaySinceLast(33)
end loop

Window.Close(winID)


Hope that helps.
compstudent




PostPosted: Tue Feb 08, 2011 6:15 pm   Post subject: Re: Tic Tac Toe Game Problem

Token, I understand that I should recode my game procedure to make it much shorter, faster and simpler. I would if I had the time, but at the moment I am happy that it works. I also copied the wrong version - new one had taken the check out.

Thank you for the reply Terrence - good to know for other projects (although will thankfully get to use Java from now on).

As for making an AI - from what I have seen people have just made long if statements for all the possible situations. Is that the best way to go or is there a simpler way.

Thanks
TokenHerbz




PostPosted: Tue Feb 08, 2011 6:42 pm   Post subject: RE:Tic Tac Toe Game Problem

you could make 2 kinds of AI.

A very basic one that chooses randomly of the left over locations which may or may not "block" your win.

Or you could code a lengthier one, which will be impossible to beat, however you could add chance percent of hitting that spot or not etc.

Lots you could do with that. Smile
compstudent




PostPosted: Tue Feb 08, 2011 11:14 pm   Post subject: Re: Tic Tac Toe Game Problem

Could you please elaborate on how to go about making a lengthier one. How I am currently thinking about it would be to loop through to find any two in a row or column or diagonal and block that.
compstudent




PostPosted: Wed Feb 09, 2011 5:35 pm   Post subject: Re: Tic Tac Toe Game Problem

Hi,

I am close to finished (I think). I have made an AI that just blocks the win. I am now having problems with the one player game play.

Turing:

procedure game1player

    turn := false
    loop

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

                drawx (0, 0)
                turn := true
                full (0, 0) := true
                board (0, 0) := "x"



            elsif z = 1 and x < 100 and y < 200 and y > 100 and full (0, 1) = false then

                drawx (0, 1)
                turn := true
                full (0, 1) := true
                board (0, 1) := "x"

            elsif z = 1 and x < 100 and y < 300 and y > 200 and full (0, 2) = false then

                drawx (0, 2)
                turn := true
                full (0, 2) := true
                board (0, 2) := "x"

            elsif z = 1 and x > 100 and x < 200 and y < 100 then

                drawx (1, 0)
                turn := true
                full (1, 0) := true
                board (1, 0) := "x"


            elsif z = 1 and x > 100 and x < 200 and y > 100 and y < 200 then

                drawx (1, 1)
                turn := true
                full (1, 1) := true
                board (1, 1) := "x"


            elsif z = 1 and x > 100 and x < 200 and y > 200 and y < 300 then

                drawx (1, 2)
                turn := true
                full (1, 2) := true
                board (1, 2) := "x"


            elsif z = 1 and x > 200 and x < 300 and y < 100 then

                drawx (2, 0)
                turn := true
                full (2, 0) := true
                board (2, 0) := "x"


            elsif z = 1 and x > 200 and x < 300 and y > 100 and y < 200 then

                drawx (2, 1)
                turn := true
                full (2, 1) := true
                board (2, 1) := "x"


            elsif z = 1 and x > 200 and x < 300 and y > 200 and y < 300 then

                drawx (2, 2)
                turn := true
                full (2, 2) := true
                board (2, 2) := "x"




            end if
        end if

        if turn = true then
            AI
            drawo (x, y)
            turn := false
        end if

        exit when Game_Won = true or Game_Draw = true

    end loop



end game1player


I am not sure what is wrong, however after x goes, it will not draw an o.
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 2 of 2  [ 29 Posts ]
Goto page Previous  1, 2
Jump to:   


Style:  
Search: