Computer Science Canada

Tic Tac Toe Help - Drawing X's and O's

Author:  S_Grimm [ Thu Sep 18, 2008 8:32 am ]
Post subject:  Tic Tac Toe Help - Drawing X's and O's

Ok, i'm working on a little tic tac toe game during spare, and i need to draw the x's and o's. i don't want to use a pile of if\else statements, but will if nessesarry. more or less, i would like to be able to click the "square" and have the x or o drawn there. Any ideas?
Thanks in advance
A\V

Author:  Ktomislav [ Thu Sep 18, 2008 8:51 am ]
Post subject:  Re: Tic Tac Toe Help - Drawing X's and O's

Something like this?

code:

View.Set ('offscreenonly')
var x1, y1, x2, y2: int
var x_mouse, y_mouse, button, buttonupdown: int
x1 := 100
x2 := 200
y1 := 100
y2 := 200
Draw.Box (x1, y1, x2, y2, black)

loop
    Mouse.Where (x_mouse, y_mouse, button) % or mousewhere
    if (button = 1) then
        Mouse.ButtonWait ("up", x_mouse, y_mouse, button, buttonupdown)
        Draw.Line (x1, y1, x2, y2, red)
        Draw.Line (x2, y1, x1, y2, red)
        View.Update
    end if
end loop

Author:  S_Grimm [ Thu Sep 18, 2008 12:39 pm ]
Post subject:  RE:Tic Tac Toe Help - Drawing X\'s and O\'s

something like it. but it needs to have a count so that i can check for win.

Author:  Insectoid [ Thu Sep 18, 2008 12:48 pm ]
Post subject:  RE:Tic Tac Toe Help - Drawing X\'s and O\'s

You need a 2-d array to keep track of what square has been claimed by which team. Then you use a combination of for loops an if statements to check if someone has won.

Author:  S_Grimm [ Tue Sep 23, 2008 10:32 am ]
Post subject:  RE:Tic Tac Toe Help - Drawing X\'s and O\'s

so and loop if square one and square 2 and square 3 are claimed by x then end loop sort of style?

Author:  Insectoid [ Tue Sep 23, 2008 11:58 am ]
Post subject:  RE:Tic Tac Toe Help - Drawing X\'s and O\'s

Uh, its a bit more complicated than that...

I suggest looking someone else's tic-tac-toe. (don't use mine, it's unreadable Smile)

Author:  Warchamp7 [ Tue Sep 23, 2008 12:03 pm ]
Post subject:  Re: RE:Tic Tac Toe Help - Drawing X\'s and O\'s

This is how I would end up doing it

If a player goes in a sqaure, set the BoxArray for that square to 1 or 2 depending on the player then have a check

BoxArray (1 to 9)
PlayerCheck

1 is for X
2 is for O

Game Grid
1 2 3
4 5 6
7 8 9

Turing:

%Short example. Basically have every possible way to win in the if statement

for XYZ : 1..2

PlayerCheck := XYZ
if (BoxArray(1) = PlayerCheck and BoxArray(2) = PlayerCheck and BoxArray(3) = PlayerCheck) or (BoxArray(1) = PlayerCheck and BoxArray(5) = PlayerCheck and BoxArray(9) = PlayerCheck) then
XWin := true
else
end if

end for

Author:  Insectoid [ Tue Sep 23, 2008 12:36 pm ]
Post subject:  RE:Tic Tac Toe Help - Drawing X\'s and O\'s

I'm fairly sure you need some brackets in that if statement, though I may be wrong.

Author:  Warchamp7 [ Wed Sep 24, 2008 8:21 am ]
Post subject:  Re: RE:Tic Tac Toe Help - Drawing X\'s and O\'s

insectoid @ Tue Sep 23, 2008 12:36 pm wrote:
I'm fairly sure you need some brackets in that if statement, though I may be wrong.


I did

Quote:
(BoxArray(1) = PlayerCheck and BoxArray(2) = PlayerCheck and BoxArray(3) = PlayerCheck) or (BoxArray(1) = PlayerCheck and BoxArray(5) = PlayerCheck and BoxArray(9) = PlayerCheck)

Author:  Insectoid [ Wed Sep 24, 2008 11:09 am ]
Post subject:  RE:Tic Tac Toe Help - Drawing X\'s and O\'s

Woops, stupid me. Didn't even see it.

