Tic Tac Toe -- Determining Winner?

I'm unsure of how to determine a winner in Tic-Tac-Toe.. here is my code :

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include<iostream>
#include<cctype>
#include<iomanip>

using namespace std;
             void initialize(char seating[][3]);
             void questions(char& ticketType, int& row, 
             char& column);
             void printseating(char seating[][3], int row, char column);
             void printseating2(char seating[][3], int row, char column);
             int answer;

char type, column;
    int row;
    char seating[3][3];

int main(int argc, char *argv[])
{
 
    initialize(seating) ;
    
    answer = 1;
    answer = static_cast<char>(toupper(answer));

 cout << "       A B C" << endl;
      cout << " Row 1 * * *" << endl;
    cout << " Row 2 * * *" << endl;
    cout << " Row 3 * * *" << endl;
    
    while(answer == 1 || answer == 0)     
    {
        questions(type, row, column);
        printseating(seating, row, column);
        cout << '\n';


        cout << "Press 1 for first player's move. 0 for 2nd player's move. : " ;
        cin >> answer;
        answer = static_cast<char>(toupper(answer));
        if(answer == 3)
        return 0;
    }// end while  
    
      system("PAUSE");
    return EXIT_SUCCESS;
}


void initialize(char seating[][3])
{
     for(int i=0 ;i < 3 ;i++)
       for(int j=0 ;j<3 ;j++)
         seating[i][j]='*';
}



void questions(char& type, int& row, char& column)
{    
     
    type = static_cast<char>(toupper(type));
    cout << '\n';
                
    cout << "What Row Number would you like to mark? (1-3) : ";
    cin >> row;  
    
    cout << "What seat number would you like to mark? (A-C) : ";
    cin >> column;
    column = static_cast<char>(toupper(column));  
    
}    

void printseating(char seating[][3], int row, char column)
{
    int i, j;
    
    if(seating[row-1][static_cast<int>(column-65)]=='X')
   {cout << "This spot on the board is already taken. Choose another : " << '\n';
       cin >> column;
       column = static_cast<char>(toupper(column)); 
   } 
   if(answer==1)
    { 
    seating[row-1][static_cast<int>(column)-65]= 'X';
    
    
    cout << '\n';
    cout << "       A B C" << '\n';
         
    for(i = 0; i < 3; i++)
    {
       cout << left << setw(3) << "Row " << setw(2)<< i+1 ;
       for(j = 0; j < 3; j++)
       {
          cout << right  << " " << seating[i][j];
       }
       cout << '\n';
    }}
    
    
   if(answer==0)
   { if(seating[row-1][static_cast<int>(column-65)]=='O')
   {
       cout << "This spot on the board is already taken. Choose another : " << '\n';
       cin >> column;
       column = static_cast<char>(toupper(column));  
   }  
    seating[row-1][static_cast<int>(column)-65]= 'O';
    
    cout << '\n';
    cout << "       A B C" << '\n';
         
    for(i = 0; i < 3; i++)
    {
       cout << left << setw(3) << "Row " << setw(2)<< i+1 ;
       for(j = 0; j < 3; j++)
       {
          cout << right  << " " << seating[i][j];
       }
       cout << '\n';
    }}

}


The program works just fine, I just can't determine a winner.
Last edited on
That looks really overcomplicated, the way I would do it is make a two dimensional array, and check every row/column/diagnol line for 3 matching symbols, it looks like you've already made the two dimensional array so now all you need is a bunch of if statements, then add one to whoever won wins count, and reset the array.
@Zephilinox

couldn't have said it better myself.
I'm not really sure how I would do the if statements... I've tried and they never work.

I tried to do a function with this if statement :
1
2
3
if((row==1 && column == 'A')&&(row==1 && column == 'B')&&(row==1 && column == 'C'))
     {if(seating[row-1][static_cast<int>(column-65)]=='X')
     {cout << "hi" << endl;}}


and then do a bunch of them for different scenarios... but it doesn't work.
Last edited on
if (grid[0][0] == symbol && grid[0][1] == symbol && grid[0][2] == symbol) return 0;

thats cut out from my tic tac toe game, replace grid with your array name and remove return 0; if you decide not to put it in a function.
Thanks much! :) That helped. I created a lot... it was a lot of work, but I finally got it to work!!
Topic archived. No new replies allowed.