Tic Tac Toe, find the winner

So I have a tic tac toe program in the works. My board is a single dimensional array like so:

char board[9] = {'1','2','3','4','5','6','7','8','9'};

It is displayed like"

1
2
3
4
5
cout << board[0] << " | " << board[1] << " | " << board[2] << endl;
cout << "---------" << endl;
cout << board[3] << " | " << board[4] << " | " << board[5] << endl;
cout << "---------" << endl;
cout << board[6] << " | " << board[7] << " | " << board[8] << endl;


Now, I'm having an issue with figuring out if a player has got 3 in a row somewhere. If someone could point my brain in the right direction here, that would be great. I think if I switched my array to 2D, it would be easier. But, ive also read that multi dimensional arrays are no bueno, so im sticking with 1D. I'm sure there's a way to check it
Last edited on
There is... You'll have to put a condition for all possible combinations of 3 in a row. Like
Horizontal: {1, 2, 3}, {4, 5, 6}, {7, 8, 9}
Vertical: {1, 4, 7}, {2, 5, 8}, {3, 6, 9}
Diagonal: {1, 5, 9}, {3, 5, 7}.

Yea that's what I ended up doing, it was pretty lengthy. I didn't know if there was some cool algorithm that would could figure it out.
Actually you can create one for a general grid. For 3x3, its not really necessary, its easier to just give the conditions explicitly...

I made this a while back, I used loops to check the vertical and horizontal and simple if statements for the diagonal:

g->getGrid is just the 3x3 array I used for my board.

I used a char array, so 79 is 'O' and 88 is 'X' in the Ascii table:
http://www.asciitable.com/

And if '99' gets returned, it tells the calling function that it was a draw.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
char winCheck( Game *g )
{
    int gridCount = 0;
    

    for( int i = 0; i < 3; i++ )
    {
        if( ( g->getGrid( i, 0 ) == 88 ) &&  ( g->getGrid( i, 1 ) == 88 ) &&  ( g->getGrid( i, 2 ) == 88 ) )
        {
            return 88;
        }
        

        if( ( g->getGrid( i, 0 ) == 79 ) &&  ( g->getGrid( i, 1 ) == 79 ) &&  ( g->getGrid( i, 2 ) == 79 ) )
        {
            return 79;
        }
    }
    

    for( int i = 0; i < 3; i++ )
    {
        if( ( g->getGrid( 0, i ) == 88 ) &&  ( g->getGrid( 1, i ) == 88 ) &&  ( g->getGrid( 2, i ) == 88 ) )
        {
            return 88;
        }
        

        if( ( g->getGrid( 0, i ) == 79 ) &&  ( g->getGrid( 1, i ) == 79 ) &&  ( g->getGrid( 2, i ) == 79 ) )
        {
            return 79;
        }
    }
    

    if( ( g->getGrid( 0, 0 ) == 88 ) &&  ( g->getGrid( 1, 1 ) == 88 ) &&  ( g->getGrid( 2, 2 ) == 88 ) )
        return 88;
    
    if( ( g->getGrid( 0, 0 ) == 79 ) &&  ( g->getGrid( 1, 1 ) == 79 ) &&  ( g->getGrid( 2, 2 ) == 79 ) )
        return 79;
        

    if( ( g->getGrid( 0, 2 ) == 88 ) &&  ( g->getGrid( 1, 1 ) == 88 ) &&  ( g->getGrid( 2, 0 ) == 88 ) )
        return 88;
        

    if( ( g->getGrid( 0, 2 ) == 79 ) &&  ( g->getGrid( 1, 1 ) == 79 ) &&  ( g->getGrid( 2, 0 ) == 79 ) )
        return 79;
        

    //check for a full grid( no win )
    for( int i = 0; i < 3; i++ )
    {
        for( int j = 0; j < 3; j++ )
        {
            if( g->getGrid( i, j ) != 43 )
            {
                gridCount++;
            }
            

            if( gridCount == 9 )
                return 99;
        }

    }

    return 0;
}


I found this much better than making an if statement for each check.
This looks like an algorithm for checking 2d array right? I'm using a 1D array. I read somewhere that using 2D arrays are no bueno.
That's not true... I don't think it makes much of a difference... And if you want an algorithm for 1D, you can make one... Like, here's one for checking horizontal wins, for a general n x n grid... a is the 1D array...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int wincheck=0, i, j;
char a[n*n];
bool win;

for (i=1; i<=n; i=i+n)
{
   for (j=i; j<(n*n); j++)
   {
      if (a[j]==a[j+1])
         wincheck++;
    }
    if (wincheck==(n-1))
    {
          win==true;
          break;
     }
}


You can make something similar for vertical checks.. and for diagonal, its best to have the conditions defined explicitly, as there are only two combos possible...
PS - Haven't compiled the code, so dunno about compiler errors... But the logic is sound for any general grid...
Last edited on
Oh yeah, sorry. For some reason I thought it was a 2D array!

And I used a 2D array when I made this. Skip to 1:10 on the video here:
http://sites.google.com/site/davevisone/home/cplusplus/tic---tac---toe

As it is on the screen, is how I used the array.
Last edited on
Topic archived. No new replies allowed.