Computer Science Canada

Function Check with Tic Tac Toe

Author:  Ennsy_908 [ Wed May 30, 2007 8:29 am ]
Post subject:  Function Check with Tic Tac Toe

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Tic Tac Toe %
% Created by: Chris Enns %
% ICS 3M1 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
setscreen ("graphics 200; 200")
proc GameBoard %Draws Game Board
Draw.ThickLine (325, 150, 580, 150, 5, 16) % Bottom Horizontal Line
Draw.ThickLine (410, 65, 410, 320, 5, 16) % Left Vertical Line
Draw.ThickLine (495, 65, 495, 320, 5, 16) % Right Vertical Line
Draw.ThickLine (325, 235, 580, 235, 5, 16) % Top Horizontal Line
end GameBoard
proc XBL % Draws X in bottom left spot
Draw.ThickLine (335, 140, 400, 70, 3, 12)
Draw.ThickLine (335, 70, 400, 140, 3, 12)
end XBL

proc XBM % Draws X in bottom middle spot
Draw.ThickLine (420, 140, 485, 70, 3, 12)
Draw.ThickLine (420, 70, 485, 140, 3, 12)
end XBM

proc XBR % Draws X in bottom right spot
Draw.ThickLine (505, 140, 570, 70, 3, 12)
Draw.ThickLine (505, 70, 570, 140, 3, 12)
end XBR

proc XML % Draws X in middle left spot
Draw.ThickLine (335, 225, 400, 160, 3, 12)
Draw.ThickLine (335, 160, 400, 225, 3, 12)
end XML

proc XM % Draws X in middle spot
Draw.ThickLine (420, 225, 485, 160, 3, 12)
Draw.ThickLine (420, 160, 485, 225, 3, 12)
end XM

proc XMR % Draws X in middle right spot
Draw.ThickLine (505, 225, 580, 160, 3, 12)
Draw.ThickLine (505, 160, 580, 225, 3, 12)
end XMR

proc XTL % Draws X in top left spot
Draw.ThickLine (335, 315, 400, 245, 3, 12)
Draw.ThickLine (335, 246, 400, 315, 3, 12)
end XTL

proc XTM % Draws X in top middle spot
Draw.ThickLine (420, 315, 485, 245, 3, 12)
Draw.ThickLine (420, 245, 485, 315, 3, 12)
end XTM

proc XTR % Draws X in top right spot
Draw.ThickLine (505, 315, 580, 245, 3, 12)
Draw.ThickLine (505, 245, 580, 315, 3, 12)
end XTR

proc OBL % Draw O in bottom left spot
Draw.Oval (367, 107, 30, 30, blue)
Draw.Oval (367, 107, 37, 37, blue)
Draw.Fill (367, 140, blue, blue)
end OBL

proc OBM % Draws O in bottom middle spot
Draw.Oval (452, 107, 30, 30, blue)
Draw.Oval (452, 107, 37, 37, blue)
Draw.Fill (452, 140, blue, blue)
end OBM

proc OBR % Draws O in bottom right spot
Draw.Oval (537, 107, 30, 30, blue)
Draw.Oval (537, 107, 37, 37, blue)
Draw.Fill (537, 140, blue, blue)
end OBR

proc OML % Draws O in middle left spot
Draw.Oval (367, 192, 30, 30, blue)
Draw.Oval (367, 192, 37, 37, blue)
Draw.Fill (367, 223, blue, blue)
end OML

proc OM % Draws O in middle spot
Draw.Oval (452, 192, 30, 30, blue)
Draw.Oval (452, 192, 37, 37, blue)
Draw.Fill (452, 223, blue, blue)
end OM

proc OMR % Draws O in middle right spot
Draw.Oval (537, 192, 30, 30, blue)
Draw.Oval (537, 192, 37, 37, blue)
Draw.Fill (537, 223, blue, blue)
end OMR

proc OTL % Draws O in top left spot
Draw.Oval (367, 277, 30, 30, blue)
Draw.Oval (367, 277, 37, 37, blue)
Draw.Fill (367, 310, blue, blue)
end OTL

proc OTM % Draws O in top middle spot
Draw.Oval (452, 277, 30, 30, blue)
Draw.Oval (452, 277, 37, 37, blue)
Draw.Fill (452, 310, blue, blue)
end OTM

proc OTR % Draws O in top right spot
Draw.Oval (537, 277, 30, 30, blue)
Draw.Oval (537, 277, 37, 37, blue)
Draw.Fill (537, 310, blue, blue)
end OTR

proc DrawX % Draws x in place selected
var x, y, b, r : int
loop
Mouse.Where (x, y, b)

if b = 1 and x > 325 and x < 410
and y > 65 and y < 150 then
XBL

exit when b = 1
elsif b = 1 and x > 410 and x < 495
and y > 65 and y < 150 then
XBM
exit when b = 1
elsif b = 1 and x > 495 and x < 580
and y > 65 and y < 150 then
XBR
exit when b = 1
elsif b = 1 and x > 325 and x < 410
and y > 150 and y < 235 then
XML
exit when b = 1
elsif b = 1 and x > 410 and x < 495
and y > 150 and y < 235 then
XM
exit when b = 1
elsif b = 1 and x > 495 and x < 580
and y > 150 and y < 235 then
XMR
exit when b = 1
elsif b = 1 and x > 325 and x < 410
and y > 235 and y < 320 then
XTL
exit when b = 1
elsif b = 1 and x > 410 and x < 495
and y > 235 and y < 320 then
XTM
exit when b = 1
elsif b = 1 and x > 495 and x < 580
and y > 235 and y < 320 then
XTR
exit when b = 1
end if
end loop
end DrawX

proc DrawO % Draws O in place selected
var x, y, b : int
loop
Mouse.Where (x, y, b)

if b = 1 and x > 325 and x < 410
and y > 65 and y < 150 then
OBL
exit when b = 1
elsif b = 1 and x > 410 and x < 495
and y > 65 and y < 150 then
OBM
exit when b = 1
elsif b = 1 and x > 495 and x < 580
and y > 65 and y < 150 then
OBR
exit when b = 1
elsif b = 1 and x > 325 and x < 410
and y > 150 and y < 235 then
OML
exit when b = 1
elsif b = 1 and x > 410 and x < 495
and y > 150 and y < 235 then
OM
exit when b = 1
elsif b = 1 and x > 495 and x < 580
and y > 150 and y < 235 then
OMR
exit when b = 1
elsif b = 1 and x > 325 and x < 410
and y > 235 and y < 320 then
OTL
exit when b = 1
elsif b = 1 and x > 410 and x < 495
and y > 235 and y < 320 then
OTM
exit when b = 1
elsif b = 1 and x > 495 and x < 580
and y > 235 and y < 320 then
OTR
exit when b = 1
end if
end loop
end DrawO

var Check : array 1 .. 3, 1 .. 3 of int := init (0, 0, 0, 0, 0, 0, 0, 0, 0)

function Win : boolean
for i : 1 .. 3
if Check (i, 1) not= 0 and Check (i, 1) = Check (i, 2)
and Check (i, 1) = Check (i, 3) then
cls
result true
end if

if Check (1, i) not= 0 and Check (1, i) = Check (2, i)
and Check (1, i) = Check (3, i) then
put " win 2"
result true
end if
if Check (2, 2) not= 0 and Check (1, 1) = Check (2, 2)
and Check (2, 2) = Check (3, 3) then
put "Win 3"
result true

elsif Check (2, 2) not= 0 and Check (1, 3) = Check (2, 2)
and Check (2, 2) = Check (3, 1) then
put " Win 4"
result true
end if
result false
end for
end Win
put "X starts"
put ""
put "Click were you want to put an X or O"
delay (500)


GameBoard
DrawX
delay (500)
DrawO
delay (500)
DrawX
delay (500)
DrawO
delay (500)
DrawX
delay (500)
put Win
DrawO
delay (500)
put Win
DrawX
delay (500)
put Win
DrawO
delay (500)
put Win
DrawX
delay (500)
put Win


I Can't Figure out why the Check Doesn't Work

When I run it it comes up as false

Author:  Albrecd [ Wed May 30, 2007 1:38 pm ]
Post subject:  Re: Function Check with Tic Tac Toe

The reason your check won't work is that you never change the array Check after it is declared. Every time an X or an O is drawn you need to change the value of the proper element of Check; otherwise, your checking function will always return false.

Author:  agnivohneb [ Wed May 30, 2007 5:01 pm ]
Post subject:  RE:Function Check with Tic Tac Toe

Your program is to complex. I made a Tic Tac Toe game last semester for my ICS 3O1 summative and it is 138 lines, nearly half of what yours is and i added the feature to save and load games, and set usernames. I got 95% on it. Keep working at it. Plan how your going to set up your functions and procedures.

I'm not trying to put you down, just trying to help you make a better program.

At the end of the semester I will post mine (I'm not posting now you will cheat) and we can see who is better.

Smile

Author:  Ennsy_908 [ Fri Jun 01, 2007 8:45 am ]
Post subject:  Re: Function Check with Tic Tac Toe

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Tic Tac Toe %
% Created by: Chris Enns %
% ICS 3M1 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
var Check : array 1 .. 3, 1 .. 3 of int := init (0, 0, 0, 0, 0, 0, 0, 0, 0)
var x, y, b : int
% Instructions
put " TicTacToe"
put "The object of the game is to get a row of X's or a row of O's before your"
put "opponent. These rows can be vertical, horizontal, or diagonal. Click Where"
put " you want to place an X or an O"
put ""
put "********************************************************************************"
put ""
put " IMPORTANT: DO NOT CLICK WHHERE AN X OR O HAS ALREADY BEEN DRAWN. THIS WILL"
put " CAUSE AN OVERLAP AND YOU WILL NOT BE ABLE TO FINISH THE GAME"
put ""
put "********************************************************************************"
put ""
put " Click anywhere to play"
loop
Mouse.Where (x, y, b)
if b = 1 then
cls
exit when b = 1
end if
end loop
proc GameBoard %Draws Game Board
Draw.ThickLine (325, 150, 580, 150, 5, 16) % Bottom Horizontal Line
Draw.ThickLine (410, 65, 410, 320, 5, 16) % Left Vertical Line
Draw.ThickLine (495, 65, 495, 320, 5, 16) % Right Vertical Line
Draw.ThickLine (325, 235, 580, 235, 5, 16) % Top Horizontal Line
end GameBoard

proc XBL % Draws X in bottom left spot
Draw.ThickLine (335, 140, 400, 70, 3, 12)
Draw.ThickLine (335, 70, 400, 140, 3, 12)
end XBL

proc XBM % Draws X in bottom middle spot
Draw.ThickLine (420, 140, 485, 70, 3, 12)
Draw.ThickLine (420, 70, 485, 140, 3, 12)
end XBM

proc XBR % Draws X in bottom right spot
Draw.ThickLine (505, 140, 570, 70, 3, 12)
Draw.ThickLine (505, 70, 570, 140, 3, 12)
end XBR

proc XML % Draws X in middle left spot
Draw.ThickLine (335, 225, 400, 160, 3, 12)
Draw.ThickLine (335, 160, 400, 225, 3, 12)
end XML

proc XM % Draws X in middle spot
Draw.ThickLine (420, 225, 485, 160, 3, 12)
Draw.ThickLine (420, 160, 485, 225, 3, 12)
end XM

proc XMR % Draws X in middle right spot
Draw.ThickLine (505, 225, 580, 160, 3, 12)
Draw.ThickLine (505, 160, 580, 225, 3, 12)
end XMR

proc XTL % Draws X in top left spot
Draw.ThickLine (335, 315, 400, 245, 3, 12)
Draw.ThickLine (335, 246, 400, 315, 3, 12)
end XTL

proc XTM % Draws X in top middle spot
Draw.ThickLine (420, 315, 485, 245, 3, 12)
Draw.ThickLine (420, 245, 485, 315, 3, 12)
end XTM

proc XTR % Draws X in top right spot
Draw.ThickLine (505, 315, 580, 245, 3, 12)
Draw.ThickLine (505, 245, 580, 315, 3, 12)
end XTR

proc OBL % Draw O in bottom left spot
Draw.Oval (367, 107, 30, 30, blue)
Draw.Oval (367, 107, 37, 37, blue)
Draw.Fill (367, 140, blue, blue)
end OBL

proc OBM % Draws O in bottom middle spot
Draw.Oval (452, 107, 30, 30, blue)
Draw.Oval (452, 107, 37, 37, blue)
Draw.Fill (452, 140, blue, blue)
end OBM

proc OBR % Draws O in bottom right spot
Draw.Oval (537, 107, 30, 30, blue)
Draw.Oval (537, 107, 37, 37, blue)
Draw.Fill (537, 140, blue, blue)
end OBR

proc OML % Draws O in middle left spot
Draw.Oval (367, 192, 30, 30, blue)
Draw.Oval (367, 192, 37, 37, blue)
Draw.Fill (367, 223, blue, blue)
end OML

proc OM % Draws O in middle spot
Draw.Oval (452, 192, 30, 30, blue)
Draw.Oval (452, 192, 37, 37, blue)
Draw.Fill (452, 223, blue, blue)
end OM

proc OMR % Draws O in middle right spot
Draw.Oval (537, 192, 30, 30, blue)
Draw.Oval (537, 192, 37, 37, blue)
Draw.Fill (537, 223, blue, blue)
end OMR

proc OTL % Draws O in top left spot
Draw.Oval (367, 277, 30, 30, blue)
Draw.Oval (367, 277, 37, 37, blue)
Draw.Fill (367, 310, blue, blue)
end OTL

proc OTM % Draws O in top middle spot
Draw.Oval (452, 277, 30, 30, blue)
Draw.Oval (452, 277, 37, 37, blue)
Draw.Fill (452, 310, blue, blue)
end OTM

proc OTR % Draws O in top right spot
Draw.Oval (537, 277, 30, 30, blue)
Draw.Oval (537, 277, 37, 37, blue)
Draw.Fill (537, 310, blue, blue)
end OTR

proc DrawX % Draws x in place selected

loop
Mouse.Where (x, y, b)

if b = 1 and x > 325 and x < 410
and y > 65 and y < 150 then
XBL
Check (1, 1) := 1
exit when b = 1
elsif b = 1 and x > 410 and x < 495
and y > 65 and y < 150 then
XBM
Check (1, 2) := 1
exit when b = 1
elsif b = 1 and x > 495 and x < 580
and y > 65 and y < 150 then
XBR
Check (1, 3) := 1
exit when b = 1
elsif b = 1 and x > 325 and x < 410
and y > 150 and y < 235 then
XML
Check (2, 1) := 1
exit when b = 1
elsif b = 1 and x > 410 and x < 495
and y > 150 and y < 235 then
XM
Check (2, 2) := 1
exit when b = 1
elsif b = 1 and x > 495 and x < 580
and y > 150 and y < 235 then
XMR
Check (2, 3) := 1
exit when b = 1
elsif b = 1 and x > 325 and x < 410
and y > 235 and y < 320 then
XTL
Check (3, 1) := 1
exit when b = 1
elsif b = 1 and x > 410 and x < 495
and y > 235 and y < 320 then
XTM
Check (3, 2) := 1
exit when b = 1
elsif b = 1 and x > 495 and x < 580
and y > 235 and y < 320 then
XTR
Check (3, 3) := 1
exit when b = 1
end if
end loop
end DrawX

proc DrawO % Draws O in place selected
var x, y, b : int
loop
Mouse.Where (x, y, b)

if b = 1 and x > 325 and x < 410
and y > 65 and y < 150 then
OBL
Check (1, 1) := 2
exit when b = 1
elsif b = 1 and x > 410 and x < 495
and y > 65 and y < 150 then
OBM
Check (1, 2) := 2
exit when b = 1
elsif b = 1 and x > 495 and x < 580
and y > 65 and y < 150 then
OBR
Check (1, 3) := 2
exit when b = 1
elsif b = 1 and x > 325 and x < 410
and y > 150 and y < 235 then
OML
Check (2, 1) := 2
exit when b = 1
elsif b = 1 and x > 410 and x < 495
and y > 150 and y < 235 then
OM
Check (2, 2) := 2
exit when b = 1
elsif b = 1 and x > 495 and x < 580
and y > 150 and y < 235 then
OMR
Check (2, 3) := 2
exit when b = 1
elsif b = 1 and x > 325 and x < 410
and y > 235 and y < 320 then
OTL
Check (3, 1) := 2
exit when b = 1
elsif b = 1 and x > 410 and x < 495
and y > 235 and y < 320 then
OTM
Check (3, 2) := 2
exit when b = 1
elsif b = 1 and x > 495 and x < 580
and y > 235 and y < 320 then
OTR
Check (3, 3) := 2
exit when b = 1
end if
end loop
end DrawO




function Win : boolean
for i : 1 .. 3

if Check (i, 1) not= 0 and Check (i, 1) = Check (i, 2)
and Check (i, 1) = Check (i, 3) then
result true
end if

if Check (1, i) not= 0 and Check (1, i) = Check (2, i)
and Check (1, i) = Check (3, i) then
result true
end if
if Check (2, 2) not= 0 and Check (1, 1) = Check (2, 2)
and Check (2, 2) = Check (3, 3) then
result true

elsif Check (2, 2) not= 0 and Check (1, 3) = Check (2, 2)
and Check (2, 2) = Check (3, 1) then
result true
end if
result false
end for
end Win

GameBoard
DrawX
delay (500)
DrawO
delay (500)
DrawX
delay (500)
DrawO
delay (500)
DrawX
delay (500)
if Win then
put " winner"
end if
DrawO
delay (500)
if Win then
put " winner"
end if
put Win
DrawX
delay (500)
if Win then
put " winner"
end if
put Win
DrawO
delay (500)
if Win then
put " winner"
end if
put Win
DrawX
delay (500)
if Win then
put " winner"
end if
put Win


I am now giving a value to check but now only a few of the checks work and i have no idea how to fix it


: