
-----------------------------------
enjoy
Tue Nov 09, 2004 11:40 am

School Project.... PLEASE help
-----------------------------------
i did this much... but the program is not working the way i wanted to work!!!  .... could any please help me :lol: ??

its a tic tac toe game but not working properly
this is the code!



var font7 : int := Font.New ("Times:50")
var where1, where2 : int
var ans1, ans2 : int := 0
var counter : int := -1

drawfillbox (0, 0, 480, 275, black)
color (white)
colorback (black)
put " PLAYER 1!" : 70 ..
put "PLAYER 2!"
drawfillbox (180, 270, 175, 10, white)
drawfillbox (300, 270, 305, 10, white)
drawfillbox (75, 190, 405, 185, white)
drawfillbox (75, 85, 405, 90, white)
drawbox (75, 10, 405, 270, red)
for a : 0 .. 35
    drawline (177, 270, 177, 10, a)
    drawline (178, 270, 178, 10, a)
    drawline (302, 270, 302, 10, a)
    drawline (303, 270, 303, 10, a)
    drawline (75, 187, 405, 187, a)
    drawline (75, 188, 405, 188, a)
    drawline (75, 87, 405, 87, a)
    drawline (75, 88, 405, 88, a)
end for
Font.Draw ("1", 105, 210, font7, white)
Font.Draw ("2", 225, 210, font7, white)
Font.Draw ("3", 330, 210, font7, white)
Font.Draw ("4", 105, 115, font7, white)
Font.Draw ("5", 220, 115, font7, white)
Font.Draw ("6", 330, 115, font7, white)
Font.Draw ("7", 105, 30, font7, white)
Font.Draw ("8", 220, 30, font7, white)
Font.Draw ("9", 330, 30, font7, white)

procedure player_x
    loop
        counter := counter + 1
        locate (3, 1)
        exit when counter = 9 or ans1 = 6 or ans1 = 150 or ans1 = 2400
            or ans1 = 741 or ans1 = 852 or ans1 = 963 or ans1 = 951 or
            ans1 = 753 or ans2 = 6 or ans2 = 150 or ans2 = 2400
            or ans2 = 741 or ans2 = 852 or ans2 = 963 or ans2 = 951 or
            ans2 = 753
        get where1

        if where1 = 1 then
            ans1 := ans1 + 1
            Font.Draw ("1", 105, 210, font7, black)
            Font.Draw ("X", 105, 210, font7, green)
        elsif where1 = 2 then
            ans1 := ans1 + 2
            Font.Draw ("2", 225, 210, font7, black)
            Font.Draw ("X", 225, 210, font7, green)
        elsif where1 = 3 then
            ans1 := ans1 + 3
            Font.Draw ("3", 330, 210, font7, black)
            Font.Draw ("X", 330, 210, font7, green)
        elsif where1 = 4 then
            ans1 := ans1 + 40
            Font.Draw ("4", 105, 115, font7, black)
            Font.Draw ("X", 105, 115, font7, green)
        elsif where1 = 5 then
            ans1 := ans1 + 50
            Font.Draw ("5", 220, 115, font7, black)
            Font.Draw ("X", 220, 115, font7, green)
        elsif where1 = 6 then
            ans1 := ans1 + 60
            Font.Draw ("6", 330, 115, font7, black)
            Font.Draw ("X", 330, 115, font7, green)
        elsif where1 = 7 then
            ans1 := ans1 + 700
            Font.Draw ("7", 105, 30, font7, black)
            Font.Draw ("X", 105, 30, font7, green)
        elsif where1 = 8 then
            ans1 := ans1 + 800
            Font.Draw ("8", 220, 30, font7, black)
            Font.Draw ("X", 220, 30, font7, green)
        elsif where1 = 9 then
            ans1 := ans1 + 900
            Font.Draw ("9", 330, 30, font7, black)
            Font.Draw ("X", 330, 30, font7, green)
        end if

        get where2
        if where2 = 1 then
            ans2 := ans2 + 1
            Font.Draw ("1", 105, 210, font7, black)
            Font.Draw ("O", 105, 210, font7, green)
        elsif where2 = 2 then
            ans2 := ans2 + 2
            Font.Draw ("2", 225, 210, font7, black)
            Font.Draw ("O", 225, 210, font7, green)
        elsif where2 = 3 then
            ans2 := ans2 + 3
            Font.Draw ("3", 330, 210, font7, black)
            Font.Draw ("O", 330, 210, font7, green)
        elsif where2 = 4 then
            ans2 := ans2 + 40
            Font.Draw ("4", 105, 115, font7, black)
            Font.Draw ("O", 105, 115, font7, green)
        elsif where2 = 5 then
            ans2 := ans2 + 50
            Font.Draw ("5", 220, 115, font7, black)
            Font.Draw ("O", 220, 115, font7, green)
        elsif where2 = 6 then
            ans2 := ans2 + 60
            Font.Draw ("6", 330, 115, font7, black)
            Font.Draw ("O", 330, 115, font7, green)
        elsif where2 = 7 then
            ans2 := ans2 + 700
            Font.Draw ("7", 105, 30, font7, black)
            Font.Draw ("O", 105, 30, font7, green)
        elsif where2 = 8 then
            ans2 := ans2 + 800
            Font.Draw ("8", 220, 30, font7, black)
            Font.Draw ("O", 220, 30, font7, green)
        elsif where2 = 9 then
            ans2 := ans2 + 900
            Font.Draw ("9", 330, 30, font7, black)
            Font.Draw ("O", 330, 30, font7, green)
        end if

    end loop