Author:  gitoxa [ Wed Sep 24, 2008 9:49 pm ]
Post subject:  RE:Tic Tac Toe Help - Drawing X\'s and O\'s

Warchamp, what's the point of this line of code? Razz

code:
PlayerCheck := XYZ


And it's much better just to check to see if all the elements are equal to each other, less comparisons. Using your variables:
code:
if (BoxArray(1) = BoxArray(2) and BoxArray(2) = BoxArray(3))
or (BoxArray(1) = BoxArray(5) and BoxArray(5) = BoxArray(9)) then

Author:  Warchamp7 [ Thu Sep 25, 2008 7:49 am ]
Post subject:  Re: RE:Tic Tac Toe Help - Drawing X\'s and O\'s

gitoxa @ Wed Sep 24, 2008 9:49 pm wrote:
Warchamp, what's the point of this line of code? Razz

code:
PlayerCheck := XYZ


And it's much better just to check to see if all the elements are equal to each other, less comparisons. Using your variables:
code:
if (BoxArray(1) = BoxArray(2) and BoxArray(2) = BoxArray(3))
or (BoxArray(1) = BoxArray(5) and BoxArray(5) = BoxArray(9)) then


For the player check, it's just a quick for loop to check player 1 and player 2 with one group of code

As for your method, a nice idea Smile

Author:  S_Grimm [ Thu Sep 25, 2008 8:33 am ]
Post subject:  RE:Tic Tac Toe Help - Drawing X\'s and O\'s

ok thanks guys. I think that gitoxa has the right idea. Now, the only way that i can draw an x or o inside the grid when a mouse button is clicked is to use Mouse.Where and a series of if statements, right?
Here's The Example:
If mousex >= 0 and mousex <= 150 and mousey >= 0 and mousey <= 150 and mousebutton = 1 then
draw x
else .
end if

Author:  gitoxa [ Thu Sep 25, 2008 4:29 pm ]
Post subject:  RE:Tic Tac Toe Help - Drawing X\'s and O\'s

Warchamp, I just meant there's no point in assigning a variable to equal XYZ when you can just use XYZ instead.

And A/V, that's one way to do it. The way I did it wa I used a 2d array for the squares, and div'd the MouseX and MouseY values by the size of the squares (with some other manipulation just to give me the right numbers). That ways probably a little more difficult to incoporate correctly, but it allowed to reduce it down to one if statement, and two number manipulations.

Author:  S_Grimm [ Fri Sep 26, 2008 7:27 am ]
Post subject:  RE:Tic Tac Toe Help - Drawing X\'s and O\'s

ok thanks guys.

Author:  Zeroth [ Fri Sep 26, 2008 11:17 am ]
Post subject:  Re: RE:Tic Tac Toe Help - Drawing X\'s and O\'s

gitoxa @ Thu Sep 25, 2008 1:29 pm wrote:
Warchamp, I just meant there's no point in assigning a variable to equal XYZ when you can just use XYZ instead.

Yes there is. Its called Magic Numbers. Why are we comparing to XYZ? What does XYZ signify? Its a bad, bad, bad habit to get into, and can really slow down maintenance in larger programs. Really, you should be encouraging the behaviour of Warchamp, not smacking him down for it. From personal experience, magic numbers are the most insidious, nasty, confusing thing to deal with in 6-month old, 1 year old code.

Author:  gitoxa [ Fri Sep 26, 2008 11:27 am ]
Post subject:  RE:Tic Tac Toe Help - Drawing X\'s and O\'s

Why is he using XYZ in the first place?
In a different context I could understand him doing that, but not this one.

Author:  Clayton [ Fri Sep 26, 2008 1:17 pm ]
Post subject:  RE:Tic Tac Toe Help - Drawing X\'s and O\'s

I'm going to tend to agree with gitoxa here, why have:

Turing:

for XYZ : 1 .. 2
    PlayerCheck = XYZ
    if foo(1) == PlayerCheck && ...
end for


As opposed to:

Turing:

for PlayerCheck : 1 .. 2
    if foo(1) == PlayerCheck && ...


?

Author:  S_Grimm [ Tue Sep 30, 2008 11:29 am ]
Post subject:  RE:Tic Tac Toe Help - Drawing X\'s and O\'s

gitoxa's methond does seem simpler....... hmm... Ill try it and see what happens... I plan on having a rough code for you all to look at out by tommorow. i haven't really had time to work on it, between work and homework (that takes up most of my spare). if anyone else has any good ideas for it, let me know plz.


: