Computer Science Canada

Tic Tac Toe

Author:  kalin [ Sun Jan 04, 2004 8:15 pm ]
Post subject:  Tic Tac Toe

Hey, I'm having trouble with the AI for it, could someone please help me?? I've restarted like twenty times and I could use some help... My msn is mynameisamber@hotmail.com feel free to add me!!!! and please help!!

Author:  DanShadow [ Sun Jan 04, 2004 9:24 pm ]
Post subject: 

You could make an unbeatable AI that checks moves ahead. Like a bunch of if values:
code:

if bottom_left_square="x" and middle_square="x" then
Draw.Oval(top_right_corner_x,top_right_corner_y,5,5,2)
elsif bottom_left_square="x" and bottom_middle_square="x" then
Draw.Oval(bottom_right_corner_x,top_right_corner_y,5,5,2)
else
%Other things to check all spaces
end if

This of course isnt completely unbeatable, but you can make it unbeatable if you know a lot of tic tac toe combo moves, lik hitting a bunch of certain ones, so you automatically will get a line if you pick 1 of 2 squares to complete a line. Thats one way you could make AI in tic tac toe.

Author:  Tony [ Sun Jan 04, 2004 10:32 pm ]
Post subject: 

the best AI is to make completely random moves Laughing

otherwise the simpliest AI would be:

DEFEND - block if user has two in a row
ATTACK - move so that AI has 2/3 in a row
RANDOM - if you're not defending or attacking

Author:  AsianSensation [ Sun Jan 04, 2004 11:30 pm ]
Post subject: 

Bugz made his turing 4x4x4 tictactoe unbeatable AI and coded it in c++ and exported to VB so it has the graphics...Im sure if you ask nicely enough, he would give you the code...........................or maybe not.

Author:  kalin [ Mon Jan 05, 2004 7:14 am ]
Post subject: 

thanks! That should help!

Author:  DanShadow [ Mon Jan 05, 2004 9:49 am ]
Post subject: 

Your welcome...
and random moves AI wont win though, lol.

Author:  icedesi [ Mon Jan 05, 2004 7:42 pm ]
Post subject: 

That is a good idea eh, never thought of that! 8) Rolling Eyes

Author:  kalin [ Wed Jan 21, 2004 5:48 pm ]
Post subject: 

Well, either way, I gotta start from scratch!! Boohoo. Oh well. I'll suck it up. Anyone got any tips on making it so that a function says that places are taken?? If not its ok!

Author:  recneps [ Wed Jan 21, 2004 6:58 pm ]
Post subject: 

look at my tic tac toe in source code... its not AI, but it shows how i made it so you can only click on sq's with nothing in em

Author:  kalin [ Wed Jan 21, 2004 7:02 pm ]
Post subject: 

the one that has the tic tac toe and a couple other games in it?? AKA the zipped up one???

Author:  Tony [ Wed Jan 21, 2004 7:04 pm ]
Post subject: 

just search for "tic tac toe" and "recneps" as the post author in [turing source code] or [turing submissions]... depending on where it was posted and I'm sure you'll find it

Author:  kalin [ Wed Jan 21, 2004 7:59 pm ]
Post subject: 

Well, I kinda sorted out a problem I had, now I have a bigger problem . Can anyone help??

code:
var takenx : array 1 .. 9 of boolean
var takeny : array 1 .. 9 of int
var boxx, boxy : array 1 .. 9 of int
var turn, who_won_game : string := "nobody"
var x, y, b : int := 0
var answer : string
var u, c : array 1 .. 5 of int

%process playstuff
%   loop
%      Music.PlayFile ("Zelda 2 - Majora's Mask - Deku Palace.mp3")
% end loop

%end playstuff

%fork playstuff

procedure Initialize (var c, u : array 1 .. 5 of int)
    for initial : 1 .. 5
        u (initial) := -999
        c (initial) := -999
    end for
end Initialize

for i : 1 .. 9
    takenx (i) := false
    takeny (i) := 0
    turn := "player 1"
end for

boxx (1) := 60
boxx (2) := 160
boxx (3) := 260
boxy (1) := 60
boxy (2) := 60
boxy (3) := 60
boxx (4) := 60
boxx (5) := 160
boxx (6) := 260
boxy (4) := 160
boxy (5) := 160
boxy (6) := 160
boxx (7) := 60
boxx (8) := 160
boxx (9) := 260
boxy (7) := 260
boxy (8) := 260
boxy (9) := 260

procedure Draw_Screen
    for i : 1 .. 9
        Draw.Box (boxx (i) - 50, boxy (i) - 50, boxx (i) + 50, boxy (i) + 50, 255)
    end for
end Draw_Screen

function Get_Users_Move:int
    for i : 1 .. 9
        if (turn = "player 1") and (x > boxx (i) - 50 and x < boxx (i) + 50) and (y > boxy (i) - 50 and y < boxy (i) + 50) and (b = 1) then
            takenx (i) := true
            takeny (i) := 1
            turn := "computer"
        end if
    end for
end Get_Users_Move

procedure Plot_Move
    for i : 1 .. 9
        if (takenx (i) = true) and (takeny (i) = 1) then
            Draw.Line (boxx (i) - 50, boxy (i) + 50, boxx (i) + 50, boxy (i) - 50, 12)
            Draw.Line (boxx (i) - 50, boxy (i) - 50, boxx (i) + 50, boxy (i) + 50, 12)
        elsif (takenx (i) = true) and (takeny (i) = 2) then
            Draw.Oval (boxx (i), boxy (i), 45, 45, 2)
        end if
    end for
end Plot_Move
%!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
function Block_User :int
    if (takenx (2) = true and takenx (3) = true) and (takeny (2) = 1 and takeny (3) = 1) then
        Draw.Oval (boxx (1), boxy (1), 45, 45, 2)
    elsif (takenx (1) = true and takenx (3) = true) and (takeny (1) = 1 and takeny (3) = 1) then
        Draw.Oval (boxx (4), boxy (2), 45, 45, 2)
    elsif (takenx (1) = true and takenx (2) = true) and (takeny (1) = 1 and takeny (2) = 1) then
        Draw.Oval (boxx (7), boxy (3), 45, 45, 2)
    elsif (takenx (5) = true and takenx (6) = true) and (takeny (5) = 1 and takeny (6) = 1) then
        Draw.Oval (boxx (2), boxy (4), 45, 45, 2)
    elsif (takenx (4) = true and takenx (6) = true) and (takeny (4) = 1 and takeny (6) = 1) then
        Draw.Oval (boxx (5), boxy (5), 45, 45, 2)
    elsif (takenx (4) = true and takenx (5) = true) and (takeny (4) = 1 and takeny (5) = 1) then
        Draw.Oval (boxx (8), boxy (6), 45, 45, 2)
    elsif (takenx (8) = true and takenx (9) = true) and (takeny (8) = 1 and takeny (9) = 1) then
        Draw.Oval (boxx (3), boxy (7), 45, 45, 2)
    elsif (takenx (7) = true and takenx (9) = true) and (takeny (7) = 1 and takeny (9) = 1) then
        Draw.Oval (boxx (6), boxy (8), 45, 45, 2)
    elsif (takenx (7) = true and takenx (8) = true) and (takeny (7) = 1 and takeny (8) = 1) then
        Draw.Oval (boxx (9), boxy (9), 45, 45, 2)
    elsif (takenx (5) = true and takenx (9) = true) and (takeny (5) = 1 and takeny (9) = 1) then
        Draw.Oval (boxx (1), boxy (1), 45, 45, 2)
    elsif (takenx (1) = true and takenx (9) = true) and (takeny (1) = 1 and takeny (9) = 1) then
        Draw.Oval (boxx (5), boxy (5), 45, 45, 2)
    elsif (takenx (1) = true and takenx (5) = true) and (takeny (1) = 1 and takeny (5) = 1) then
        Draw.Oval (boxx (9), boxy (9), 45, 45, 2)
    elsif (takenx (5) = true and takenx (3) = true) and (takeny (5) = 1 and takeny (3) = 1) then
        Draw.Oval (boxx (3), boxy (7), 45, 45, 2)
    elsif (takenx (7) = true and takenx (3) = true) and (takeny (7) = 1 and takeny (3) = 1) then
        Draw.Oval (boxx (5), boxy (5), 45, 45, 2)
    elsif (takenx (7) = true and takenx (5) = true) and (takeny (7) = 1 and takeny (5) = 1) then
        Draw.Oval (boxx (7), boxy (3), 45, 45, 2)
    elsif (takenx (4) = true and takenx (7) = true) and (takeny (4) = 1 and takeny (7) = 1) then
        Draw.Oval (boxx (1), boxy (1), 45, 45, 2)
    elsif (takenx (1) = true and takenx (7) = true) and (takeny (1) = 1 and takeny (7) = 1) then
        Draw.Oval (boxx (2), boxy (4), 45, 45, 2)
    elsif (takenx (1) = true and takenx (4) = true) and (takeny (1) = 1 and takeny (4) = 1) then
        Draw.Oval (boxx (3), boxy (7), 45, 45, 2)
    elsif (takenx (5) = true and takenx (8) = true) and (takeny (5) = 1 and takeny (8) = 1) then
        Draw.Oval (boxx (2), boxy (4), 45, 45, 2)
    elsif (takenx (2) = true and takenx (8) = true) and (takeny (2) = 1 and takeny (8) = 1) then
        Draw.Oval (boxx (5), boxy (5), 45, 45, 2)
    elsif (takenx (2) = true and takenx (5) = true) and (takeny (2) = 1 and takeny (5) = 1) then
        Draw.Oval (boxx (6), boxy (8), 45, 45, 2)
    elsif (takenx (6) = true and takenx (9) = true) and (takeny (6) = 1 and takeny (9) = 1) then
        Draw.Oval (boxx (7), boxy (3), 45, 45, 2)
    elsif (takenx (3) = true and takenx (9) = true) and (takeny (3) = 1 and takeny (9) = 1) then
        Draw.Oval (boxx (8), boxy (6), 45, 45, 2)
    elsif (takenx (3) = true and takenx (6) = true) and (takeny (3) = 1 and takeny (6) = 1) then
        Draw.Oval (boxx (9), boxy (9), 45, 45, 2)
    end if
end Block_User

%!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

function Get_Computers_Move :int
    for i : 1 .. 5
        if u (i) not= -999 and u (i) = 5 then
            Draw.Oval (boxx (i), boxy (i), 45, 45, 2)
            turn := "player 1"
        end if
    end for
end Get_Computers_Move

function User_Won : boolean
    if (takenx (1) = true and takenx (2) = true and takenx (3) = true) and (takeny (1) = 1 and takeny (2) = 1 and takeny (3) = 1)
            or (takenx (4) = true and takenx (5) = true and takenx (6) = true) and (takeny (4) = 1 and takeny (5) = 1 and takeny (6) = 1)
            or (takenx (7) = true and takenx (8) = true and takenx (9) = true) and (takeny (7) = 1 and takeny (8) = 1 and takeny (9) = 1)
            or (takenx (1) = true and takenx (5) = true and takenx (9) = true) and (takeny (1) = 1 and takeny (5) = 1 and takeny (9) = 1)
            or (takenx (7) = true and takenx (5) = true and takenx (3) = true) and (takeny (7) = 1 and takeny (5) = 1 and takeny (3) = 1)
            or (takenx (1) = true and takenx (4) = true and takenx (7) = true) and (takeny (1) = 1 and takeny (4) = 1 and takeny (7) = 1)
            or (takenx (2) = true and takenx (5) = true and takenx (8) = true) and (takeny (2) = 1 and takeny (5) = 1 and takeny (8) = 1)
            or (takenx (3) = true and takenx (6) = true and takenx (9) = true) and (takeny (3) = 1 and takeny (6) = 1 and takeny (9) = 1) then
        who_won_game := "player 1"
    end if