end player_x
player_x


-----------------------------------
McKenzie
Tue Nov 09, 2004 1:57 pm


-----------------------------------
The way you are keeping track of who has taken a spot is odd.  Currently you are implementing them as counters. What you want instead is to remember who has taken a spot. You could use numbers for this if you like. e.g. 0=empty 1=X 2=O
so when X takes the spot use :
ans1 := 1

make sure also that before you let someone take a spot that it is empty e.g.
if where1 = 1 and ans1 = 0 then 

note: Your style needs some work. Although this program is best done with 2-Dim arrays you would be in better shape if you chose better variable names.

-----------------------------------
myob
Tue Nov 09, 2004 8:08 pm


-----------------------------------
this is what i would do if im going to make a tic tac toe game, i know its not all error trapped and stuff but it gives u an general idea of how things are traced and etc. when u want to trace which block has which, just make an int variable for that block, and if its blank, value = 0, if has x, value = 1, if has o, value = 2. then it is easy to trace and u can even make the computer to automatically determine the winner.

View.Set ("graphics:700;401")
var counter, x, y, button, btnNumber, btnUpDown : int := 0
for i : 1 .. 4
    Draw.Line (100 * i, 100, 100 * i, 400, 1)
    Draw.Line (100, 100 * i, 400, 100 * i, 1)
end for
for i : 3 .. 11
    locate (20, 4)
    if i mod 2 = 1 then
        put "Player One's Turn"
    else
        put "Player Two's Turn"
    end if
    Mouse.ButtonWait ("down", x, y, btnNumber, btnUpDown)
    locatexy (x, y)
    if counter mod 2 = 1 then
        put "x" ..
    else
        put "o" ..
    end if
end loop


-----------------------------------
enjoy
Tue Nov 09, 2004 10:52 pm

thanx
-----------------------------------
thanx for ur suggestions.... i really appretiate it :D 

could some on plz tell me how i can make it more effeicient... may be by using 2-d array.... and i also have to make it so that the user can play with the computer (different levels)

??? :?:

-----------------------------------
Cervantes
Wed Nov 10, 2004 4:00 pm


-----------------------------------
If you want to use 2D arrays, [url=http://www.compsci.ca/v2/viewtopic.php?t=366]learn to use arrays here or [url=http://www.compsci.ca/v2/viewtopic.php?t=1117]here.

-----------------------------------
Mr. Glib
Wed Nov 10, 2004 6:00 pm


-----------------------------------
enjoy, why not use a 2-d array to keep track of player moves? Set a 2-d array (3 row, 3 cols) to a bunch of zeros. If the user selects a spot, that position's value in your array is set to one (or some other value). You'll have to check with some sort of if block to ensure that only valid moves are made. If you want to keep track of who wins why not use a magic square? 

7 | 2 | 3
0 | 4 | 8
5 | 6 | 1

If the player's total = 12 then they won (be careful, you'll have to check to see if there is a winning combo in each row, col and diagonal.

-----------------------------------
enjoy
Wed Nov 10, 2004 6:13 pm

thanx Mr. Glib
-----------------------------------
hey Mr. Glib.... could u please make a small coding that would do that for plz... cuz i don't know where to start form!!


and guys i wanna use the mouse.where code but i have never used it before so could u guys plz show me how to do it....

thanx alot
i really appretiate ur help :lol:

-----------------------------------
Cervantes
Wed Nov 10, 2004 6:34 pm


-----------------------------------
well, you could mosie on over to the [url=http://www.compsci.ca/v2/viewforum.php?f=3]Turing Tutorials section and read a [url=http://www.compsci.ca/v2/viewtopic.php?t=6]Mouse.Where Tutorial.

-----------------------------------
myob
Wed Nov 10, 2004 6:42 pm


-----------------------------------
or, just do what i said earlier. set value of the array to zero if nothing in it, set it to 1 if player 1, set it to 2 if player 2. so like this box

1|2|1
2|1|2
2|1|1

i wrote down the codes to check whoever wins the game, anymore codes from me basically will mean that i made the game for you  :lol: 

var status : array 1 .. 3, 1 .. 3 of int %this is the 2-D array
var row, col, dia : int := 0 %this is the variables to keep track of where it forms a line
%following for loop sets all elements of the array "status" to zero
for i : 1 .. 3
    for j : 1 .. 3
        status (i, j) := 0
    end for
end for
%following for loop checks if any rows/columns/diagonals have same thing
%since we set it so the value of the array "status" equals 0 if it has nothing, equals 1 if player 1 played in it, and
%equals 2 if player 2 played in it.
%therefore, if any row has been played by the same player, it would add up to 6 or 3, which are both divisible by 3.
%when you check it, if the sum of rows mod 3 equals 0, that means it is the winning row.
%also check needed to check if any zero is in the row, if a row has 0|1|2, the sum of all that still divisible by 3 yet it's not a winning row.
for i : 1 .. 3
    if (status (1, i) + status (2, i) + status (3, i)) mod 3 = 0 and status (1, i) not= 0 and status (2, i) not= 0 then
        col := i %so now col = i = (which column wins the game)
    elsif (status (i, 1) + status (i, 2) + status (i, 3)) mod 3 = 0 and status (i, 1) not= 0 and status (i, 2) not= 0 then
        row := i %so now row = i = (which row wins the game)
    end if
    if i = 2 then
        if (status (i + 1, i + 1) + status (i, i) + status (i - 1, i - 1)) mod 3 = 0 and status (i + 1, i + 1) not= 0 and status (i, i) not= 0 then
            dia := 1 %so the diagonal from top-left to bottom-right wins the game
        elsif (status (i + 1, i - 1) + status (i, i) + status (i - 1, i + 1)) mod 3 = 0 and status (i + 1, i - 1) not= 0 and status (i, i) not= 0 then
            dia := 2 %so the diagonal from top-right to bottom-left wins the game
        end if
    end if
end for
%if you want check for which player wins the game, simply use an "if" command
if col not= 0 then
    put "Player ", status (1, col), " wins"
elsif row not= 0 then
    put "Player ", status (row, 1), " wins"
elsif dia not= 0 then
    put "Player ", status (2, 2), " wins"
else
    put "Draw"
end if


-----------------------------------
Mr. Glib
Wed Nov 10, 2004 7:20 pm


-----------------------------------
Looks like I was beaten to the punch! :) 

To check for a winning move, you'll have to create a 2-D array for the magic sqaure grid I mentioned above:
 
var magicSq : array 1..3, 1..3 of int
magicSq(1,1) := %insert # here
magicSq(1,2) := %insert # here
%and so on..


Create 2 more 2-d arrays to keep track of each player's magic square #s:

var p1magicSq : array 1..3, 1..3 of int  %set all these spots to zero using the method outlined in the next example.


here's a sample array to keep track of spots on the grid that are taken:

var taken : array 1..3, 1..3 of int 
%load array with zeros
for i: 1..3
 for j: 1..3
  taken(i,j):=0
 end for
end for


Now if some chooses one of the spots in your game:
 
if where1 = 1 then
  row := 1
  col := 1
  if taken(row, col) = 0 then
    taken(row, col) := 1 %spot is now taken
    p1magicSq(row, col) := magicSq(row,col)
  else
    put "try again!"
  end if
end if


To find a winner:

if (p1magicSq(1,1) + p1magicSq(1,2) + p1magicSq(1,3) = 12 then
  put "player 1 wins! woot!"
end if
%Note: you will have to do this check for each row, all the columns and the 2 diagonals.


-----------------------------------
eNc
Wed Nov 10, 2004 8:09 pm

Re: School Project.... PLEASE help
-----------------------------------
i did this much... but the program is not working the way i wanted to work!!!  .... could any please help me :lol: ??

its a tic tac toe game but not working properly
this is the code!



var font7 : int := Font.New ("Times:50")
var where1, where2 : int
var ans1, ans2 : int := 0
var counter : int := -1

drawfillbox (0, 0, 480, 275, black)
color (white)
colorback (black)
put " PLAYER 1!" : 70 ..
put "PLAYER 2!"
drawfillbox (180, 270, 175, 10, white)
drawfillbox (300, 270, 305, 10, white)
drawfillbox (75, 190, 405, 185, white)
drawfillbox (75, 85, 405, 90, white)
drawbox (75, 10, 405, 270, red)
for a : 0 .. 35
    drawline (177, 270, 177, 10, a)
    drawline (178, 270, 178, 10, a)
    drawline (302, 270, 302, 10, a)
    drawline (303, 270, 303, 10, a)
    drawline (75, 187, 405, 187, a)
    drawline (75, 188, 405, 188, a)
    drawline (75, 87, 405, 87, a)
    drawline (75, 88, 405, 88, a)
end for
Font.Draw ("1", 105, 210, font7, white)
Font.Draw ("2", 225, 210, font7, white)
Font.Draw ("3", 330, 210, font7, white)
Font.Draw ("4", 105, 115, font7, white)
Font.Draw ("5", 220, 115, font7, white)
Font.Draw ("6", 330, 115, font7, white)
Font.Draw ("7", 105, 30, font7, white)
Font.Draw ("8", 220, 30, font7, white)
Font.Draw ("9", 330, 30, font7, white)

procedure player_x
    loop
        counter := counter + 1
        locate (3, 1)
        exit when counter = 9 or ans1 = 6 or ans1 = 150 or ans1 = 2400
            or ans1 = 741 or ans1 = 852 or ans1 = 963 or ans1 = 951 or
            ans1 = 753 or ans2 = 6 or ans2 = 150 or ans2 = 2400
            or ans2 = 741 or ans2 = 852 or ans2 = 963 or ans2 = 951 or
            ans2 = 753
        get where1

        if where1 = 1 then
            ans1 := ans1 + 1
            Font.Draw ("1", 105, 210, font7, black)
            Font.Draw ("X", 105, 210, font7, green)
        elsif where1 = 2 then
            ans1 := ans1 + 2
            Font.Draw ("2", 225, 210, font7, black)
            Font.Draw ("X", 225, 210, font7, green)
        elsif where1 = 3 then
            ans1 := ans1 + 3
            Font.Draw ("3", 330, 210, font7, black)
            Font.Draw ("X", 330, 210, font7, green)
        elsif where1 = 4 then
            ans1 := ans1 + 40
            Font.Draw ("4", 105, 115, font7, black)
            Font.Draw ("X", 105, 115, font7, green)
        elsif where1 = 5 then
            ans1 := ans1 + 50
            Font.Draw ("5", 220, 115, font7, black)
            Font.Draw ("X", 220, 115, font7, green)
        elsif where1 = 6 then
            ans1 := ans1 + 60
            Font.Draw ("6", 330, 115, font7, black)
            Font.Draw ("X", 330, 115, font7, green)
        elsif where1 = 7 then
            ans1 := ans1 + 700
            Font.Draw ("7", 105, 30, font7, black)
            Font.Draw ("X", 105, 30, font7, green)
        elsif where1 = 8 then
            ans1 := ans1 + 800
            Font.Draw ("8", 220, 30, font7, black)
            Font.Draw ("X", 220, 30, font7, green)
        elsif where1 = 9 then
            ans1 := ans1 + 900
            Font.Draw ("9", 330, 30, font7, black)
            Font.Draw ("X", 330, 30, font7, green)
        end if

        get where2
        if where2 = 1 then
            ans2 := ans2 + 1
            Font.Draw ("1", 105, 210, font7, black)
            Font.Draw ("O", 105, 210, font7, green)
        elsif where2 = 2 then
            ans2 := ans2 + 2
            Font.Draw ("2", 225, 210, font7, black)
            Font.Draw ("O", 225, 210, font7, green)
        elsif where2 = 3 then
            ans2 := ans2 + 3
            Font.Draw ("3", 330, 210, font7, black)
            Font.Draw ("O", 330, 210, font7, green)
        elsif where2 = 4 then
            ans2 := ans2 + 40
            Font.Draw ("4", 105, 115, font7, black)
            Font.Draw ("O", 105, 115, font7, green)
        elsif where2 = 5 then
            ans2 := ans2 + 50
            Font.Draw ("5", 220, 115, font7, black)
            Font.Draw ("O", 220, 115, font7, green)
        elsif where2 = 6 then
            ans2 := ans2 + 60
            Font.Draw ("6", 330, 115, font7, black)
            Font.Draw ("O", 330, 115, font7, green)
        elsif where2 = 7 then
            ans2 := ans2 + 700
            Font.Draw ("7", 105, 30, font7, black)
            Font.Draw ("O", 105, 30, font7, green)
        elsif where2 = 8 then
            ans2 := ans2 + 800
            Font.Draw ("8", 220, 30, font7, black)
            Font.Draw ("O", 220, 30, font7, green)
        elsif where2 = 9 then
            ans2 := ans2 + 900
            Font.Draw ("9", 330, 30, font7, black)
            Font.Draw ("O", 330, 30, font7, green)
        end if

    end loop
end player_x
player_x



LOL omg I know you go to NOrth Toronto CI, and Mr. Small is your teacher and your in gr 10... LOL

-----------------------------------
myob
Wed Nov 10, 2004 10:56 pm


-----------------------------------
Looks like I was beaten to the punch! :) 

To check for a winning move, you'll have to create a 2-D array for the magic sqaure grid I mentioned above:
 
var magicSq : array 1..3, 1..3 of int
magicSq(1,1) := %insert # here
magicSq(1,2) := %insert # here
%and so on..


Create 2 more 2-d arrays to keep track of each player's magic square #s:

var p1magicSq : array 1..3, 1..3 of int  %set all these spots to zero using the method outlined in the next example.


here's a sample array to keep track of spots on the grid that are taken:

var taken : array 1..3, 1..3 of int 
%load array with zeros
for i: 1..3
 for j: 1..3
  taken(i,j):=0
 end for
end for


Now if some chooses one of the spots in your game:
 
if where1 = 1 then
  row := 1
  col := 1
  if taken(row, col) = 0 then
    taken(row, col) := 1 %spot is now taken
    p1magicSq(row, col) := magicSq(row,col)
  else
    put "try again!"
  end if
end if


To find a winner:

if (p1magicSq(1,1) + p1magicSq(1,2) + p1magicSq(1,3) = 12 then
  put "player 1 wins! woot!"
end if
%Note: you will have to do this check for each row, all the columns and the 2 diagonals.


to my best understanding, we have almost the same concept except u use magic squares and i use 0,1,2.  :lol:

-----------------------------------
enjoy
Thu Nov 11, 2004 3:42 pm


-----------------------------------
thanx guys.... but there is a small problem!!

myob... u know ur code always indecates that it is  a tie game...

Mr. Glib... urs i don't even know how to use ... i don't know wat is happening there...

eNc... very funny  :lol:

-----------------------------------
enjoy
Thu Nov 11, 2004 3:44 pm


-----------------------------------
Looks like I was beaten to the punch!  

To check for a winning move, you'll have to create a 2-D array for the magic sqaure grid I mentioned above: 
Code: 
  
var magicSq : array 1..3, 1..3 of int 
magicSq(1,1) := %insert # here 
magicSq(1,2) := %insert # here 
%and so on.. 


i don't know wat numbers are u talking about??

-----------------------------------
enjoy
Fri Nov 12, 2004 7:38 pm

no one??
-----------------------------------
where are u all ... no one has written anything here... i need some help here guys :shock: .... i will be waiting for your replies!! :roll:

-----------------------------------
Mr. Glib
Fri Nov 12, 2004 11:18 pm


-----------------------------------
I think you should check out myob's response...it looks like a good solution to your tic tac toe game.

-----------------------------------
enjoy
Thu Nov 18, 2004 9:00 pm

yahooooo!!!
-----------------------------------
now i am done with the 2 player program (where one user plays against another)

thanx for ur help!!!!


now i need to make a  program that has three levels where a user will play against the computer..... and it is due really soon ... so could u guys give me some ideas on how to do that....

and then the final and the altermate program is a 3D Tic Tac Toe :D .... 


but for now coud u guys give me some ideas about the user agains the computer program!!!

thanx guys
