tic tac toe AI
Author 
Message 
alnamy

Posted: Sun Jun 06, 2004 5:16 pm Post subject: tic tac toe AI 


hey i am currently taking grade 10 science and i am having a lot of difficulties making a 1 person tic tac toe game (against computer). I have completed a 1v1 (two players) tic tac toe game . but i am stuck on where to start a 1 person tic tac toe game. Could someone kindly show me the programming logic or if possible, show me the source code? Thank you very much 





Sponsor Sponsor



Tony

Posted: Sun Jun 06, 2004 7:02 pm Post subject: (No subject) 


basics of AI
doesn't cover tictactoe really, but might be of use anyways...
as for AI... there're 3 possible moves
defend  block player's 2 in a row to prevent lose
attack  if no need for defend, form 2 in a row of own
random  if no place to attack ether, just place your sign randomly

Tony's programming blog. DWITE  a programming contest. 




alnamy

Posted: Sun Jun 06, 2004 8:29 pm Post subject: please help 


Here's what I have now. I understand the logic now but i am not sure how to modify it for the comp
% By BTSP
% The Tic Tac Toe Game
%Sets the screen size
setscreen ("graphics:400;400")
%Makes the background
drawfillbox (0, 0, maxx, maxy, black)
%Draws the large box
drawbox (5, 5, maxx  5, maxy  5, yellow)
%Draws the smaller boxes
drawline (maxx div 3, maxy  5, maxx div 3, 5, yellow)
drawline (maxx div 1.5, maxy  5, maxx div 1.5, 5, yellow)
drawline (maxx  5, maxy div 3, 5, maxy div 3, yellow)
drawline (maxx  5, maxy div 1.5, 5, maxy div 1.5, yellow)
var x, y, button : int
var squares : array 1 .. 9 of int
var playerturn : int := 1
var deemah : int :=0
for a : 1 .. 9
squares (a) := a+2
end for
var players : array 1 .. 2 of string
players (1) := "Judy"
players (2) := "Alice"
loop
mousewhere (x, y, button)
if x >= 5 and x <= maxx div 3 and y >= maxy div 1.5 and y <= maxy  5 and button = 1 and squares (1) = 3 then
% First square
drawfilloval (maxx div 6, maxy div 1.2, 30, 30, yellow)
drawfilloval (maxx div 6, maxy div 1.2, 20, 20, playerturn)
if playerturn = 1 then
squares (1) := 1
playerturn := 2
elsif playerturn = 2 then
squares (1) := 2
playerturn := 1
end if
elsif x > maxx div 3 and x <= maxx div 1.5 and y >= maxy div 1.5 and y <= maxy  5 and button = 1 and squares (2) = 4 then
% Second Square
drawfilloval (maxx div 2, maxy div 1.2, 30, 30, yellow)
drawfilloval (maxx div 2, maxy div 1.2, 20, 20, playerturn)
if playerturn = 1 then
squares (2) := 1
playerturn := 2
elsif playerturn = 2 then
squares (2) := 2
playerturn := 1
end if
elsif x > maxx div 1.5 and x <= maxx  5 and y >= maxy div 1.5 and y <= maxy  5 and button = 1 and squares (3) = 5 then
% Third Square
drawfilloval (maxx div 1.2, maxy div 1.2, 30, 30, yellow)
drawfilloval (maxx div 1.2, maxy div 1.2, 20, 20, playerturn)
if playerturn = 1 then
squares (3) := 1
playerturn := 2
elsif playerturn = 2 then
squares (3) := 2
playerturn := 1
end if
elsif x >= 5 and x <= maxx div 3 and y >= maxy div 3 and y <= maxy div 1.5 and button = 1 and squares (4) = 6 then
% Fourth Square
drawfilloval (maxx div 6, maxy div 2, 30, 30, yellow)
drawfilloval (maxx div 6, maxy div 2, 20, 20, playerturn)
if playerturn = 1 then
squares (4) := 1
playerturn := 2
elsif playerturn = 2 then
squares (4) := 2
playerturn := 1
end if
elsif x > maxx div 3 and x <= maxx div 1.5 and y >= maxy div 3 and y <= maxy div 1.5 and button = 1 and squares (5) = 7 then
% Fifth Square
drawfilloval (maxx div 2, maxy div 2, 30, 30, yellow)
drawfilloval (maxx div 2, maxy div 2, 20, 20, playerturn)
if playerturn = 1 then
squares (5) := 1
playerturn := 2
elsif playerturn = 2 then
squares (5) := 2
playerturn := 1
end if
elsif x > maxx div 1.5 and x <= maxx  5 and y >= maxy div 3 and y <= maxy div 1.5 and button = 1 and squares (6) = 8 then
% Sixth Square
drawfilloval (maxx div 1.2, maxy div 2, 30, 30, yellow)
drawfilloval (maxx div 1.2, maxy div 2, 20, 20, playerturn)
if playerturn = 1 then
squares (6) := 1
playerturn := 2
elsif playerturn = 2 then
squares (6) := 2
playerturn := 1
end if
elsif x >= 5 and x <= maxx div 3 and y > 5 and y <= maxy div 3 and button = 1 and squares (7) = 9 then
% Seventh Square
drawfilloval (maxx div 6, maxy div 6, 30, 30, yellow)
drawfilloval (maxx div 6, maxy div 6, 20, 20, playerturn)
if playerturn = 1 then
squares (7) := 1
playerturn := 2
elsif playerturn = 2 then
squares (7) := 2
playerturn := 1
end if
elsif x > maxx div 3 and x <= maxx div 1.5 and y > 5 and y <= maxy div 3 and button = 1 and squares (8) = 10 then
% Eighth Square
drawfilloval (maxx div 2, maxy div 6, 30, 30, yellow)
drawfilloval (maxx div 2, maxy div 6, 20, 20, playerturn)
if playerturn = 1 then
squares (8) := 1
playerturn := 2
elsif playerturn = 2 then
squares (8) := 2
playerturn := 1
end if
elsif x > maxx div 1.5 and x <= maxx  5 and y > 5 and y <= maxy div 3 and button = 1 and squares (9) = 11 then
% Ninth Square
drawfilloval (maxx div 1.2, maxy div 6, 30, 30, yellow)
drawfilloval (maxx div 1.2, maxy div 6, 20, 20, playerturn)
if playerturn = 1 then
squares (9) := 1
playerturn := 2
elsif playerturn = 2 then
squares (9) := 2
playerturn := 1
end if
end if
if squares (1) = squares (2) and squares (2) = squares (3) then
deemah := squares (1)
exit
elsif squares (1) = squares (4) and squares (4) = squares (7) then
deemah := squares (1)
exit
elsif squares (3) = squares (6) and squares (6) = squares (9) then
deemah := squares (3)
exit
elsif squares (7) = squares (8) and squares (8) = squares (9) then
deemah := squares (7)
exit
elsif squares (2) = squares (5) and squares (5) = squares (8) then
deemah := squares (2)
exit
elsif squares (4) = squares (5) and squares (5) = squares (6) then
deemah := squares (4)
exit
elsif squares (1) = squares (5) and squares (5) = squares (9) then
deemah := squares (1)
exit
elsif squares (3) = squares (5) and squares (5) = squares (7) then
deemah := squares (3)
exit
end if
delay (50)
exit when squares (1) not= 3 and squares (2) not= 4 and squares (3) not= 5 and squares (4) not= 6 and squares (5) not= 7 and squares (6) not= 8 and squares (7) not= 9 and squares (8) not= 10 and
squares (9) not= 11
end loop 






