Impossible-To-Win Tic Tac Toe

Hi,
I am a beginner in c++ (been studying it for 2 and a half months) and I wrote this tic tac toe game. Even though it is unbeatable (at least I believe so), the code seems pretty sloppy and too long to me, so I am curious if someone experienced could tell me what are the examples of bad programming habits in this code, and how I could shrink it.
Thanks in advance :)

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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iomanip>

using namespace std;

bool isTaken(char[][3], int);
void pcTurn(char[][3]);
void playerTurn(char[][3]);
void redrawField(char[][3]);
char isWin(char[][3]);
bool attack(char[][3]);
bool defense(char[][3]);
void random(char[][3]);
bool tricks(char[][3]);

int check = 0;
int check2 = 0;
int check3 = 0;
int check4 = 0;

int main()
{
    char field[3][3] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
    srand(time(NULL));
    int moves = 0;

    do{
        redrawField(field);
        playerTurn(field);
        moves++;
        if(!isWin(field)&&moves<9){
            pcTurn(field);
            moves++;
        }
    }while(!isWin(field)&&moves<9);

    redrawField(field);

    if(!isWin(field)){
        cout << "\n\t\t\t\t  ";
        cout << "It's a tie!";
    }else{
        cout << "\n\t\t\t\t ";
        cout << "Player "<< isWin(field) << " won!";
    }

    system("pause>nul");

    return 0;
}

bool isTaken(char field[][3], int pos){

    int z = 1;

    for(int i=0; i<3; i++){
        for(int j=0; j<3; j++){
            if(z == pos){
                if(field[i][j] == 'X' || field[i][j] == 'O'){
                    return true;
                }else{
                    return false;
                }
            }
            z++;
        }
    }
}

void redrawField(char field[][3]){

    system("cls");

    cout << "\n\n\n\n\n\n";

    for(int i=0; i<3; i++){
        cout << "\t\t\t\t";
        for(int j=0; j<3; j++){
            cout << setw(4) << field[i][j];
        }
        cout << "\n\n";
    }
}

void playerTurn(char field[][3]){

    int choice, z=1;

    do{
        cout << "\n\n\t\t\t\t  ";
        cout << "Your turn: ";
        cin >> choice;
    }while(isTaken(field, choice));

    for(int i=0; i<3; i++){
        for(int j=0; j<3; j++){
            if(z == choice){
               field[i][j] =  'X';
            }
            z++;
        }
    }
}

void pcTurn(char field[][3]){
    if(!attack(field)){
        if(!defense(field)){
            if(!tricks(field)){
                random(field);
            }
        }
    }
}

bool attack(char field[][3]){

    bool xEx = false;

    char tempI, tempJ;
    int z = 0;

    for(int i=0; i<3; i++){

        for(int j=0; j<3; j++){

            if(field[i][j] == 'O'){
                z++;
            }else if(field[i][j] != 'O' && field[i][j] != 'X'){
                tempI = i;
                tempJ = j;
            }
        }
        if(z == 2 && field[tempI][tempJ] != 'O' && field[tempI][tempJ] != 'X' && i == tempI){
            field[tempI][tempJ] = 'O';
            return true;
        }
        z = 0;
    }


    for(int j=0; j<3; j++){
        for(int i=0; i<3; i++){

            if(field[i][j] == 'O'){
                z++;
            }else if(field[i][j] != 'O' && field[i][j] != 'X'){
                tempI = i;
                tempJ = j;
            }
        }
        if(z == 2 && field[tempI][tempJ] != 'O' && field[tempI][tempJ] != 'X' && j == tempJ){
            field[tempI][tempJ] = 'O';
            return true;
        }
        tempI = 0;
        tempJ = 0;
        z = 0;
    }

    for(int i=0; i<3; i++){
        for(int j=0; j<3; j++){

            if(i==j){
                if(field[i][j] == 'O'){
                    z++;
                }else if(field[i][j] != 'O' && field[i][j] != 'X'){
                    tempI = i;
                    tempJ = j;
                }
                if(field[i][j]=='X'){
                    xEx = true;
                }
            }
        }
    }

    if(z == 2 && field[tempI][tempJ] != 'O' && field[tempI][tempJ] != 'X'){
        field[tempI][tempJ] = 'O';
        return true;
    }
    tempI = 0;
    tempJ = 0;
    z = 0;
    xEx = false;

    for(int i=0; i<3; i++){
        for(int j=0; j<3; j++){

            if(i+j==2){
                if(field[i][j] == 'O'){
                    z++;
                }else if(field[i][j] != 'O' && field[i][j] != 'X'){
                    tempI = i;
                    tempJ = j;
                }
                if(field[i][j]=='X'){
                    xEx = true;
                }
            }
        }
    }

    if(z == 2 && field[tempI][tempJ] != 'O' && field[tempI][tempJ] != 'X' && !xEx){
        field[tempI][tempJ] = 'O';
        return true;
    }

    return false;
}

bool defense(char field[][3]){

    bool oEx = false;

    char tempI, tempJ;
    int z;

    for(int i=0; i<3; i++){

        for(int j=0; j<3; j++){

            if(field[i][j] == 'X'){
                z++;
            }else if(field[i][j] != 'O' && field[i][j] != 'X'){
                tempI = i;
                tempJ = j;
            }
        }
        if(z == 2 && field[tempI][tempJ] != 'O' && field[tempI][tempJ] != 'X' && tempI == i){
            field[tempI][tempJ] = 'O';
            return true;
        }

        tempI = 0;
        tempJ = 0;
        z = 0;
    }

    for(int j=0; j<3; j++){

        for(int i=0; i<3; i++){

            if(field[i][j] == 'X'){
                z++;
            }else if(field[i][j] != 'O' && field[i][j] != 'X'){
                tempI = i;
                tempJ = j;
            }
        }
        if(z == 2 && field[tempI][tempJ] != 'O' && field[tempI][tempJ] != 'X' && tempJ == j){
            field[tempI][tempJ] = 'O';
            return true;
        }
        tempI = 0;
        tempJ = 0;
        z = 0;
    }


    for(int i=0; i<3; i++){
        for(int j=0; j<3; j++){

            if(i==j){
                if(field[i][j] == 'X'){
                    z++;
                }else if(field[i][j] != 'O' && field[i][j] != 'X'){
                    tempI = i;
                    tempJ = j;
                }
                if(field[i][j]=='O'){
                    oEx = true;
                }
            }
        }
    }

    if(z == 2 && field[tempI][tempJ] != 'O' && field[tempI][tempJ] != 'X' && !oEx){
        field[tempI][tempJ] = 'O';
        return true;
    }
    tempI = 0;
    tempJ = 0;
    z = 0;
    oEx = false;

    for(int i=0; i<3; i++){
        for(int j=0; j<3; j++){

            if(i+j==2){
                if(field[i][j] == 'X'){
                    z++;
                }else if(field[i][j] != 'O' && field[i][j] != 'X'){
                    tempI = i;
                    tempJ = j;
                }
                if(field[i][j]=='O'){
                    oEx = true;
                }
            }
        }
    }

    if(z == 2 && field[tempI][tempJ] != 'O' && field[tempI][tempJ] != 'X' && !oEx){
        field[tempI][tempJ] = 'O';
        return true;
    }

    return false;
}
...
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
bool tricks(char field[][3]){

    if(!isTaken(field, 5)){
        field[1][1] = 'O';
        return true;
    }

    if(field[1][1] == 'X' && !isTaken(field, 1)){
        field[0][0] = 'O';
        return true;
    }

    if(!check){
        if(field[1][1] == 'O' && field[0][0] == 'X' && field[2][1] == 'X'){
            if(!isTaken(field, 7)){
                field[2][0] = 'O';
                check++;
                return true;
            }
        }

        if(field[1][1] == 'O' && field[0][2] == 'X' && field[2][1] == 'X'){
            if(!isTaken(field, 9)){
                field[2][2] = 'O';
                check++;
                return true;
            }
        }

        if(field[1][1] == 'O' && field[0][1] == 'X' && field[2][2] == 'X'){
            if(!isTaken(field, 3)){
                field[0][2] = 'O';
                check++;
                return true;
            }
        }

        if(field[1][1] == 'O' && field[2][0] == 'X' && field[0][1] == 'X'){
            if(!isTaken(field, 1)){
                field[0][0] = 'O';
                check++;
                return true;
            }
        }
    }

    if(!check2){
        if(field[1][1] == 'O' && field[0][0] == 'X' && field[2][2] == 'X'){
            if(!isTaken(field, 4)){
                field[1][0] = 'O';
                check2++;
                return true;
            }
        }

        if(field[1][1] == 'O' && field[2][0] == 'X' && field[0][2] == 'X'){
            if(!isTaken(field, 6)){
                field[1][2] = 'O';
                check2++;
                return true;
            }
        }

    }


    if(!check3){
        if(field[1][1] == 'X' && field[2][2] == 'X'){
            if(!isTaken(field, 7)){
                field[2][0] = 'O';
                check3++;
                return true;
            }
        }
    }

    if(!check4){

        if(field[1][1] == 'O' && field[0][1] == 'X' && field[1][2] == 'X'){
            if(!isTaken(field, 3)){
                field[0][2] = 'O';
                check4++;
                return true;
            }
        }

        if(field[1][1] == 'O' && field[0][1] == 'X' && field[1][0] == 'X'){
            if(!isTaken(field, 1)){
                field[0][0] = 'O';
                check4++;
                return true;
            }
        }

        if(field[1][1] == 'O' && field[1][0] == 'X' && field[2][1] == 'X'){
            if(!isTaken(field, 7)){
                field[2][0] = 'O';
                check4++;
                return true;
            }
        }

        if(field[1][1] == 'O' && field[2][1] == 'X' && field[1][2] == 'X'){
            if(!isTaken(field, 9)){
                field[2][2] = 'O';
                check4++;
                return true;
            }
        }


    }

    return false;
}

void random(char field[][3]){
    int pos, z;

    do{
        pos = (rand()%9)+1;
        z = 1;

        if(!isTaken(field, pos)){

           for(int i=0; i<3; i++){
                for(int j=0; j<3; j++){
                    if(z == pos){
                        field[i][j] =  'O';
                    }
                    z++;
                }
            }
        break;
        }


    }while(1);
}

char isWin(char field[][3]){
    int x = 0, o = 0;

    for(int i=0; i<3; i++){
        for(int j=0; j<3; j++){

            if(field[i][j] == 'O'){
                o++;
            }else if(field[i][j] == 'X'){
                x++;
            }
        }
        if(x == 3){
            return 'X';
        }else if(o == 3){
            return 'O';
        }
        x = 0;
        o = 0;
    }

    for(int j=0; j<3; j++){
        for(int i=0; i<3; i++){

            if(field[i][j] == 'O'){
                o++;
            }else if(field[i][j] == 'X'){
                x++;
            }
        }
        if(x == 3){
            return 'X';
        }else if(o == 3){
            return 'O';
        }
        x = 0;
        o = 0;
    }

    for(int i=0; i<3; i++){
        for(int j=0; j<3; j++){

            if(i==j){
                if(field[i][j] == 'O'){
                    o++;
                }else if(field[i][j] == 'X'){
                    x++;
                }
            }
        }
    }

    if(x == 3){
        return 'X';
    }else if(o == 3){
        return 'O';
    }
    x = 0;
    o = 0;

    for(int i=0; i<3; i++){
        for(int j=0; j<3; j++){

            if(i+j==2){
                if(field[i][j] == 'O'){
                    o++;
                }else if(field[i][j] == 'X'){
                    x++;
                }
            }
        }
    }

    if(x == 3){
        return 'X';
    }else if(o == 3){
        return 'O';
    }

    return NULL;
}
what are the examples of bad programming habits in this code

1) Use of globals
2) Use of using namespace std;
3) Use of system("pause");
4) The game is not replayable. If you want to play again, you have to restart the program.

Other than the use of cout, this really isn't C++. Consider making your board a class and your functions members of that class. If field were a member of that class, you wouldn't have to pass it to every function.




Ok, I will try to re-write it, but we haven't really got into object-oriented programming on my college, and they haven't yet mentioned classes. Anyway I will look into it and try.
Thank you very much! :)
i thought it was unbeatable, but i beat it in 2 tries, but not bad the bot got me in first try lol
Can you please tell me what you entered? Which numbers? :)
123
369
147
7814
and at the others side same pattern as 7814
My compiler told me you didn't initialize varibles in function attack(). I didn't check your code, but you have to be careful with this mistake
Nice job. The code is well organized. I especially want to congratulate you on realizing that something is wrong. That's the sort of intuition that helps beginners become experts.

isTaken() is very inefficient because you count your way up to find the i and j coordinates that correspond to pos. It would be much shorter to compute it instead. Also note that you can return a bool value directly instead of doing if (condition) return true; else return false;:
1
2
3
4
5
bool isTaken(char field[][3], int pos){
    int i = pos / 3;
    int j = pos % 3;
    return (field[i][j] == 'X' || field[i][j] == 'O')
}


The same technique to compute the row & column can be used in many places.

You also have a lot of code that basically looks like this:
for each row do foo
for each column do foo
for each diagonal do foo

You can parameterize this by creating a function called foo() and passing it the positions of 3 squares in a row, column or diagonal. PLEASE NOTE: the code below is untested:

1
2
3
4
5
6
7
8
9
foo(int positions[3])
{
    for (int x=0 x<3; ++x) {
        int i = positions[x]/3;
        int j = positions[x]%3;
        // i and j are now the row and column of a square in the group.
        // do the rest of the foo logic
    }
}


Now you can call foo() for each row, column and diagonal like this:
1
2
3
4
5
6
7
8
9
10
int groups[][3] = {
{0,1,2}, {3,4,5}, {6,7,8},   // rows
{0,3,6}, {1,4,7}, {2,5,8},   // columns
{0,4,8}, {2,4,6}  // diagonals
};
const int NumGroups=sizeof(groups)/sizeof(groups[0]);
...
for (int i = 0; i<NumGroups; ++i) {
    foo(groups[i]);
}


For example, attack() can be done like this:

// attack the group. If you can attack then return true and set i and j to the attack position:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bool attackGroup(int positions[3], int &i, int &j)
{
    int numO=0;
    int numEmpty = 0;
    for (int x=0; x<3; ++x) {
        int r= positions[x]/3;
        int c= positions[x]%3;
        if (field[r][c]) == 'O') ++numO;
        else if (field[r][c] != 'X') {
            ++numEmpty;
             i = r;
             j = c;
        }
    }
    return numO == 2 && numEmpty == 1;
}

Using these two changes throughout the code should shorten it dramatically.
Topic archived. No new replies allowed.