Computer Science Canada

Someone -H-E-L-P--M-E- with tic tac toe games

Author:  SilverSprite [ Fri Jul 25, 2003 1:01 am ]
Post subject:  Someone -H-E-L-P--M-E- with tic tac toe games

I'm writing a tic tac toe program to calculate the total number of various games assuming that x starts first. Anyways my program must double count somewhere.. someone help???? I'll post both turing and c++ sources.

UNCOMMENT THE TURING COMMENTS TO SEE THE PROGRAM AT WORK
code:

var count := 0

var grid : array 1 .. 3, 1 .. 3 of int
for i : 1 .. 3
    for j : 1 .. 3
        grid (i, j) := 0
    end for
end for

procedure drawboard
    for i : 1 .. 3
        for j : 1 .. 3
            put grid (j, i), " " ..
        end for
        put ""
    end for
    put count
end drawboard

function check : boolean
    for j : 1 .. 2
        for i : 1 .. 3
            if grid (1, i) = j and grid (2, i) = j and grid (3, i) = j then
                result true
            end if
            if grid (i, 1) = j and grid (i, 2) = j and grid (i, 3) = j then
                result true
            end if
        end for
        if grid (1, 1) = j and grid (2, 2) = j and grid (3, 3) = j then
            result true
        elsif grid (3, 1) = j and grid (2, 2) = j and grid (1, 3) = j then
            result true
        end if
    end for
    result false
end check

function checkall : boolean
    for i : 1 .. 3
        for j : 1 .. 3
            if grid (j, i) = 0 then
                result false
            end if
        end for
    end for
    result true
end checkall

procedure recurse (k : int)
    put k
    delay (100)
    if check then
        count += 1
        return
    end if
    for i : 1 .. 3
        for j : 1 .. 3
            if grid (j, i) = 0 then
                if (k mod 2 = 1) then
                    grid (j, i) := 2

                    %drawboard
                    %delay (750)
                    %cls

                    recurse (k + 1)
                    grid (j, i) := 0

                    %drawboard
                    %delay (750)
                    %cls

                else
                    grid (j, i) := 1

                    %drawboard
                    %delay (750)
                    %cls

                    recurse (k + 1)
                    grid (j, i) := 0

                    %drawboard
                    % delay (750)
                    % cls

                end if
            end if
        end for
    end for
    if checkall then
        count += 1
    end if
end recurse

for i : 1 .. 3
    for j : 1 .. 3
        grid (j, i) := 1

        %drawboard
        %delay (750)
        %cls

        recurse (1)
        grid (j, i) := 0

        %drawboard
        %delay (750)
        %cls

    end for
end for
put count



I HAVENT GOTTEN CLEARSCREEN OR DELAY TO WORK ON C++ SO DONT UNCOMMENT
code:

#include <iostream>
using namespace std;

int count, grid[3][3];

/*void drawboard()
{
    for (int i=0; i<3; i++)
    {
        for (int k=0; j<3; j++)
            cout << grid [j][i]<< " ";
        clrscr();       
    }
    cout << count << endl;
}
void clrscr()
{
  HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  COORD coord = {0, 0};
  DWORD count;
 
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  GetConsoleScreenBufferInfo(hStdOut, &csbi);
 
  FillConsoleOutputCharacter(hStdOut, ' ', csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
 
  SetConsoleCursorPosition(hStdOut, coord);
}*/
bool check()
{
    for (int i=0; i<3; i++)
    {
        for (int j=0; j<3; j++)
            if (grid[0][i] == j && grid[1][i] == j && grid[2][i] == j) return true;
            else if (grid[i][0] == j && grid[i][1] == j && grid[i][2] == j) return true;
        if (grid[0][0]==j && grid[1][1]==j && grid[2][2]==j) return true;
        if (grid[2][0]==j && grid[1][1]==j && grid[0][2]==j) return true;
    }
    return false;
}
bool checkall()
{
    for (int i=0; i<3; i++)
        for (int j=0; j<3; j++)
            if (grid[j][i] == 0) return false;
    return true;
}
void recurse(int k)
{
    if (check()){count++; return;}
    for (int i=0; i<3; i++)
        for (int j=0; j<3; j++)
            if (k%2 == 1)
            {
                grid[j][i]=2;
               
//            drawboard();
//            sleep(600);
//            clrscr();
               
                recurse(k+1);
                grid[j][i]=0;
               
//            drawboard();
//            sleep(600);
//            clrscr();
            }
            else
            {
                grid[j][i]=1;
               
//            drawboard();
//            sleep(600);
//            clrscr();
               
                recurse(k+1);
                grid[j][i]=0;
               
//            drawboard();
//            sleep(600);
//            clrscr();
            }
    if (checkall()) count++;
}

int main()
{
    for (int i=0; i<3; i++)
        for (int j=0; j<3; j++)
        {
            grid[j][i]=1;
            
//          drawboard();
//            sleep(600);
//            clrscr();
            
            recurse(1);
            grid[j][i]=0;
            
//          drawboard();
//            sleep(600);
//            clrscr();
        }
    cout << count << endl;
}

Author:  JayLo [ Fri Jul 25, 2003 1:08 am ]
Post subject: 

there.

Author:  Dan [ Fri Jul 25, 2003 1:17 am ]
Post subject: 

moved to turing help

also thats not a great title for the post

lucky i am not asok b/c he whould have blown this post to hell and back agean Twisted Evil

Author:  SilverSprite [ Fri Jul 25, 2003 1:47 am ]
Post subject: 

Jay Lo wrote:
there.


Didn't you read my post?? I don't have trouble with making tic tac toe. I'm having trouble couting EVERY SINGLE POSSIBLE CAASE.

Author:  JayLo [ Fri Jul 25, 2003 12:02 pm ]
Post subject: 

sorry. my bad. Embarassed

Author:  SilverSprite [ Fri Jul 25, 2003 1:46 pm ]
Post subject: 

np btw dont i get bits for my excellent example of a depth frsit search (or the other one i always confuse them.)

Author:  JayLo [ Sun Jul 27, 2003 10:47 pm ]
Post subject: 

if you want 30 bits from me, just ask... geez... lol.

Author:  krishon [ Tue Jul 29, 2003 8:12 pm ]
Post subject: 

lol, wut ppl do for bits these days

Author:  SilverSprite [ Wed Jul 30, 2003 10:56 am ]
Post subject: 

i'm jk.. whoa

Author:  bugzpodder [ Thu Jul 31, 2003 12:25 pm ]
Post subject: 

i sent you my proggie back a few weeks ago. do you want bits that badly? here, i gave you all my bits, plus darkness still owns me 500 so i'll give it to you once i get it

Author:  rizzix [ Fri Aug 01, 2003 12:00 am ]
Post subject: 

500!!! even mods don't give that much!

Author:  SilverSprite [ Fri Aug 01, 2003 1:12 pm ]
Post subject: 

yeah ok bugz.. heres all of mine too.. +1429

Author:  AsianSensation [ Fri Aug 01, 2003 6:48 pm ]
Post subject: 

btw, speaking of giving bits...

I still own SilverSprite and Bugz bits for math questions

so here is 5 for SilverSprite
+5 bits to SilverSprite

and 40 for Bugz
+40 bits to Bugz

Author:  SilverSprite [ Fri Aug 01, 2003 8:51 pm ]
Post subject: 

why do you owe me bits?

Author:  AsianSensation [ Sat Aug 02, 2003 9:00 am ]
Post subject: 

check the "Math Solution" thread, I own you guys bits, I don't even know why, but oh well...


: