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
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <cstdlib>
#include <ctime>
#include <cassert>

using namespace std;

char sqr_rep(char playerchoice, int box_num)
{
    switch (box_num) {
    case 0:
        return ' ';
    case 1:
        return 'X' == playerchoice ? 'X' : 'O';
    case 10:
        return 'X' == playerchoice ? 'O' : 'X';
    default:
        assert(false && "box_num not a valid quantity");
    }
}
void draw_board(char playchoice,
    int box1,
    int box2,
    int box3,
    int box4,
    int box5,
    int box6,
    int box7,
    int box8,
    int box9)
{
    cout << "Enter numbers 1-9(as shown) to play \n";
    cout << "1 | 2 | 3\n";
    cout << "---------\n";
    cout << "4 | 5 | 6\n";
    cout << "---------\n";
    cout << "7 | 8 | 9\n\n";
    if (playchoice == 'X')
        cout << "You go first\n";
    else
        cout << "Computer goes first\n";

    char pc = playchoice;
    cout << sqr_rep(pc, box1) << "|" << sqr_rep(pc, box2) << "|"
         << sqr_rep(pc, box3) << endl;
    cout << sqr_rep(pc, box4) << "|" << sqr_rep(pc, box5) << "|"
         << sqr_rep(pc, box6) << endl;
    cout << sqr_rep(pc, box7) << "|" << sqr_rep(pc, box8) << "|"
         << sqr_rep(pc, box9) << endl;
}
int main()
{
    srand(time(0));
    char play;
    while (play != 'N') {
        int x = 3, y = 10, entry, t = 0, box1 = 0, box2 = 0, box3 = 0, box4 = 0,
            box5 = 0, box6 = 0, box7 = 0, box8 = 0, box9 = 0;
        int num1, num2, num3, num4, num5, num6, num7, num8;
        int i = 0, n;
        char playchoice;

        cout << "Tick Tack Toe\n\n";
        cout << "Do you want to be X (first players) or O (second player)?";
        cin >> playchoice;
        if (playchoice == 'X') {
            cout << "You go first\n";
        }
        else {
            cout << "Computer goes first\n";
            playchoice = 'O';
        }

        if (playchoice == 'X')
            n = 1;
        else
            n = 0;
        while (t < 9) {
            t = t + 1;
            i = (n + t) % 2;

            draw_board(playchoice, box1, box2, box3, box4, box5, box6, box7, box8,
                box9);
                
            if (i == 0) { /* Player move*/
                cin >> entry;

                if (entry == 1 and box1 == 0) {
                    box1 = 1;
                }
                else if (entry == 2 and box2 == 0) {
                    box2 = 1;
                }
                else if (entry == 3 and box3 == 0) {
                    box3 = 1;
                }
                else if (entry == 4 and box4 == 0) {
                    box4 = 1;
                }
                else if (entry == 5 and box5 == 0) {
                    box5 = 1;
                }
                else if (entry == 6 and box6 == 0) {
                    box6 = 1;
                }
                else if (entry == 7 and box7 == 0) {
                    box7 = 1;
                }
                else if (entry == 8 and box8 == 0) {
                    box8 = 1;
                }
                else if (entry == 9 and box9 == 0) {
                    box9 = 1;
                }
                else {
                    cout << endl
                         << "Please Enter a number from 1-9 that has not been taken";
                    t = t - 2;
                }
            }
            else /* Computer move*/
            {
                if (playchoice == 'X')
                    playchoice = 'O';
                else
                    playchoice = 'X';

                /*first choice*/
                if (num1 == 20) {
                    cout << "\n\nComputer Wins!\n\n";
                    break;
                }
                else if (num2 == 20) {
                    cout << "\n\nComputer Wins!\n\n";
                    break;
                }
                else if (num3 == 20) {
                    cout << "\n\nComputer Wins!\n\n";
                    break;
                }
                else if (num4 == 20) {
                    cout << "\n\nComputer Wins!\n\n";
                    break;
                }
                else if (num5 == 20) {
                    cout << "\n\nComputer Wins!\n\n";
                    break;
                }
                else if (num6 == 20) {
                    cout << "\n\nComputer Wins!\n\n";
                    break;
                }
                else if (num7 == 20) {
                    cout << "\n\nComputer Wins!\n\n";
                    break;
                }
                else if (num8 == 20) {
                    cout << "\n\nComputer Wins!\n\n";
                    break;
                }
                else if (box5 == 0) {
                    box5 = 10;
                }
                /*second choice*/ else if ((num1 == 2 or num4 == 2 or num7 == 2) and box1 == 0) {
                    box1 = 10;
                }
                else if ((num1 == 2 or num6 == 2 or num8 == 2) and box3 == 0) {
                    box3 = 10;
                }
                /*box 5 is taken care of a the beginning of this code*/
                else if ((num3 == 2 or num4 == 2 or num8 == 2) and box7 == 0) {
                    box7 = 10;
                }
                else if ((num3 == 2 or num6 == 2 or num7 == 2) and box9 == 0) {
                    box9 = 10;
                }
                else if ((num1 == 2 or num5 == 2) and box2 == 0) {
                    box2 = 10;
                }
                else if ((num2 == 2 or num4 == 2) and box4 == 0) {
                    box4 = 10;
                }
                else if ((num2 == 2 or num6 == 2) and box6 == 0) {
                    box6 = 10;
                }
                else if ((num3 == 2 or num5 == 2) and box8 == 0) {
                    box8 = 10;
                }
                /*third choice*/ else if ((num1 == 10 or num4 == 10 or num7 == 10) and box1 == 0) {
                    box1 = 10;
                }
                else if ((num1 == 10 or num6 == 10 or num8 == 10) and box3 == 0) {
                    box3 = 10;
                }
                /*box 5 is taken care of a the beginning of this code*/
                else if ((num3 == 10 or num4 == 10 or num8 == 10) and box7 == 0) {
                    box7 = 10;
                }
                else if ((num3 == 10 or num6 == 10 or num7 == 10) and box9 == 0) {
                    box9 = 10;
                }
                else if ((num1 == 10 or num5 == 10) and box2 == 0) {
                    box2 = 10;
                }
                else if ((num2 == 10 or num4 == 10) and box4 == 0) {
                    box4 = 10;
                }
                else if ((num2 == 10 or num6 == 10) and box6 == 0) {
                    box6 = 10;
                }
                else if ((num3 == 10 or num5 == 10) and box8 == 0) {
                    box8 = 10;
                }
                /*fourth choice*/ else if (box1 == 0) {
                    box1 = 10;
                }
                else if (box2 == 0) {
                    box2 = 10;
                }
                else if (box3 == 0) {
                    box3 = 10;
                }
                else if (box4 == 0) {
                    box4 = 10;
                }
                else if (box5 == 0) {
                    box5 = 10;
                }
                else if (box6 == 0) {
                    box6 = 10;
                }
                else if (box7 == 0) {
                    box7 = 10;
                }
                else if (box8 == 0) {
                    box8 = 10;
                }
                else {
                    box9 = 10;
                }

                if (playchoice == 'O')
                    playchoice = 'X';
                else
                    playchoice = 'O';
            }


            num1 = box1 + box2 + box3;
            num2 = box4 + box5 + box6;
            num3 = box7 + box8 + box9;
            num4 = box1 + box4 + box7;
            num5 = box2 + box5 + box8;
            num6 = box3 + box6 + box9;
            num7 = box1 + box5 + box9;
            num8 = box3 + box5 + box7;
            if (num1 == 30 or num2 == 30 or num3 == 30 or num4 == 30 or num5 == 30 or num6 == 30 or num7 == 30 or num8 == 30) {
                cout << endl
                     << endl;
                cout << "COMPUTER WINS!\n";
                break;
            }
            if (num1 == 3 or num2 == 3 or num3 == 3 or num4 == 3 or num5 == 3 or num6 == 3 or num7 == 3 or num8 == 3) {
                cout << endl
                     << endl;
                cout << "PLAYER WINS!";
                break;
            }
            cout << num1 << " " << num2 << " " << num3 << " " << num4 << " " << num5
                 << " " << num6 << " " << num7 << " " << num8;
        }
        if (t == 9) {
            cout << endl
                 << endl;
            cout << "Draw.\n";
        }
        if (num1 == 30 or num2 == 30 or num3 == 30 or num4 == 30 or num5 == 30 or num6 == 30 or num7 == 30 or num8 == 30) {
            cout << endl
                 << endl;
            cout << "COMPUTER WINS!\n";
        }
        if (num1 == 3 or num2 == 3 or num3 == 3 or num4 == 3 or num5 == 3 or num6 == 3 or num7 == 3 or num8 == 3) {
            cout << endl
                 << endl;
            cout << "PLAYER WINS!\n";
        }
        cout << "\n Do you want to play again (Y/N)";
        cin >> play;
        //system("cls");
    }
    return 0;
}