TFish

|
Posted: Sun Jan 24, 2010 9:26 pm Post subject: Tic Tac Toe Perfect AI |
|
|
A short Tic Tac Toe code with perfect AI that myself and a partner made. I had stripped it down so it doesn't use any pictures or fonts, though. If you do happen to beat it, or if you find a glitch, post it here
Turing: | var box : array 1 .. 3, 1 .. 3 of int
var x, y, click, ax, ay, random, win, counter, col1, col2 : int := 0
var turn : boolean := true
var done, cats : boolean := false
setscreen ("graphics:500;500,position:center;center,title:Tic Tac Toe,offscreenonly,nobuttonbar")
procedure initBoard ()
for i : 1 .. 3
for j : 1 .. 3
box (i, j ) := 0
end for
end for
randint (random, 1, 2)
if random = 1 then
turn := true
elsif random = 2 then
turn := false
end if
counter := 0
cats := false
done := false
end initBoard
procedure gameOver
for decreasing i : 2 .. 1
for j : 1 .. 3
if box (3, j ) = i and box (2, j ) = i and box (1, j ) = i then
done := true
exit
elsif box (j, 3) = i and box (j, 2) = i and box (j, 1) = i then
done := true
exit
elsif box (3, 3) = i and box (2, 2) = i and box (1, 1) = i then
done := true
exit
elsif box (1, 3) = i and box (2, 2) = i and box (3, 1) = i then
done := true
exit
end if
end for
end for
end gameOver
procedure blockWin
for decreasing i : 2 .. 1
for row : 1 .. 3
for column : 1 .. 3
if done = false and turn = false and box (3, column ) = i and box (2, column ) = i and box (1, column ) = 0 then
box (1, column ) := 2
turn := true
counter + = 1
elsif done = false and turn = false and box (3, column ) = i and box (1, column ) = i and box (2, column ) = 0 then
box (2, column ) := 2
turn := true
counter + = 1
elsif done = false and turn = false and box (1, column ) = i and box (2, column ) = i and box (3, column ) = 0 then
box (3, column ) := 2
turn := true
counter + = 1
elsif done = false and turn = false and box (row, 3) = i and box (row, 2) = i and box (row, 1) = 0 then
box (row, 1) := 2
turn := true
counter + = 1
elsif done = false and turn = false and box (row, 3) = i and box (row, 1) = i and box (row, 2) = 0 then
box (row, 2) := 2
turn := true
counter + = 1
elsif done = false and turn = false and box (row, 1) = i and box (row, 2) = i and box (row, 3) = 0 then
box (row, 3) := 2
turn := true
counter + = 1
elsif done = false and turn = false and box (3, 3) = i and box (2, 2) = i and box (1, 1) = 0 then
box (1, 1) := 2
turn := true
counter + = 1
elsif done = false and turn = false and box (3, 3) = i and box (1, 1) = i and box (2, 2) = 0 then
box (2, 2) := 2
turn := true
counter + = 1
elsif done = false and turn = false and box (1, 1) = i and box (2, 2) = i and box (3, 3) = 0 then
box (3, 3) := 2
turn := true
counter + = 1
elsif done = false and turn = false and box (1, 3) = i and box (2, 2) = i and box (3, 1) = 0 then
box (3, 1) := 2
turn := true
counter + = 1
elsif done = false and turn = false and box (1, 3) = i and box (3, 1) = i and box (2, 2) = 0 then
box (2, 2) := 2
turn := true
counter + = 1
elsif done = false and turn = false and box (3, 1) = i and box (2, 2) = i and box (1, 3) = 0 then
box (1, 3) := 2
turn := true
counter + = 1
end if
end for
end for
end for
end blockWin
procedure aiStrategy
if done = false and counter = 3 and turn = false and ((box (2, 3) = 1 and box (2, 1) = 1) or (box (3, 3) = 1 and box (1, 1) = 1) or (box (1, 3) = 1 and box (3, 1) = 1)) and box (1, 2) = 0 then
box (1, 2) := 2
counter + = 1
turn := true
elsif done = false and counter = 3 and turn = false and ((box (2, 2) = 1 and box (3, 3) = 1) or (box (3, 3) = 1 and box (1, 2) = 1) or (box (1, 1) = 1 and box (2, 3) = 1) or (box (2, 3) = 1 and
box (1, 2) = 1)) and box (1, 3) = 0 then
box (1, 3) := 2
counter + = 1
turn := true
elsif done = false and counter = 3 and turn = false and ((box (1, 3) = 1 and box (2, 1) = 1) or (box (3, 1) = 1 and box (1, 2) = 1) or (box (2, 1) = 1 and box (1, 2) = 1)) and box (1, 1) = 0 then
box (1, 1) := 2
counter + = 1
turn := true
elsif done = false and counter = 3 and turn = false and ((box (1, 3) = 1 and box (3, 2) = 1) or (box (3, 1) = 1 and box (2, 3) = 1) or (box (3, 1) = 1 and box (2, 3) = 1) or (box (3, 2) = 1 and
box (2, 3) = 1)) and box (3, 3) = 0 then
box (3, 3) := 2
counter + = 1
turn := true
elsif done = false and counter = 3 and turn = false and ((box (1, 1) = 1 and box (2, 2) = 1) or (box (1, 1) = 1 and box (3, 2) = 1) or (box (2, 1) = 1 and box (3, 3) = 1) or (box (2, 1) = 1 and
box (3, 2) = 1)) and box (3, 1) = 0 then
box (3, 1) := 2
counter + = 1
turn := true
elsif done = false and counter = 3 and turn = false and box (1, 2) = 1 and box (3, 2) = 1 and box (2, 1) = 0 then
box (2, 1) := 2
turn := true
counter + = 1
elsif done = false and counter = 2 and turn = false and box (2, 3) = 1 and box (2, 2) = 0 then
box (2, 2) := 2
turn := true
counter + = 1
elsif done = false and counter = 2 and turn = false and (box (3, 2) = 1 or box (2, 3) = 1 or box (2, 1) = 1 or box (1, 2) = 1) and box (3, 3) = 0 then
box (3, 3) := 2
turn := true
counter + = 1
elsif done = false and counter = 2 and turn = false and (box (1, 3) = 1 or box (3, 1) = 1) and box (3, 3) = 0 then
box (3, 3) := 2
turn := true
counter + = 1
elsif done = false and counter = 2 and turn = false and (box (1, 1) = 1 or box (3, 3) = 1) and box (1, 3) = 0 then
box (1, 3) := 2
turn := true
counter + = 1
elsif done = false and counter = 1 and turn = false and box (2, 2) = 0 then
box (2, 2) := 2
turn := true
counter + = 1
elsif done = false and counter = 1 and turn = false and box (2, 2) = 1 and box (3, 3) = 0 then
box (3, 3) := 2
turn := true
counter + = 1
elsif done = false and counter = 0 and turn = false then
box (2, 2) := 2
counter + = 1
turn := true
end if
end aiStrategy
procedure drawBox
for row : 1 .. 3
for column : 1 .. 3
if box (row, column ) = 1 then
drawfillbox (row * 100, column * 100, row * 100 + 100, column * 100 + 100, red)
elsif box (row, column ) = 2 then
drawfillbox (row * 100, column * 100, row * 100 + 100, column * 100 + 100, blue)
end if
drawbox (row * 100, column * 100, row * 100 + 100, column * 100 + 100, 255)
end for
end for
end drawBox
initBoard
loop
mousewhere (x, y, click )
for row : 1 .. 3
for column : 1 .. 3
if done = false and box (row, column ) = 0 and x > row * 100 and y > column * 100 and x < row * 100 + 100 and y < column * 100 + 100 and click = 1 then
box (row, column ) := 1
turn := false
counter + = 1
end if
drawBox
gameOver
blockWin
aiStrategy
if done = false and turn = false then
randint (ax, 1, 3)
randint (ay, 1, 3)
if turn = false and (box (ax, ay ) = 1 or box (ax, ay ) = 2) then
randint (ax, 1, 3)
randint (ay, 1, 3)
elsif turn = false and box (ax, ay ) = 0 then
box (ax, ay ) := 2
turn := true
counter + = 1
end if
end if
drawBox
gameOver
drawbox (100, 100, 400, 400, 0)
end for
end for
if counter = 9 and done = false then
cats := true
end if
View.Update
exit when done = true or cats = true
end loop
|
Enjoy |
|
|