end User_Won

function Computer_Won : boolean
    if (takenx (1) = true and takenx (2) = true and takenx (3) = true) and (takeny (1) = 2 and takeny (2) = 2 and takeny (3) = 2)
            or (takenx (4) = true and takenx (5) = true and takenx (6) = true) and (takeny (4) = 2 and takeny (5) = 2 and takeny (6) = 2)
            or (takenx (7) = true and takenx (8) = true and takenx (9) = true) and (takeny (7) = 2 and takeny (8) = 2 and takeny (9) = 2)
            or (takenx (1) = true and takenx (5) = true and takenx (9) = true) and (takeny (1) = 2 and takeny (5) = 2 and takeny (9) = 2)
            or (takenx (7) = true and takenx (5) = true and takenx (3) = true) and (takeny (7) = 2 and takeny (5) = 2 and takeny (3) = 2)
            or (takenx (1) = true and takenx (4) = true and takenx (7) = true) and (takeny (1) = 2 and takeny (4) = 2 and takeny (7) = 2)
            or (takenx (2) = true and takenx (5) = true and takenx (8) = true) and (takeny (2) = 2 and takeny (5) = 2 and takeny (8) = 2)
            or (takenx (3) = true and takenx (6) = true and takenx (9) = true) and (takeny (3) = 2 and takeny (6) = 2 and takeny (9) = 2) then
        who_won_game := "computer"
    end if
end Computer_Won

%-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
%-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=


loop
    setscreen ("offscreenonly")
    mousewhere (x, y, b)
    Draw_Screen
    Initialize (c, u)
    Plot_Move
    exit when who_won_game not= "nobody"
end loop
locate (3, 1)
if who_won_game = "player 1" then
    put "You Won!!!"
elsif who_won_game = "computer" then
    put "The computer won... Oh well. Try again next time"
end if


See the thing is, a) it wont load for me until I go to close it. And b) it refuses to have the 'x' show up. Can anyone help??

If this same problem has been posted, sorry,m I looked, but mustve missed, if its been asked. If it has, give me the link and I'll go there. Thx!

Author:  Cervantes [ Thu Jan 22, 2004 7:03 pm ]
Post subject: 

you forgot to put in View.Update

Author:  kalin [ Thu Jan 22, 2004 7:09 pm ]
Post subject: 

I know, I can't use that at school....older version bah. Hate that

Author:  Cervantes [ Thu Jan 22, 2004 7:33 pm ]
Post subject: 

then petition your teacher to let you use 4.0.5 Very Happy

and if you can't use View.Update, take out the View.Set ("offscreenonly")

that oughta do it

Author:  kalin [ Fri Jan 23, 2004 6:13 am ]
Post subject: 

ok, thx!

Author:  McKenzie [ Fri Jan 23, 2004 8:55 am ]
Post subject: 

have your teacher follow shorthair's link. I bet he/she can probably legally upgrade but may not realize. (Warning - some teachers don't want to install the new version because they have to update their knowledge Sad

Author:  DanShadow [ Fri Jan 23, 2004 9:56 am ]
Post subject: 

lol...thats true. Stupid lazy compsci teachers. (although there are some good ones....like mine isnt half bad...except for the fact th at im as smart as him pretty much now. Very Happy In only 1 semester...I learned double what he tought, an d by the end of the year at ISU time, everyone cam to me for advice and assistance, muahaha Wink )

Author:  kalin [ Fri Jan 23, 2004 3:22 pm ]
Post subject: 

too late now, the semester is over and its looking like I'm just going to scrape by........oh well.....


: