Tic Tac Toe Using Turing
Author |
Message |
Charan_Batth
|
Posted: Thu Jan 15, 2015 4:00 pm Post subject: Tic Tac Toe Using Turing |
|
|
Turing: |
View.Set ("graphics:500;600")
var x, y, b : int % X and Y coordinates, button variables
var winsX, winsO, winNo : int := 0 % Counter for wins, losses, and ties
var font, fontWins : int
var turn : boolean := false % True for X, false for O
var picX, picO : int % Picture variable for the X and O
var randCell : int % Random cell variable for AI
var steve : boolean := true % Set Steve to "False" to play 2 player game
var turns : int := 1 % Variable counting who's turn it is
var gameOn, exitVar : boolean := false % Flags for exiting the whole game loop, or single game instance
var keyCheck : array char of boolean % Array controlling keyboard input
var AICheck : array 1 .. 8 of boolean :=
init (false, false, false, false, false, false, false, false) % Row/Column/Diagonal check array
var posX, posO : array 1 .. 8 of int :=
init (0, 0, 0, 0, 0, 0, 0, 0) % Row/Column/Diagonal points counter (O and X)
var square : array 1 .. 9 of boolean :=
init (false, false, false, false, false, false, false, false, false)% Row/Column/Diagonal cell check
font := Font.New ("Times New Roman:14:bold")
fontWins := Font.New ("Times New Roman:12")
/* Draws X picture then saves it in a variable */
Draw.ThickLine (5, 5, 95, 95, 3, 16)
Draw.ThickLine (5, 95, 95, 5, 3, 16)
picX := Pic.New (0, 0, 100, 100)
cls
/* Draws O picture then saves it in a variable */
Draw.FillOval (50, 50, 45, 45, 16)
Draw.FillOval (50, 50, 42, 42, 0)
picO := Pic.New (0, 0, 100, 100)
cls
/* Sets all required game variables to default values */
procedure Restart
for i : 1 .. 9
square (i ) := false
end for
for i : 1 .. 8
AICheck (i ) := false
posX (i ) := 0
posO (i ) := 0
end for
if turns mod 2 = 0 then
turn := true
else
turn := false
end if
end Restart
/* Artificial Intelligence, Steve. Used for Single-Player games */
procedure AI
if turn = false then
if square (1) = true and square (2) = true and square (3) = true then
AICheck (1) := true
end if
if square (4) = true and square (5) = true and square (6) = true then
AICheck (2) := true
end if
if square (7) = true and square (8) = true and square (9) = true then
AICheck (3) := true
end if
if square (1) = true and square (4) = true and square (7) = true then
AICheck (4) := true
end if
if square (2) = true and square (5) = true and square (8) = true then
AICheck (5) := true
end if
if square (3) = true and square (6) = true and square (9) = true then
AICheck (6) := true
end if
if square (1) = true and square (5) = true and square (9) = true then
AICheck (7) := true
end if
if square (7) = true and square (5) = true and square (3) = true then
AICheck (8) := true
end if
loop
if AICheck (1) = false then
if posO (1) = 7 then
Pic.Draw (picO, 300, 100, picMerge)
posO (1) + = 8
posO (6) + = 8
posO (8) + = 8
turn := true
square (3) := true
exit
elsif posO (1) = 9 then
Pic.Draw (picO, 100, 100, picMerge)
posO (1) + = 6
posO (4) + = 6
posO (7) + = 6
turn := true
square (1) := true
exit
elsif posO (1) = 14 then
Pic.Draw (picO, 200, 100, picMerge)
posO (1) + = 1
posO (5) + = 1
turn := true
square (2) := true
exit
end if
end if
if AICheck (2) = false then
if posO (2) = 10 then
Pic.Draw (picO, 200, 200, picMerge)
posO (2) + = 5
posO (5) + = 5
posO (7) + = 5
posO (8) + = 5
turn := true
square (5) := true
exit
elsif posO (2) = 12 then
Pic.Draw (picO, 300, 200, picMerge)
posO (2) + = 3
posO (6) + = 3
turn := true
square (6) := true
exit
elsif posO (2) = 8 then
Pic.Draw (picO, 100, 200, picMerge)
posO (2) + = 7
posO (4) + = 7
turn := true
square (4) := true
exit
end if
end if
if AICheck (3) = false then
if posO (3) = 13 then
Pic.Draw (picO, 100, 300, picMerge)
posO (3) + = 2
posO (4) + = 2
posO (8) + = 2
turn := true
square (7) := true
exit
elsif posO (3) = 6 then
Pic.Draw (picO, 200, 300, picMerge)
posO (3) + = 9
posO (5) + = 9
turn := true
square (8) := true
exit
elsif posO (3) = 11 then
Pic.Draw (picO, 300, 300, picMerge)
posO (3) + = 4
posO (6) + = 4
posO (7) + = 4
turn := true
square (9) := true
exit
end if
end if
if AICheck (4) = false then
if posO (4) = 9 then
Pic.Draw (picO, 100, 100, picMerge)
posO (1) + = 6
posO (4) + = 6
posO (7) + = 6
turn := true
square (1) := true
exit
elsif posO (4) = 8 then
Pic.Draw (picO, 100, 200, picMerge)
posO (2) + = 7
posO (4) + = 7
turn := true
square (4) := true
exit
elsif posO (4) = 13 then
Pic.Draw (picO, 100, 300, picMerge)
posO (3) + = 2
posO (4) + = 2
posO (8) + = 2
turn := true
square (7) := true
exit
end if
end if
if AICheck (5) = false then
if posO (5) = 14 then
Pic.Draw (picO, 200, 100, picMerge)
posO (1) + = 1
posO (5) + = 1
turn := true
square (2) := true
exit
elsif posO (5) = 10 then
Pic.Draw (picO, 200, 200, picMerge)
posO (2) + = 5
posO (5) + = 5
posO (7) + = 5
posO (8) + = 5
turn := true
square (5) := true
exit
elsif posO (5) = 6 then
Pic.Draw (picO, 200, 300, picMerge)
posO (3) + = 9
posO (5) + = 9
turn := true
square (8) := true
exit
end if
end if
if AICheck (6) = false then
if posO (6) = 7 then
Pic.Draw (picO, 300, 100, picMerge)
posO (1) + = 8
posO (6) + = 8
posO (8) + = 8
turn := true
square (3) := true
exit
elsif posO (6) = 12 then
Pic.Draw (picO, 300, 200, picMerge)
posO (2) + = 3
posO (6) + = 3
turn := true
square (6) := true
exit
elsif posO (6) = 11 then
Pic.Draw (picO, 300, 300, picMerge)
posO (3) + = 4
posO (6) + = 4
posO (7) + = 4
turn := true
square (9) := true
exit
end if
end if
if AICheck (7) = false then
if posO (7) = 9 then
Pic.Draw (picO, 100, 100, picMerge)
posO (1) + = 6
posO (4) + = 6
posO (7) + = 6
turn := true
square (1) := true
exit
elsif posO (7) = 10 then
Pic.Draw (picO, 200, 200, picMerge)
posO (2) + = 5
posO (5) + = 5
posO (7) + = 5
posO (8) + = 5
turn := true
square (5) := true
exit
elsif posO (7) = 11 then
Pic.Draw (picO, 300, 300, picMerge)
posO (3) + = 4
posO (6) + = 4
posO (7) + = 4
turn := true
square (9) := true
exit
end if
end if
if AICheck (8) = false then
if posO (8) = 13 then
Pic.Draw (picO, 100, 300, picMerge)
posO (3) + = 2
posO (4) + = 2
posO (8) + = 2
turn := true
square (7) := true
exit
elsif posO (8) = 10 then
Pic.Draw (picO, 200, 200, picMerge)
posO (2) + = 5
posO (5) + = 5
posO (7) + = 5
posO (8) + = 5
turn := true
square (5) := true
exit
elsif posO (8) = 7 then
Pic.Draw (picO, 300, 100, picMerge)
posO (1) + = 8
posO (6) + = 8
posO (8) + = 8
turn := true
square (3) := true
exit
end if
end if
if AICheck (1) = false then
if posX (1) = 7 then
Pic.Draw (picO, 300, 100, picMerge)
posO (1) + = 8
posO (6) + = 8
posO (8) + = 8
turn := true
square (3) := true
exit
elsif posX (1) = 9 then
Pic.Draw (picO, 100, 100, picMerge)
posO (1) + = 6
posO (4) + = 6
posO (7) + = 6
turn := true
square (1) := true
exit
elsif posX (1) = 14 then
Pic.Draw (picO, 200, 100, picMerge)
posO (1) + = 1
posO (5) + = 1
turn := true
square (2) := true
exit
end if
end if
if AICheck (2) = false then
if posX (2) = 10 then
Pic.Draw (picO, 200, 200, picMerge)
posO (2) + = 5
posO (5) + = 5
posO (7) + = 5
posO (8) + = 5
turn := true
square (5) := true
exit
elsif posX (2) = 12 then
Pic.Draw (picO, 300, 200, picMerge)
posO (2) + = 3
posO (6) + = 3
turn := true
square (6) := true
exit
elsif posX (2) = 8 then
Pic.Draw (picO, 100, 200, picMerge)
posO (2) + = 7
posO (4) + = 7
turn := true
square (4) := true
exit
end if
end if
if AICheck (3) = false then
if posX (3) = 13 then
Pic.Draw (picO, 100, 300, picMerge)
posO (3) + = 2
posO (4) + = 2
posO (8) + = 2
turn := true
square (7) := true
exit
elsif posX (3) = 6 then
Pic.Draw (picO, 200, 300, picMerge)
posO (3) + = 9
posO (5) + = 9
turn := true
square (8) := true
exit
elsif posX (3) = 11 then
Pic.Draw (picO, 300, 300, picMerge)
posO (3) + = 4
posO (6) + = 4
posO (7) + = 4
turn := true
square (9) := true
exit
end if
end if
if AICheck (4) = false then
if posX (4) = 9 then
Pic.Draw (picO, 100, 100, picMerge)
posO (1) + = 6
posO (4) + = 6
posO (7) + = 6
turn := true
square (1) := true
exit
elsif posX (4) = 8 then
Pic.Draw (picO, 100, 200, picMerge)
posO (2) + = 7
posO (4) + = 7
turn := true
square (4) := true
exit
elsif posX (4) = 13 then
Pic.Draw (picO, 100, 300, picMerge)
posO (3) + = 2
posO (4) + = 2
posO (8) + = 2
turn := true
square (7) := true
exit
end if
end if
if AICheck (5) = false then
if posX (5) = 14 then
Pic.Draw (picO, 200, 100, picMerge)
posO (1) + = 1
posO (5) + = 1
turn := true
square (2) := true
exit
elsif posX (5) = 10 then
Pic.Draw (picO, 200, 200, picMerge)
posO (2) + = 5
posO (5) + = 5
posO (7) + = 5
posO (8) + = 5
turn := true
square (5) := true
exit
elsif posX (5) = 6 then
Pic.Draw (picO, 200, 300, picMerge)
posO (3) + = 9
posO (5) + = 9
turn := true
square (8) := true
exit
end if
end if
if AICheck (6) = false then
if posX (6) = 7 then
Pic.Draw (picO, 300, 100, picMerge)
posO (1) + = 8
posO (6) + = 8
posO (8) + = 8
turn := true
square (3) := true
exit
elsif posX (6) = 12 then
Pic.Draw (picO, 300, 200, picMerge)
posO (2) + = 3
posO (6) + = 3
turn := true
square (6) := true
exit
elsif posX (6) = 11 then
Pic.Draw (picO, 300, 300, picMerge)
posO (3) + = 4
posO (6) + = 4
posO (7) + = 4
turn := true
square (9) := true
exit
end if
end if
if AICheck (7) = false then
if posX (7) = 9 then
Pic.Draw (picO, 100, 100, picMerge)
posO (1) + = 6
posO (4) + = 6
posO (7) + = 6
turn := true
square (1) := true
exit
elsif posX (7) = 10 then
Pic.Draw (picO, 200, 200, picMerge)
posO (2) + = 5
posO (5) + = 5
posO (7) + = 5
posO (8) + = 5
turn := true
square (5) := true
exit
elsif posX (7) = 11 then
Pic.Draw (picO, 300, 300, picMerge)
posO (3) + = 4
posO (6) + = 4
posO (7) + = 4
turn := true
square (9) := true
exit
end if
end if
if AICheck (8) = false then
if posX (8) = 13 then
Pic.Draw (picO, 100, 300, picMerge)
posO (3) + = 2
posO (4) + = 2
posO (8) + = 2
turn := true
square (7) := true
exit
elsif posX (8) = 10 then
Pic.Draw (picO, 200, 200, picMerge)
posO (2) + = 5
posO (5) + = 5
posO (7) + = 5
posO (8) + = 5
turn := true
square (5) := true
exit
elsif posX (8) = 7 then
Pic.Draw (picO, 300, 100, picMerge)
posO (1) + = 8
posO (6) + = 8
posO (8) + = 8
turn := true
square (3) := true
exit
end if
end if
randCell := Rand.Int (1, 9)
if square (randCell ) = false then
if randCell = 1 then
Pic.Draw (picO, 100, 100, picMerge)
posO (1) + = 6
posO (4) + = 6
posO (7) + = 6
turn := true
square (1) := true
exit
elsif (randCell = 2 and posX (2) ~ = 5) or (randCell = 2 and posO (4) > 0) then
Pic.Draw (picO, 200, 100, picMerge)
posO (1) + = 1
posO (5) + = 1
turn := true
square (2) := true
exit
elsif randCell = 3 then
Pic.Draw (picO, 300, 100, picMerge)
posO (1) + = 8
posO (6) + = 8
posO (8) + = 8
turn := true
square (3) := true
exit
elsif (randCell = 4 and posX (2) ~ = 5) or (randCell = 4 and posO (2) > 0) then
Pic.Draw (picO, 100, 200, picMerge)
posO (2) + = 7
posO (4) + = 7
turn := true
square (4) := true
exit
elsif randCell = 5 then
Pic.Draw (picO, 200, 200, picMerge)
posO (2) + = 5
posO (5) + = 5
posO (7) + = 5
posO (8) + = 5
turn := true
square (5) := true
exit
elsif (randCell = 6 and posX (2) ~ = 5) or (randCell = 6 and posO (2) > 0) then
Pic.Draw (picO, 300, 200, picMerge)
posO (2) + = 3
posO (6) + = 3
turn := true
square (6) := true
exit
elsif randCell = 7 then
Pic.Draw (picO, 100, 300, picMerge)
posO (3) + = 2
posO (4) + = 2
posO (8) + = 2
turn := true
square (7) := true
exit
elsif (randCell = 8 and posX (4) ~ = 5) or (randCell = 8 and posO (4) > 0) then
Pic.Draw (picO, 200, 300, picMerge)
posO (3) + = 9
posO (5) + = 9
turn := true
square (8) := true
exit
elsif randCell = 9 then
Pic.Draw (picO, 300, 300, picMerge)
posO (3) + = 4
posO (6) + = 4
posO (7) + = 4
turn := true
square (9) := true
exit
end if
end if
end loop
end if
end AI
procedure DrawStuff
drawbox (100, 100, 200, 200, 1) % bottom-left
drawbox (200, 100, 300, 200, 1) % bottom-centre
drawbox (300, 100, 400, 200, 1) % bottom-right
drawbox (100, 200, 200, 300, 1) % mid-left
drawbox (200, 200, 300, 300, 1) % mid-centre
drawbox (300, 200, 400, 300, 1) % mid-right
drawbox (100, 300, 200, 400, 1) % top-left
drawbox (200, 300, 300, 400, 1) % top-centre
drawbox (300, 300, 400, 400, 1)
end DrawStuff
loop
cls
Restart
gameOn := true
DrawStuff
Font.Draw ("Tic Tac Toe", maxx div 2 - 40, maxy - 70, font, 16)
Font.Draw ("Wins for X: " + intstr (winsX ), maxx div 2 - 90, maxy - 100, fontWins, 16)
Font.Draw ("Wins for O: " + intstr (winsO ), maxx div 2 + 30, maxy - 100, fontWins, 16)
Font.Draw ("Ties: " + intstr (winNo ), maxx div 2 - 10, maxy - 120, fontWins, 16)
Font.Draw ("Press 'E' to exit and 'R' to play again", maxx div 2 - 100, maxy - 140, fontWins, 16)
loop
exit when gameOn = false
Mouse.Where (x, y, b )
if steve = true then
AI
end if
if x > 100 and x < 200 and y > 100 and
y < 200 and b = 1 and square (1) = false then
if turn = true then
Pic.Draw (picX, 100, 100, picMerge)
posX (1) + = 6
posX (4) + = 6
posX (7) + = 6
turn := false
else
Pic.Draw (picO, 100, 100, picMerge)
posO (1) + = 6
posO (4) + = 6
posO (7) + = 6
turn := true
end if
square (1) := true
elsif x > 200 and x < 300 and y > 100 and
y < 200 and b = 1 and square (2) = false then
if turn = true then
Pic.Draw (picX, 200, 100, picMerge)
posX (1) + = 1
posX (5) + = 1
turn := false
else
Pic.Draw (picO, 200, 100, picMerge)
posO (1) + = 1
posO (5) + = 1
turn := true
end if
square (2) := true
elsif x > 300 and x < 400 and y > 100 and
y < 200 and b = 1 and square (3) = false then
if turn = true then
Pic.Draw (picX, 300, 100, picMerge)
posX (1) + = 8
posX (6) + = 8
posX (8) + = 8
turn := false
else
Pic.Draw (picO, 300, 100, picMerge)
posO (1) + = 8
posO (6) + = 8
posO (8) + = 8
turn := true
end if
square (3) := true
elsif x > 100 and x < 200 and y > 200 and
y < 300 and b = 1 and square (4) = false then
if turn = true then
Pic.Draw (picX, 100, 200, picMerge)
posX (2) + = 7
posX (4) + = 7
turn := false
else
Pic.Draw (picO, 100, 200, picMerge)
posO (2) + = 7
posO (4) + = 7
turn := true
end if
square (4) := true
elsif x > 200 and x < 300 and y > 200 and
y < 300 and b = 1 and square (5) = false then
if turn = true then
Pic.Draw (picX, 200, 200, picMerge)
posX (2) + = 5
posX (5) + = 5
posX (7) + = 5
posX (8) + = 5
turn := false
else
Pic.Draw (picO, 200, 200, picMerge)
posO (2) + = 5
posO (5) + = 5
posO (7) + = 5
posO (8) + = 5
turn := true
end if
square (5) := true
elsif x > 300 and x < 400 and y > 200 and
y < 300 and b = 1 and square (6) = false then
if turn = true then
Pic.Draw (picX, 300, 200, picMerge)
posX (2) + = 3
posX (6) + = 3
turn := false
else
Pic.Draw (picO, 300, 200, picMerge)
posO (2) + = 3
posO (6) + = 3
turn := true
end if
square (6) := true
elsif x > 100 and x < 200 and y > 300 and
y < 400 and b = 1 and square (7) = false then
if turn = true then
Pic.Draw (picX, 100, 300, picMerge)
posX (3) + = 2
posX (4) + = 2
posX (8) + = 2
turn := false
else
Pic.Draw (picO, 100, 300, picMerge)
posO (3) + = 2
posO (4) + = 2
posO (8) + = 2
turn := true
end if
square (7) := true
elsif x > 200 and x < 300 and y > 300 and
y < 400 and b = 1 and square (8) = false then
if turn = true then
Pic.Draw (picX, 200, 300, picMerge)
posX (3) + = 9
posX (5) + = 9
turn := false
else
Pic.Draw (picO, 200, 300, picMerge)
posO (3) + = 9
posO (5) + = 9
turn := true
end if
square (8) := true
elsif x > 300 and x < 400 and y > 300 and
y < 400 and b = 1 and square (9) = false then
if turn = true then
Pic.Draw (picX, 300, 300, picMerge)
posX (3) + = 4
posX (6) + = 4
posX (7) + = 4
turn := false
else
Pic.Draw (picO, 300, 300, picMerge)
posO (3) + = 4
posO (6) + = 4
posO (7) + = 4
turn := true
end if
square (9) := true
end if
for i : 1 .. 8
if posX (i ) = 15 then
gameOn := false
winsX + = 1
if i = 1 then
Draw.ThickLine (100, 150, 400, 150, 7, 12)
elsif i = 2 then
Draw.ThickLine (100, 250, 400, 250, 7, 12)
elsif i = 3 then
Draw.ThickLine (100, 350, 400, 350, 7, 12)
elsif i = 4 then
Draw.ThickLine (150, 100, 150, 400, 7, 12)
elsif i = 5 then
Draw.ThickLine (250, 100, 250, 400, 7, 12)
elsif i = 6 then
Draw.ThickLine (350, 100, 350, 400, 7, 12)
elsif i = 7 then
Draw.ThickLine (100, 100, 400, 400, 7, 12)
elsif i = 8 then
Draw.ThickLine (100, 400, 400, 100, 7, 12)
end if
elsif posO (i ) = 15 then
gameOn := false
winsO + = 1
if i = 1 then
Draw.ThickLine (100, 150, 400, 150, 7, 12)
elsif i = 2 then
Draw.ThickLine (100, 250, 400, 250, 7, 12)
elsif i = 3 then
Draw.ThickLine (100, 350, 400, 350, 7, 12)
elsif i = 4 then
Draw.ThickLine (150, 100, 150, 400, 7, 12)
elsif i = 5 then
Draw.ThickLine (250, 100, 250, 400, 7, 12)
elsif i = 6 then
Draw.ThickLine (350, 100, 350, 400, 7, 12)
elsif i = 7 then
Draw.ThickLine (100, 100, 400, 400, 7, 12)
elsif i = 8 then
Draw.ThickLine (100, 400, 400, 100, 7, 12)
end if
end if
end for
if square (1) = true and square (2) = true and
square (3) = true and square (4) = true and
square (5) = true and square (6) = true and square (7) = true and
square (8) = true and square (9) = true then
gameOn := false
winNo + = 1
end if
end loop
turns + = 1
loop
Input.KeyDown (keyCheck )
if keyCheck ('R') then
exit
end if
if keyCheck ('E') then
exitVar := true
exit
end if
end loop
exit when exitVar
end loop
|
This is for my final project i have two problems and i need help with one thing 1) Problem is that why i want to play again the game and i press R, it doesn't work and when i want to quit the game it doesn't work. 2) Problem is that i want the user to choose either X or O rather than making X for the user and O for the computer 2 is that i want to include two modes one is being computer other is play with a friend basically, for example if u press 1 u can play with computer and if press 2 u can play with a friend.
Mod Edit:
Please wrap your code in either of the following in order to preserve whitespace (indentation) and to highlight the syntax.
code: |
[syntax="turing"] ... code ... [/syntax]
[code] ... code ... [/code ]
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Zren
|
Posted: Thu Jan 15, 2015 6:15 pm Post subject: RE:Tic Tac Toe Using Turing |
|
|
Quote: Problem is that why i want to play again the game and i press R, it doesn't work
This doesn't work?
Turing: |
loop
Input.KeyDown (keyCheck )
if keyCheck ('R') then
exit
end if
if keyCheck ('E') then
exitVar := true
exit
end if
end loop
|
Or do you want to be able to restart/quit DURING a game?
~~~
Quote: Problem is that i want the user to choose either X or O rather than making X for the user and O for the computer 2 is that i want to include two modes one is being computer other is play with a friend basically, for example if u press 1 u can play with computer and if press 2 u can play with a friend.
Instead of having an arrays of booleans to represent each cell, why not have an array of a data type that can accept a least 3 states (empty cell, X, O). Strings have WAAAAY more than 3 possible states, but are much easier to read. Just watch out for typos.
Turing: |
% The Grid
var grid := array 1 .. 3, 1 .. 3 of string
% The states each cell can be.
var emptyCell := " "
var currentPlayer := "X"
var enemyPlayer := "O"
% Initialize
for y : 1..3
for x : 1..3
grid(x, y) := emptyCell
end for
end for
% AI
if grid(1,1) = enemyPlayer and grid(1,2) = enemyPlayer and grid(1,3) = emptyCell then
grid(1,3) := currentPlayer
end if
|
I kinda threw multi dimensional arrays at you, but it should make sense to convert it to a single dimensional array like you're doing. |
|
|
|
|
|
|
|