% TIC TAC TOE
% PROGRAMMED BY: ANDREW COWAN
var x, y, buttonnumber, buttonupdown, buttons, font, p1turn, p2turn : int
var sq1, sq2, sq3, sq4, sq5, sq6, sq7, sq8, sq9, sq1taken, sq2taken, sq3taken,
sq4taken, sq5taken, sq6taken, sq7taken, sq8taken, sq9taken, sq1taken2, sq2taken2,
sq3taken2, sq4taken2, sq5taken2, sq6taken2, sq7taken2, sq8taken2, sq9taken2, sq1taken3,
sq2taken3, sq3taken3, sq4taken3, sq5taken3, sq6taken3, sq7taken3, sq8taken3,
sq9taken3, win : int := 0
var ch : string(1)
font := Font.New ("hooplah:36")
setscreen ("graphics:400,400, nobuttonbar")
p1turn := 1
p2turn := 0
% DRAW THE PLAYING FIELD
Draw.FillBox (maxx, maxy, 0, 0, 254)
Draw.FillBox (135, 0, 131, maxy, 2)
Draw.FillBox (268, 0, 264, maxy, 2)
Draw.FillBox (0, 135, maxx, 131, 2)
Draw.FillBox (0, 268, maxx, 264, 2)
% EXECUTE THIS IF SOMEONE WINS
process gg()
put "Play again? Y/N"
getch(ch)
if ch = "y" or ch = "Y" then
cls
Draw.FillBox (maxx, maxy, 0, 0, 254)
Draw.FillBox (135, 0, 131, maxy, 2)
Draw.FillBox (268, 0, 264, maxy, 2)
Draw.FillBox (0, 135, maxx, 131, 2)
Draw.FillBox (0, 268, maxx, 264, 2)
sq1 := 0
sq2 := 0
sq3 := 0
sq4 := 0
sq5 := 0
sq6 := 0
sq7 := 0
sq8 := 0
sq9 := 0
sq1taken := 0
sq2taken := 0
sq3taken := 0
sq4taken := 0
sq5taken := 0
sq6taken := 0
sq7taken := 0
sq8taken := 0
sq9taken := 0
sq1taken2 := 0
sq2taken2 := 0
sq3taken2 := 0
sq4taken2 := 0
sq5taken2 := 0
sq6taken2 := 0
sq7taken2 := 0
sq8taken2 := 0
sq9taken2 := 0
sq1taken3 := 0
sq2taken3 := 0
sq3taken3 := 0
sq1taken3 := 0
sq2taken3 := 0
sq3taken3 := 0
sq4taken3 := 0
sq5taken3 := 0
sq6taken3 := 0
sq7taken3 := 0
sq8taken3 := 0
sq9taken3 := 0
win := 0
else
put ":("
end if
end gg
loop
% WAIT FOR MOUSE BUTTON TO BE PRESSED
buttonwait("down", x, y, buttonnumber, buttonupdown)
% DETECT CLICK FOR ALL SQUARES
% DETECT SQUARE 1
if x >= 0 and x <= 133 and y >= 268 and sq1taken = 0 and sq1taken2 = 0 and win not= 1 then
if p1turn = 1 and sq1taken = 0 then
Draw.Text ("X", 50, 320, font, red)
sq1taken := 1
sq1taken3 := 1
p1turn := 0
p2turn := 1
elsif p2turn = 1 and sq1taken = 0 and sq1taken2 = 0 and win not= 1 then
Draw.Text ("O", 50, 320, font, blue)
sq1taken2 := 1
sq1taken3 := 1
p1turn := 1
p2turn := 0
end if
end if
% DETECT SQUARE 2
if x >= 134 and x <= 266 and y >= 268 and sq2taken = 0 and sq2taken2 = 0 and win not= 1 then
if p1turn = 1 and sq2taken = 0 then
Draw.Text ("X", 175, 320, font, red)
sq2taken := 1
sq2taken3 := 1
p1turn := 0
p2turn := 1
elsif p2turn = 1 and sq2taken = 0 and win not= 1 then
Draw.Text ("O", 175, 320, font, blue)
sq2taken2 := 1
sq2taken3 := 1
p1turn := 1
p2turn := 0
end if
end if
% DETECT SQUARE 3
if x >= 267 and x <= maxx and y >= 268 and sq3taken = 0 and sq3taken2 = 0 and win not= 1 then
if p1turn = 1 and sq3taken = 0 then
Draw.Text ("X", 300, 320, font, red)
sq3taken := 1
sq3taken3 := 1
p1turn := 0
p2turn := 1
end if
if p2turn = 1 and sq3taken = 0 and sq3taken2 = 0 and win not= 1 then
Draw.Text ("O", 300, 320, font, blue)
sq3taken2 := 1
sq3taken3 := 1
p1turn := 1
p2turn := 0
end if
end if
% DETECT SQUARE 4
if x >= 0 and x <= 133 and y <= 268 and y >= 135 and sq4taken = 0 and sq4taken2 = 0 and win not= 1 then
if p1turn = 1 and sq4taken = 0 then
Draw.Text ("X", 50, 175, font, red)
sq4taken := 1
sq4taken3 := 1
p1turn := 0
p2turn := 1
end if
if p2turn = 1 and sq4taken = 0 and sq4taken2 = 0 and win not= 1 then
Draw.Text ("O", 50, 175, font, blue)
sq4taken2 := 1
sq4taken3 := 1
p1turn := 1
p2turn := 0
end if
end if
% DETECT SQUARE 5
if x >= 134 and x <= 268 and y >= 135 and y <= 268 and sq5taken = 0 and sq5taken2 = 0 and win not= 1 then
if p1turn = 1 and sq5taken = 0 then
Draw.Text ("X", 175, 175, font, red)
sq5taken := 1
sq5taken3 := 1
p1turn := 0
p2turn := 1
end if
if p2turn = 1 and sq5taken = 0 and sq5taken2 = 0 and win not= 1 then
Draw.Text ("O", 175, 175, font, blue)
sq5taken2 := 1
sq5taken3 := 1
p1turn := 1
p2turn := 0
end if
end if
% DETECT SQUARE 6
if x >= 267 and x <= maxx and y >= 135 and y <= 268 and sq6taken = 0 and sq6taken2 = 0 and win not= 1 then
if p1turn = 1 and sq6taken = 0 then
Draw.Text ("X", 300, 175, font, red)
sq6taken := 1
sq6taken3 := 6
p1turn := 0
p2turn := 1
end if
if p2turn = 1 and sq6taken = 0 and sq6taken2 = 0 and win not= 1 then
Draw.Text ("O", 300, 175, font, blue)
sq6taken2 := 1
sq6taken3 := 1
p1turn := 1
p2turn := 0
end if
end if
% DETECT SQUARE 7
if x >= 0 and x <= 133 and y >= 0 and y <= 134 and sq7taken = 0 and sq7taken2 = 0 and win not= 1 then
if p1turn = 1 and sq7taken = 0 then
Draw.Text ("X", 50, 50, font, red)
sq7taken := 1
sq7taken3 := 1
p1turn := 0
p2turn := 1
end if
if p2turn = 1 and sq7taken = 0 and sq7taken2 = 0 and win not= 1 then
Draw.Text ("O", 50, 50, font, blue)
sq7taken2 := 1
sq7taken3 := 1
p1turn := 1
p2turn := 0
end if
end if
% DETECT SQUARE 8
if x >= 134 and x <= 268 and y >= 0 and y <= 134 and sq8taken = 0 and sq8taken2 = 0 and win not= 1 then
if p1turn = 1 and sq8taken = 0 then
Draw.Text ("X", 175, 50, font, red)
sq8taken := 1
sq8taken3 := 1
p1turn := 0
p2turn := 1
end if
if p2turn = 1 and sq8taken = 0 and sq8taken2 = 0 and win not= 1 then
Draw.Text ("O", 175, 50, font, blue)
sq8taken2 := 1
sq8taken3 := 1
p1turn := 1
p2turn := 0
end if
end if
% DETECT SQUARE 9
if x >= 268 and x <= maxx and y >= 0 and y <= 134 and sq9taken = 0 and sq9taken2 = 0 and win not= 1 then
if p1turn = 1 and sq9taken = 0 then
Draw.Text ("X", 300, 50, font, red)
sq9taken := 1
sq9taken3 := 1
p1turn := 0
p2turn := 1
end if
if p2turn = 1 and sq9taken = 0 and sq9taken2 = 0 and win not= 1 then
Draw.Text ("O", 300, 50, font, blue)
sq9taken2 := 1
sq9taken3 := 1
p1turn := 1
p2turn := 0
end if
end if
% CHECK IF PLAYER 1 WINS
if sq1taken = 1 and sq2taken = 1 and sq3taken = 1 then
put "X's win!"
fork gg()
win := 1
end if
if sq4taken = 1 and sq5taken = 1 and sq6taken = 1 then
put "X's win!"
fork gg()
win := 1
end if
if sq7taken = 1 and sq8taken = 1 and sq9taken = 1 then
put "X's win!"
fork gg()
win := 1
end if
if sq1taken = 1 and sq5taken = 1 and sq9taken = 1 then
put "X's win!"
fork gg()
win := 1
end if
if sq3taken = 1 and sq5taken = 1 and sq7taken = 1 then
put "X's win!"
fork gg()
win := 1
end if
if sq1taken = 1 and sq4taken = 1 and sq7taken = 1 then
put "X's win!"
fork gg()
win := 1
end if
if sq2taken = 1 and sq5taken = 1 and sq8taken = 1 then
put "X's win!"
fork gg()
win := 1
end if
if sq3taken = 1 and sq6taken = 1 and sq9taken = 1 then
put "X's win!"
fork gg()
win := 1
end if
% CHECK IF PLAYER 2 WINS
if sq1taken2 = 1 and sq2taken2 = 1 and sq3taken2 = 1 and win not= 1 then
put "O's win!"
fork gg()
win := 1
end if
if sq4taken2 = 1 and sq5taken2 = 1 and sq6taken2 = 1 and win not= 1 then
put "O's win!"
fork gg()
win := 1
end if
if sq7taken2 = 1 and sq8taken2 = 1 and sq9taken2 = 1 and win not= 1 then
put "O's win!"
fork gg()
win := 1
end if
if sq1taken2 = 1 and sq5taken2 = 1 and sq9taken2 = 1 and win not= 1 then
put "O's win!"
fork gg()
win := 1
end if
if sq3taken2 = 1 and sq5taken2 = 1 and sq7taken2 = 1 and win not= 1 then
put "O's win!"
fork gg()
win := 1
end if
if sq1taken2 = 1 and sq4taken2 = 1 and sq7taken2 = 1 and win not= 1 then
put "O's win!"
fork gg()
win := 1
end if
if sq2taken2 = 1 and sq5taken2 = 1 and sq8taken2 = 1 and win not= 1 then
put "O's win!"
fork gg()
win := 1
end if
if sq3taken2 = 1 and sq6taken2 = 1 and sq9taken2 = 1and win not= 1 then
put "O's win!"
fork gg()
win := 1
end if
% CHECK IF NOBODY WINS
if sq1taken3 = 1 and sq2taken3 = 1 and sq3taken3 = 1 and sq4taken3 = 1 and sq5taken3 = 1
and sq6taken3 = 1 and sq7taken3 = 1 and sq8taken3 = 1 and sq9taken3 = 1 and win not= 1 then
put "It's a tie!"
fork gg()
win := 1
end if
end loop
|