Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Tic Tac Toe checking for win
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Insectoid




PostPosted: Thu Jul 31, 2008 4:58 pm   Post subject: Tic Tac Toe checking for win

I decided to make Tic Tac Toe as an excersize in 2-d arrays. Unfortunately, I have come across a problem.
In the bit where I check for a win, it doesn't always say you won right away, but waits until a turn or to later when it realizes, hey! Somebody won! and does the thing. Sometimes it never says you've won.
All three rows and the top column (3 columns and top row?) are coded in, so don't bother with the other ones. Help?

~Insectoid



TicTacToe.zip
 Description:
Tic Tac Toe- WIP

Download
 Filename:  TicTacToe.zip
 Filesize:  17.12 KB
 Downloaded:  125 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Thu Jul 31, 2008 7:45 pm   Post subject: RE:Tic Tac Toe checking for win

Doing things manually is for chumps. Make the computer do the work for you. Consider replacing your large if statement with something like this:

Turing:

% in variable declaration section
var winColXCoords : array 1..3 of int := init ( 80, 200, 320 )

% In check_for_win:

    % Check for cat's game
    var cats : int := 0
    for row : 1 .. 3
        for column : 1 .. 3
            if placement (row) (column) > 0 then
                cats += 1
            end if
        end for
    end for
    if ( cats = 9 ) then
        newgame := true
        Pic.Draw (catsgame, 0, 100, picMerge)
        delay (1000)
        return
    end if


    % Check whether someone has a complete row
    for col : 1 .. 3
        if placement (col) (1) = placement (col) (2) and placement (col) (2) = placement (col) (3) and placement (col) (1) > 0 then
            Draw.ThickLine ( winColXCoords(col), 360, winColXCoords(col), 40, 11, brightred)
            newgame := true
            winner := placement (col)(1)
            delay (1000)
        end if
    end for


You can apply a similar strategy to determining whether a row wins or not. Diagonals will need to be manually coded, but shouldn't be hard.

Your program has attitude (scribbling motif) and I approve.
Insectoid




PostPosted: Thu Jul 31, 2008 7:59 pm   Post subject: RE:Tic Tac Toe checking for win

I always have wanted to do the scribbling thing, and I've found it's much harder to do than it looks (Scribbling letters on paper is easy, but on MS paint with a mouse it's a pain.)

Thank you! I knew there was a better way.

Of course, you seem to have misunderstood my use of the variable 'winner'. It is not to store which spot won, but which player won (1 for X, 2 for O)

And why do you have 'return' under the delay? It was my understanding that that is used for functions only.
~Insectoid
DemonWasp




PostPosted: Thu Jul 31, 2008 8:03 pm   Post subject: RE:Tic Tac Toe checking for win

Look more closely: that's what I have it storing.

Turing:

% You were doing
if ( a = 1 ) then
  winner = 1
else if ( a = 2 ) then
  winner = 2
end if

%I'm doing:

if ( a > 0 ) then % same as (a=1 or a=2), since the tile is either 0, 1, or 2
  winner = a
end if
Insectoid




PostPosted: Thu Jul 31, 2008 8:29 pm   Post subject: RE:Tic Tac Toe checking for win

D'oh!

Actually, I was doing this:

Turing:

winner := turn.player


'turn' is holding who's turn it is and the picture of that player. Whatever, both work.

Edit: I did that, as well as for the rows, and manually did the diagonals, and it works! Now I just need to find out how to make a min/Max AI/figure it out for myself!

Anywho, it is now 9:30 and south park is on, and I must go.

Goodbye!
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: