Computer Science Canada

Tie game code not working in tictactoe

Author:  goroyoshi [ Mon Apr 04, 2011 8:45 am ]
Post subject:  Tie game code not working in tictactoe

What is it you are trying to achieve?
<I am trying to make a code that detects if there is a tie game but it keeps going off when only a few boxes are filled instead of all>

Turing:

setscreen ("graphics:120;180,nobuttonbar")

var p1x, p1y, p2x, p2y, win : int := 0
var box : array 1 .. 9 of int
var colour1 : int := brightred
var colour2 : int := brightblue
var xs, ys : string

for i : 1 .. 9
    box (i) := 0
end for

loop
    if box (1) = 1 then
        drawfillbox (1, 1, 32, 32, colour1)
    end if
    if box (2) = 1 then
        drawfillbox (34, 1, 65, 32, colour1)
    end if
    if box (3) = 1 then
        drawfillbox (67, 1, 99, 32, colour1)
    end if
    if box (4) = 1 then
        drawfillbox (1, 34, 32, 65, colour1)
    end if
    if box (5) = 1 then
        drawfillbox (34, 34, 65, 65, colour1)
    end if
    if box (6) = 1 then
        drawfillbox (67, 34, 99, 65, colour1)
    end if
    if box (7) = 1 then
        drawfillbox (1, 67, 32, 99, colour1)
    end if
    if box (8) = 1 then
        drawfillbox (34, 67, 65, 99, colour1)
    end if
    if box (9) = 1 then
        drawfillbox (67, 67, 99, 99, colour1)
    end if

    if box (1) = 2 then
        drawfillbox (1, 1, 32, 32, colour2)
    end if
    if box (2) = 2 then
        drawfillbox (34, 1, 65, 32, colour2)
    end if
    if box (3) = 2 then
        drawfillbox (67, 1, 99, 32, colour2)
    end if
    if box (4) = 2 then
        drawfillbox (1, 34, 32, 65, colour2)
    end if
    if box (5) = 2 then
        drawfillbox (34, 34, 65, 65, colour2)
    end if
    if box (6) = 2 then
        drawfillbox (67, 34, 99, 65, colour2)
    end if
    if box (7) = 2 then
        drawfillbox (1, 67, 32, 99, colour2)
    end if
    if box (8) = 2 then
        drawfillbox (34, 67, 65, 99, colour2)
    end if
    if box (9) = 2 then
        drawfillbox (67, 67, 99, 99, colour2)
    end if
    drawbox (0, 0, 100, 100, black)
    drawline (33, 0, 33, 100, black)
    drawline (66, 0, 66, 100, black)
    drawline (0, 33, 100, 33, black)
    drawline (0, 66, 100, 66, black)

    put "p1x : " ..
    get p1x
    put "p1y : " ..
    get p1y

    if p1x = 1 then
        if p1y = 1 then
            drawfillbox (1, 1, 32, 32, colour1)
            box (1) := 1
        end if
        if p1y = 2 then
            drawfillbox (1, 34, 32, 65, colour1)
            box (4) := 1
        end if
        if p1y = 3 then
            drawfillbox (1, 67, 32, 99, colour1)
            box (7) := 1
        end if
    end if
    if p1x = 2 then
        if p1y = 1 then
            drawfillbox (34, 1, 65, 32, colour1)
            box (2) := 1
        end if
        if p1y = 2 then
            drawfillbox (34, 34, 65, 65, colour1)
            box (5) := 1
        end if
        if p1y = 3 then
            drawfillbox (34, 67, 65, 99, colour1)
            box (8) := 1
        end if
    end if
    if p1x = 3 then
        if p1y = 1 then
            drawfillbox (67, 1, 99, 32, colour1)
            box (3) := 1
        end if
        if p1y = 2 then
            drawfillbox (67, 34, 99, 65, colour1)
            box (6) := 1
        end if
        if p1y = 3 then
            drawfillbox (67, 67, 99, 99, colour1)
            box (9) := 1
        end if
    end if

     if box (1) = 1 or box (1) = 2 and box (2) = 1 or box (2) = 2 and box (3) = 1 or box (3) = 2 and box (4) = 1 or box (4) = 2 and box (5) = 1 or box (5) = 2 and box (6) = 1 or box (6) = 2 and box (7) = 1 or box (7) = 2 and box (8) = 1 or box (8) = 2 and box (9) = 1 or box (9) = 2 then %this part keeps glitching
         win := 3
     end if
   
    if box (1) = 1 and box (2) = 1 and box (3) = 1 or box (4) = 1 and box (5) = 1 and box (6) = 1 or box (7) = 1 and box (8) = 1 and box (9) = 1 or box (1) = 1 and box (4) = 1 and box (7) = 1 or box (2) = 1 and box (5) = 1 and box (8) = 1 or box (3) = 1 and box (6) = 1 and box (9) = 1 or box (1) = 1 and box (5) = 1 and box (9) = 1 or box (3) = 1 and box (5) = 1 and box (7) = 1 then
        win := 1
    end if
    exit when win = 1 or win = 2 or win = 3
   
    put "p2x : " ..
    get p2x
    put "p2y : " ..
    get p2y

    if p2x = 1 then
        if p2y = 1 then
            drawfillbox (1, 1, 32, 32, colour2)
            box (1) := 2
        end if
        if p2y = 2 then
            drawfillbox (1, 34, 32, 65, colour2)
            box (4) := 2
        end if
        if p2y = 3 then
            drawfillbox (1, 67, 32, 99, colour2)
            box (7) := 2
        end if
    end if
    if p2x = 2 then
        if p2y = 1 then
            drawfillbox (34, 1, 65, 32, colour2)
            box (2) := 2
        end if
        if p2y = 2 then
            drawfillbox (34, 34, 65, 65, colour2)
            box (5) := 2
        end if
        if p2y = 3 then
            drawfillbox (34, 67, 65, 99, colour2)
            box (8) := 2
        end if
    end if
    if p2x = 3 then
        if p2y = 1 then
            drawfillbox (67, 1, 99, 32, colour2)
            box (3) := 2
        end if
        if p2y = 2 then
            drawfillbox (67, 34, 99, 65, colour2)
            box (6) := 2
        end if
        if p2y = 3 then
            drawfillbox (67, 67, 99, 99, colour2)
            box (9) := 2
        end if
    end if
   
    if box (1) = 2 and box (2) = 2 and box (3) = 2 or box (4) = 2 and box (5) = 2 and box (6) = 2 or box (7) = 2 and box (8) = 2 and box (9) = 2 or box (1) = 2 and box (4) = 2 and box (7) = 2 or box (2) = 2 and box (5) = 2 and box (8) = 2 or box (3) = 2 and box (6) = 2 and box (9) = 2 or box (1) = 2 and box (5) = 2 and box (9) = 2 or box (3) = 2 and box (5) = 2 and box (7) = 2 then
        win := 2
    end if
    exit when win = 1 or win = 2 or win = 3
   
    cls
end loop
if win = 1 then
    cls
    put "Player ", win , " has won"
end if
if win = 2 then
    cls
    put "Player ", win , " has won"
end if
if win = 3 then
    cls
    put "It's a tie game"
end if



Please specify what version of Turing you are using
<Answer Here>

Author:  apython1992 [ Mon Apr 04, 2011 9:24 am ]
Post subject:  RE:Tie game code not working in tictactoe

code:

if box (1) = 1 or box (1) = 2 and box (2) = 1 or box (2) = 2 and box (3) = 1 or box (3) = 2 and box (4) = 1 or box (4) = 2 and box (5) = 1 or box (5) = 2 and box (6) = 1 or box (6) = 2 and box (7) = 1 or box (7) = 2 and box (8) = 1 or box (8) = 2 and box (9) = 1 or box (9) = 2 then %this part keeps glitching
         win := 3
     end if


I'm assuming this plethora of conditions is supposed to mean "The box is full". I'm not going to go through these conditions to verify that they are correct, because there are much better ways to do it. What is a much simpler way to know that the box is full? Of course, with a little bit of logic you can call it a tie before-hand (maybe this was what you were trying to do here), but even that could be implemented much more effectively.

Author:  Zren [ Mon Apr 04, 2011 4:59 pm ]
Post subject:  Re: Tie game code not working in tictactoe

if boardIsFull() and noWinner() then tieGame()

There is a maximum of 9 turns in a tic tac toe game is there not?

Author:  RandomLetters [ Mon Apr 04, 2011 6:17 pm ]
Post subject:  RE:Tie game code not working in tictactoe

You haven't considered every possibility for tied games. There are at least 3 other arrangements which your code does not consider.

I think you should either spend hours adding every possible tied arrangement, or think of another condition that would indicate the game is a tie, such as what Zren posted.

Author:  goroyoshi [ Mon Apr 04, 2011 6:52 pm ]
Post subject:  RE:Tie game code not working in tictactoe

no randomletters, i got every combination, it checks if every box has one possibility or the other\

back to the question, shouldn't this work because its triggering if i do
turn 1 (1, 2)
turn 2 (2, 2)
turn 3 (3, 2)
tie

Author:  apython1992 [ Mon Apr 04, 2011 6:57 pm ]
Post subject:  RE:Tie game code not working in tictactoe

If it should work, would you be asking about it on the forum right now? Smile

The much better solution would be just to count how many turns have been played without a winner.

Author:  goroyoshi [ Mon Apr 04, 2011 7:00 pm ]
Post subject:  RE:Tie game code not working in tictactoe

i know but still IT SHOULD WORK AND IT'S NOT WORKING!

Author:  Tony [ Mon Apr 04, 2011 7:06 pm ]
Post subject:  Re: RE:Tie game code not working in tictactoe

goroyoshi @ Mon Apr 04, 2011 7:00 pm wrote:
IT SHOULD WORK AND IT'S NOT WORKING!

I don't believe that the Compiler is making any mistakes there. The code is doing exactly what you are asking it to do.

Author:  Zren [ Mon Apr 04, 2011 8:01 pm ]
Post subject:  RE:Tie game code not working in tictactoe

It's because of boolean logic.

if box (1) = 1 or box (1) = 2 and ....

when player 1 choose (1,1), the logic is cut short since it's calculated like...

Turing:

% Not tie game
put false or false and false

% Player 1 choose (1,1)
put true or false and false

% a or b and c

% It will first check the first 2. Since it's an or operation, if a is true, it returns true and ignores the rest.
% If b is true, it checks the next operator beside it, and since it's an and operator, it then checks if c is true as well.
% So in this case, it could be interpreted as:

% a or (b and c)

% When we want:

% (a or b) and c
put (true or false) and false


The whole statement could be fixed with a simple function...
Turing:

var a : array 1 .. 3 of boolean := init (false, false, false)

% Return true when all values are true
fcn isFull : boolean
    for i : 1 .. 3
        if not a (i) then % Short for: if a(i) = false
            result false
        end if
    end for
    result true
end isFull

put isFull
for i : 1 .. 3
    a (i) := true
    put isFull
end for

Author:  goroyoshi [ Tue Apr 05, 2011 4:37 pm ]
Post subject:  RE:Tie game code not working in tictactoe

i fixed it now, realized my retarded mistake


: