
-----------------------------------
yan
Thu May 27, 2004 7:49 pm

tic tac toe game
-----------------------------------
:cry: 
Hi
I really really need help here. I have to create a tic tac toe game in which users are allowed to take turns only after they answered the questions correctly. I need help in placing the markers ("x" and "O") in the box where the mouse is clicked.  How do I do that?

The questions and answers, I had used a procedure in another program to get them and then saved it to a file. I opened the file in a new program but can't get the questions and answers out. How do I randomize them after I get them as well?

-----------------------------------
Paul
Thu May 27, 2004 8:04 pm


-----------------------------------
use Mouse.Where for the clicking part, use F9 on it if you don't know how to use it.
As for randomizing, put questions in an array then randomize the array subscript

-----------------------------------
guruguru
Thu May 27, 2004 8:05 pm


-----------------------------------
Well, the answer is a lot of nested if statements if they are clicking on where they want to go. If they type where they want to go, it is easier. But, clicking it is! Assuming boxes are 50 x 50.


var x, y, b : int

var boxNumberSelected : array 1 .. 9 of boolean := init (false, false, false, false, false, false, false, false, false)

loop
    mousewhere (x, y, b)

    if b = 1 then
        if y < 50 then
            if x > 100 then
                boxNumberSelected (9) := true
            elsif x > 50 then
                boxNumberSelected (8) := true
            elsif x > 0 then
                boxNumberSelected (7) := true
            end if
        elsif y < 100 then
            if x > 100 then
                boxNumberSelected (6) := true
            elsif x > 50 then
                boxNumberSelected (5) := true
            elsif x > 0 then
                boxNumberSelected (4) := true
            end if
        elsif y < 150 then
            if x > 100 then
                boxNumberSelected (3) := true
            elsif x > 50 then
                boxNumberSelected (2) := true
            elsif x > 0 then
                boxNumberSelected (1) := true
            end if
        end if
    end if

    % draw your stuff here
end loop


Heh, I made my own tic-tac-toe game while making this :D. YOu can adjust values. This fits a 150 x 150 board.

EDIT: For the second part of post...

There is a great example in the file i/o portion of the Turing documentations regarding getting stuff from the file. Regarding randomization, make an array of booleans that corrrespond to questions. Choose a random number, and do the questions associated with that boolean.

-----------------------------------
yan
Thu May 27, 2004 8:17 pm


-----------------------------------
hm.. how come its not drawing in teh position i want it tho?

-----------------------------------
guruguru
Thu May 27, 2004 8:21 pm


-----------------------------------
Umm... i dont know. Some code would be helpful. Make sure you coordinates are correct. Remember: maxy is the top of the screen!
I'll post my code for drawing one box...


procedure drawCircles
    for i : 1 .. 9
        if boxNumberSelected (i) then
            if i = 1 then
                Draw.Oval (25, maxy - 25, 10, 10, black)
            % rest of boxes
            end if
        end if
    end for
end drawCircles


-----------------------------------
yan
Thu May 27, 2004 8:54 pm


-----------------------------------
i was just wondering.. does different versions of turing use different types of codes? we dunt use Draw.Oval.. we use drawoval and drawfilloval   Draw.Oval does not work.

-----------------------------------
Cervantes
Thu May 27, 2004 9:01 pm


-----------------------------------
try to stay on topic here. :eh:

guru, you don't have to use so many if statements.  thats a very boring and inefficient way of doing it.  check this code out.


var mx, my, btnNumber, btnUpDown : int
const gridwidth := 3
const gridheight := 3
const gridwidthsize := maxx div gridwidth
const gridheightsize := maxy div gridheight
var grid : array 0 .. gridwidth, 0 .. gridheight of int
for x : 0 .. gridwidth
    for y : 0 .. gridheight
        grid (x, y) := -1
    end for
end for

var turn := 0
var gx, gy : int %grid x, grid y --> the picked positions

for x : 1 .. gridwidth
    drawline (x * gridwidthsize, 0, x * gridwidthsize, maxy, black)
end for
for y : 1 .. gridheight
    drawline (0, y * gridheightsize, maxx, y * gridheightsize, black)
end for

var font := Font.New ("Arial:18")

loop

    Mouse.ButtonWait ("down", mx, my, btnNumber, btnUpDown)
    gx := (mx div gridwidthsize)
    gy := (my div gridheightsize)
    if grid (gx, gy) = -1 then
        grid (gx, gy) := turn mod 2
        turn += 1
    end if


    for x : 0 .. gridwidth
        for y : 0 .. gridheight
            if grid (x, y) not= -1 then
                if grid (x, y) = 0 then
                    Draw.Text ("X", (x * gridwidthsize) + (gridwidthsize div 2), (y * gridheightsize) + (gridheightsize div 2), font, red)
                elsif grid (x, y) = 1 then
                    Draw.Text ("O", (x * gridwidthsize) + (gridwidthsize div 2), (y * gridheightsize) + (gridheightsize div 2), font, green)
                end if
            end if
        end for
    end for


end loop


all that's left after that is victory checking :)

-----------------------------------
guruguru
Thu May 27, 2004 9:16 pm


-----------------------------------
Wow. I congratulate you *shakes hand*.

But (muhaha) I spot inefficiencies! When drawing the lines, you draw 3 lines when you need draw only one. Wait nevermind... I realized you were just drawing two lines... I thought it was drawing each portion of the line (length of gridwidthsize) but I caught myself.

Anyways... it was a spur of the moment thing and I may not be so quick of though as Cervantes  :wink: .

-----------------------------------
yan
Fri May 28, 2004 9:42 am


-----------------------------------
hm.... thanks for everyone's help... my school's version of turing is turing for windows 4.0.4c..... this is wat i got so far... 
var player1, player2 : string
var n : int
var choice : array 1 .. 50 of string
procedure x
    drawline (20, 200, 70, 260, 0)
    drawline (22, 202, 72, 262, 0)
    drawline (24, 204, 74, 264, 0)
    drawline (26, 206, 76, 266, 0)
    drawline (20, 260, 70, 200, 0)
    drawline (21, 261, 71, 201, 0)
    drawline (22, 262, 72, 202, 0)
    drawline (23, 263, 73, 203, 0)
end x

procedure o
    drawoval (200, 233, 35, 35, 0)
end o

put "Who wants to go first? X or O?"
get player1
drawfill (maxx, maxy, 1, 2)
if player1 = "x" or player1 = "X" then
    player2 := "O"
    %x
elsif
        player1 = "o" or player1 = "O" then
    player2 := "X"
    %o
else
    loop
        put "Try again. Please enter X or O."
        get player1
        exit when player1 = "x" or player1 = "X" or player1 = "o" or player1 = "O"
    end loop
    if player1 = "x" or player1 = "X" then
        player2 := "O"
        %x
    elsif
            player1 = "o" or player1 = "O" then
        player2 := "X"
        %o
    end if
end if
var file : int
/*
open : file, "questions and answers.txt", get
assert file not = o
for i : 1 .. 50
    get : file, choice (i)
end for

put "Enter the question you want to answer (1 to 50). " ..
get n
if choice (n) not= "xxx" then
    put choice (n)
    choice (n) := "xxx"
else
    put "The question was already answered. Choose another one. " ..
    loop
        get n
        exit when choice (n) not= "xxx"
    end loop
end if*/
%drawfill (maxx, maxy, 13, 2)
drawbox (100, 200, 150, 250, 10) %last row, first box
drawbox (150, 200, 200, 250, 10) %last row, middle box
drawbox (200, 200, 250, 250, 10) %last row,last box
drawbox (100, 250, 150, 300, 10) %second row, first box
drawbox (150, 250, 200, 300, 10) %second row, middle box
drawbox (200, 250, 250, 300, 10) %second row, last box
drawbox (100, 300, 150, 350, 10) %first row, first box
drawbox (150, 300, 200, 350, 10) %first row, second box
drawbox (200, 300, 250, 350, 10) %first row, last box


var test : array 1 .. 3, 1 .. 3 of string
var over : boolean := true
var whowon : string := "noone"
procedure check1
    var z : string
    for i : 1 .. 3
        if test (i, 1) = "x" or test (i, 1) = "X" or
                test (1, i) = "X" or test (1, i) = "x" then
            z := "X"
        else
            z := "Y"
        end if
    end for
    if test (1, 1) = "X" or test (1, 1) = "x" then
        z := "X"
    else
        z := "Y"
    end if
    if test (1, 3) = "x" or test (1, 3) = "X" then
        z := "X"
    else
        z := "Y"
    end if
end check1

procedure check (z : string)
    for i : 1 .. 3
        if test (i, 1) = z and test (i, 2) = z and test (i, 3) = z or
                test (1, i) = z and test (2, i) = z and test (3, i) = z then
            over := false
            whowon := z
        end if
    end for
    if test (1, 1) = z and test (2, 2) = z and test (3, 3) = z then
        over := false
        whowon := z

    end if
    if test (1, 3) = z and test (2, 2) = z and test (3, 1) = z then
        over := false
        whowon := z

    end if
end check
var turn : string
if player1 = "X" or player1 = "x"then
    turn := "X"
else
    turn := "O"
end if
procedure playing (turn : string)
    var a, y, buttonnumber, buttonupdown, buttons : int
    loop
        buttonwait ("down", a, y, buttonnumber, buttonupdown)
        colour (black)
        if turn = "X" then
            x
        else
            o
        end if
    end loop
end playing
playing (turn)
check1
var z : string
check (z)

-----------------------------------
guruguru
Fri May 28, 2004 3:27 pm


-----------------------------------
Looks ok... but its not tic tac toe yet :p. I like the snazzy "X". Try now to make it work with clicking in the certain areas and a x or o shows up.

A couple notes:

1) All variables should be declared at the top of the program.

2) All procedures should be declared immediately after all variable declarations... not in the middle of the program.

3) Your procedures need bettter names!!! Instead of "x" use "drawFancyX" or something of the sort.

4) At the end, when you "check (z)", z is a blank variable. None of the test agains it will work. You have to give z a value.

Hope that helps for organization!

-----------------------------------
yan
Fri May 28, 2004 5:56 pm


-----------------------------------
hm.... yea. that is a rough draft tho. so all the declarations and stuffs are not out place.. haha. i just added them in when i test them.. neways... any suggestions on improving my program? i still cant get it to put the x and o... when i click on the box.. wat do i have to do tho?the x and o have the coordinates of the entire screen already, how do i make that within the specific box?

-----------------------------------
Cervantes
Fri May 28, 2004 6:38 pm


-----------------------------------
why not use the turn += 1 and mod turn method that I used in my above code? its rather simple.  and to put it in a specific box, you can do like I did and use a 2D array; change the value of grid (mx div gridwidth, my div gridheight) to whatever you want.

-----------------------------------
yan
Fri May 28, 2004 8:43 pm


-----------------------------------
wats turn+=1
is it turn:=turn + 1? same thing? i havent really gotten down to modify the program to all the suggestions.. m trying them.. i mean.. putting all the suggestions in my program tho.. just didnt put it up
about the 50 questions that i have to generate randomly.. is it easier to store it in a separate file out of this program or enter them in this program and then randomly generate one???

-----------------------------------
guruguru
Fri May 28, 2004 9:45 pm


-----------------------------------
Yes. 

something += other 

is equivalent to 

something := something + other


Yes. Reading from files is simpler, saves code space, and is easy enough to generate a random question. There was a recent post on getting a random line in this forum... I'll look for it.

Edit: Ok got it. 

-----------------------------------
yan
Sat May 29, 2004 4:49 pm


-----------------------------------
hm..thanks...bt then i've decided to just type all teh questions.. its easier for me..haha. less thinking... neways.. 
it is just a test of my questions... i can generate them randomly but then i want them to appear only once.. which doesnt seem to be working.. the place where i put questions (quiz) := "xxx".. i wanted that question to become xxx so that it doesnt appear again.. bt then it doesnt work.. any suggestions?

for i : 1 .. 2
    randint (quiz, 1, 50)
    if questions (quiz) = "xxx" then
        loop
            randint (quiz, 1, 50)
            exit when questions (quiz) not= "xxx"
        end loop
    elsif questions (quiz) not= "xxx" then
    count:=count + 1
        put count ,". ",questions (quiz)
        put choice1 (quiz)
        put choice2 (quiz)
        put choice3 (quiz)
        put choice4 (quiz)

    end if

    questions (quiz) := "xxx"
    put ""
end for

-----------------------------------
guruguru
Sat May 29, 2004 7:00 pm


-----------------------------------

questions (quiz) := "xxx"


This should be in the second if statement. Then when you finish the question, it can never be called again.

And what does the questions array do? You have a choices array, but it has no relation to questions. It should be something like...


put questions(quiz)
for i : 1 .. 4
    put choices (quiz, i)
end for


... where choices is a 2d array of a total of 4 choices for each question.

-----------------------------------
yan
Mon May 31, 2004 5:30 pm


-----------------------------------
wat conditions should i put for that in the if statement then?n also.. i tried the drawing x  but it doesnt go in the position i want it to. it always goes a bit higher and to the right.

procedure board
    for i : 30 .. 220 by 70
        for j : 30 .. 170 by 70
            drawbox (i, j, i + 60, j + 60, 53)
        end for
    end for
end board
procedure draw (player : string, x, y : int)
    drawline (x, y + 20, x + 40, y + 80, green)
    drawline (x + 2, y + 22, x + 42, y + 81, green)
    drawline (x + 4, y + 24, x + 44, y + 82, green)
    drawline (x + 6, y + 26, x + 46, y + 83, green)
    drawline (x, y + 80, x + 40, y + 20, green)
    drawline (x + 1, y + 81, x + 41, y + 21, green)
    drawline (x + 2, y + 82, x + 42, y + 22, green)
    drawline (x + 3, y + 83, x + 43, y + 23, green)
end draw

procedure mouse (var x, y : int)
    var button : int
    loop %needs to be in a loop to keep on reading mouse data
        Mouse.Where (x, y, button) %code to find data on mouse
        %check to see if button is hit
        if button = 1 and x = 30 and y >= 30 and y 