Tic tac toe, small problem.

Hello Everyone
I am new to the forum,i have a simple code, everything works and goes fine. i just got stuck on a small detail.
The program is called tic tac toe. I just got stuck in the tie part. i just dont know where i can put an For or While statement to check once my square(board) is filled with the inputs by the players.

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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <iostream>
#include <stdlib.h>


using namespace std;
const int ROWSIZE = 3;
const int COLSIZE = 3;
int main()

{
char square[ROWSIZE][COLSIZE] =
                    {
                     {'*','*','*'},
                     {'*','*','*'},
                     {'*','*','*'}
                    };

char turn = 'X';
int row,column;
char winner = ' ';

bool validmove;
bool validrow;
bool validcolumn;


while(winner == ' ')

{

cout << "       Columns"<< endl;
cout << "       1 2 3" << endl;



    for(int i=1;i<4;i++)
    {
    cout << "Row: " << i << " " << square[i-1][0] << " " << square[i-1][1] << " " << square[i-1][2] << " " << endl ;
    }

    for(int i=0;i<3;i++)
    {
        if(square[i][0] == square[i][1] && square[i][1] == square[i][2] && square[i][0] != '*')
            {
                winner = square[i][0];
            }

    }

    for(int i=0;i<3;i++)
    {
        if(square[0][i] == square[1][i] && square[1][i] == square[2][i] && square[0][i] != '*')
            {
                winner = square[0][i];
            }
    }
     if(square[0][0] == square[1][1] && square[1][1] == square[2][2] && square[0][0] != '*')
            {
                winner = square[0][0];
            }
     if(square[0][2] == square[1][1] && square[1][1] == square[2][0] && square[0][2] != '*')
            {
                winner = square[0][2];
            }
    if(square[0][0] == square[0][1] && square[0][1] == square[0][2] && square[0][2] == square[1][0] && square[1][0] == square[1][1] && square[1][1] == square[1][2] && square[1][2] == square[2][0] && square[2][0] == square[2][1] && square[2][1] == square[2][2] && square[0][0] != '*')
            {
winner = 't';
}




if (winner == 'X')
{
    cout << "Strike! Player X's WON. Congrats!" << endl;
    break;
}
if (winner == 'O')
{
    cout << "Strike! Player O's Won. Congrats!" << endl;
    break;
}
else if (winner == 't')
{
    cout << "Tie! Program exiting..." << endl;
    break;
}



    if(turn == 'X')
    {
        cout << "Player X's turn." << endl;
        cout << "Enter a row and column to place an X" << endl;

    }
    else
    {
        cout << "Player O's turn." << endl;
        cout << "Enter a row and column to place an O" << endl;
    }

validmove = false;
if (validmove ==  '*')
break;


while(!validmove)
{
validrow = false;
while(!validrow)
        {
            cout << "Row: ";
            cin >> row;

            if(row == 1 || row == 2 || row == 3)
                {
                    validrow = true;
                }
            else
                {
                    cout << endl << "Invalid row!" << endl;
                }
        }

validcolumn = false;
while(!validcolumn)
{
            cout << "Column: ";
            cin >> column;

            if(column == 1 || column == 2 || column == 3)
                {
                    validcolumn = true;
                }
            else
                {
                    cout << endl << "Invalid column!" << endl;
        }
}

    if(square[row-1][column-1] == '*')
    {
    square[row-1][column-1] = turn;
    validmove = true;
            if(turn == 'X')
            {
                turn = 'O';
            }
            else
            {
                turn = 'X';
            }
    }
    else
    {
    cout << "The selected square is occupied!\nSelect Again: " << endl;
    }
}
}

//return 0;
}

I once had trouble with this myself. You have this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
if (winner == 'X')
{
    cout << "Strike! Player X's WON. Congrats!" << endl;
    break;
}
if (winner == 'O')
{
    cout << "Strike! Player O's Won. Congrats!" << endl;
    break;
}
else if (winner == 't')
{
    cout << "Tie! Program exiting..." << endl;
    break;
}

This is what had helped me was using something along these lines:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
if (winner == 'X')
{
    cout << "Strike! Player X's WON. Congrats!" << endl;
    break;
}
else if (winner == 'O')
{
    cout << "Strike! Player O's Won. Congrats!" << endl;
    break;
}
else (winner == 't')
{
    cout << "Tie! Program exiting..." << endl;
    break;
}


Hope this helps. Good luck.
Topic archived. No new replies allowed.