tic_tac_toe

in some instances when i run this program the AI part of the program will stop running after the player's 3rd move. i can not find the problem and if some1 could spot it out that would be great.
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
#include <iostream>
#include <ctype.h>

void fillBoard(int board[3][3]);

void printBoard(int board[3][3]);

void evaluate(int board[3][3], int sums[8]);

void translate(int board[3][3], int choice);

void step1(int board[3][3], int sums[], bool &turn);

void step2(int board[3][3], int sums[], bool &turn);

void step3(int board[3][3], bool &turn);

void step4(int board[3][3], int sums[], bool &turn);

void step5(int board[3][3], int sums[], bool &turn);

void trollWin (int board[3][3]);

void mywait(float seconds);

bool checkLose (int board[3][3], int sums[]);

char converter(int); 




int main(){
		
		
    int nums[3][3] = {}, sums[8] = {}, choice, turnCount=0;
    bool win;
    char first;
    
start: 
		turnCount=0;
		win=false;
		fillBoard(nums);
    std::cout << "Do you want to go (f)irst or (s)econd or (q)uit? (You are X's): ";
    std::cin >> first;
    first=tolower(first);
		std::cout << "\n";
		
		if (first=='q') {
				goto end;
		}
		if (first=='s') {
				nums[1][1]=5;
		}
		
		if (first=='t') {
				trollWin(nums);
		}
		
    while (win==false) {
		
        bool turn=false;    
        
        printBoard(nums);
				std::cout << "\nWhich space would you like to go in?: ";
        std::cin >> choice;

				turnCount++;
				
				translate(nums, choice);
				
				if (turnCount==4 && first=='s') {
						std::cout << "\n\n\n";
						printBoard(nums);
						std::cout << "You tied! Better luck next time!\n\n\n\n";
						goto start; 
						return 0;
				}
				if (turnCount==5) {
						std::cout << "\n\n\n";
						printBoard(nums);
						std::cout << "You tied! Better luck next time!\n\n\n\n";
						goto start; 
						return 0;
				}
        
        translate(nums, choice);
        
        evaluate(nums, sums);
        
        step1(nums, sums, turn);
        
        if(turn==false)
						step2(nums, sums, turn);
        
        if(turn==false)
						step3(nums, turn);
				
        if(turn==false)
						step4(nums, sums, turn);
				
        if(turn==false)
						step5(nums, sums, turn);
				
				win=checkLose (nums, sums);
				
				    }
		turnCount=0;
		goto start;
end:
		if (first=='q') {
				std::cout << "Thanks for playing!";
		}
		if (first!='q')
		std::cout << "I WON"; 
    return 0;
		
		
}
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
void fillBoard(int board[3][3]){
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            board[i][j] = 1;
        }
    }
}

void printBoard(int board[3][3]){
		
		
		std::cout << " " << converter(board[0][0]) << " | "  << converter(board[0][1]) << " | " << converter(board[0][2]) << "  \n";
		std::cout << "___|___|___\n" ; 
		std::cout << " " << converter(board[1][0]) << " | " << converter(board[1][1]) << " | " << converter(board[1][2])<< " \n" ; 
		std::cout << "___|___|___\n" ;
		std::cout << " " << converter(board[2][0]) << " | " << converter(board[2][1]) << " | " << converter(board[2][2]) << " \n";
		
}

void evaluate(int board[3][3], int sums[8]){
		
		
    std::cout << "\n";
    sums[0] = board[0][0] + board[0][1] + board[0][2];
    sums[1] = board[1][0] + board[1][1] + board[1][2];
    sums[2] = board[2][0] + board[2][1] + board[2][2];
    sums[3] = board[0][0] + board[1][0] + board[2][0];
    sums[4] = board[0][1] + board[1][1] + board[2][1];
    sums[5] = board[0][2] + board[1][2] + board[2][2];
    sums[6] = board[0][0] + board[1][1] + board[2][2];
    sums[7] = board[2][0] + board[1][1] + board[0][2];

    std::cout << '\n';
		
		
}

void translate(int board[3][3], int choice){
		
		
    switch (choice) {
        case 1:
            board[0][0] = 10;
            break;
        case 2:
            board[0][1] = 10;
            break;
        case 3:
            board[0][2] = 10;
            break;
        case 4:
            board[1][0] = 10;
            break;
        case 5:
            board[1][1] = 10;
            break;
        case 6:
            board[1][2] = 10;
            break;
        case 7:
            board[2][0] = 10;
            break;
        case 8:
            board[2][1] = 10;
            break;
        case 9:
            board[2][2] = 10;
            break;
        default:
            break;
    }
}

void step1(int board[3][3], int sums[], bool &turn){
		for (int a = 0; a < 8; a++) {
        if (sums[a] == 11) {
            //row 1 = sum[0], column 1 = sum[3], diag1=sum[6]
            for (int i = 0; i < 3; i++) {
                switch (a) {
                    case 0:
                        if (board[0][i] == 1)
                            board[0][i] = 5;
                        turn=true;
                        break;
                    case 1:
                        if (board[1][i] == 1)
                            board[1][i] = 5;
                        turn=true;
                        break;
                    case 2:
                        if (board[2][i] == 1)
                            board[2][i] = 5;
                        turn=true;
                        break;
                    case 3:
                        if (board[i][0] == 1)
                            board[i][0] = 5;
                        turn=true;
                        break;
                    case 4:
                        if (board[i][1] == 1) {
                            board[i][1] = 5;
                        }
                        turn=true;
                        break;
                    case 5:
                        if (board[i][2] == 1)
                            board[i][2] = 5;
                        turn=true;
                        break;
                    case 6:
                        if (board[0][0] == 1)
                            board[0][0] = 5;
                        if (board[1][1] == 1)
                            board[1][1] = 5;
                        if (board[2][2])
                            board[2][2] = 5;
                        turn=true;
                        break;
                    case 7:
                        if (board[0][2] == 1)
                            board[0][2] = 5;
                        if (board[1][1] == 1)
                            board[1][1] = 5;
                        if (board[2][0])
                            board[0][2] = 5;
                        turn=true;
                        break;
												
												
                    default:
                        break;
                }
            }
        }
}
}

void step2(int board[3][3], int sums[], bool &turn){
    
		for (int a = 0; a < 8; a++) {
        if (sums[a] == 21) {
            //row 1 = sum[0], column 1 = sum[3], diag1=sum[6]
            for (int i = 0; i < 3; i++) {
                switch (a) {
                    case 0:
                        if (board[0][i] == 1)
                            board[0][i] = 5;
                        turn=true;
                        break;
                    case 1:
                        if (board[1][i] == 1)
                            board[1][i] = 5;
                        turn=true;
                        break;
                    case 2:
                        if (board[2][i] == 1)
                            board[2][i] = 5;
                        turn=true;
                        break;
                    case 3:
                        if (board[i][0] == 1)
                            board[i][0] = 5;
                        turn=true;
                        break;
                    case 4:
                        if (board[i][1] == 1) {
                            board[i][1] = 5;
                        }
                        turn=true;
                        break;
                    case 5:
                        if (board[i][2] == 1)
                            board[i][2] = 5;
                        turn=true;
                        break;
                    case 6:
                        if (board[0][0] == 1)
                            board[0][0] = 5;
                        if (board[1][1] == 1)
                            board[1][1] = 5;
                        if (board[2][2])
                            board[2][2] = 5;
                        turn=true;
                        break;
                    case 7:
                        if (board[0][2] == 1)
                            board[0][2] = 5;
                        if (board[1][1] == 1)
                            board[1][1] = 5;
                        if (board[2][0])
                            board[0][2] = 5;
                        turn=true;
                        break;
												
												
												
												
                    default:
                        break;
                }
            }
        }
    }
    
}

void step3(int board[3][3], bool &turn){
    if (board[1][1] == 1) {
        board[1][1] = 5;
        turn=true;
    }
    
    if (board[1][1]==10&&board[0][0]==1){
        board[0][0]=5;
        turn=true;
    }
}

void step4(int board[3][3], int sums[], bool &turn){
		
    
		if (board[1][1]==5) {
				if (board[0][0] == 10 && board[2][2] == 10) {
						if (board[0][1] == 1) {
								board[0][1] = 5;
								turn=true;
						}
				}
				if (board[0][2] == 10 && board[2][0] == 10) {
						if (board[0][1] == 1) {
								board[0][1] = 5;
								turn=true;
						}
				}
		}
		
    
}

void step5(int board[3][3], int sums[], bool &turn){
    
    bool filledSpace=false;
    
    if (board[0][2]==1){
        board[0][2]=5;
        filledSpace=true;
    }
    
    srand(static_cast<int>(time(NULL)));
    
    int randSpot1, randSpot2;
    
    while(filledSpace==false){
        
        randSpot1= rand()%3;
        randSpot2= rand()%3;
        
        if(board[randSpot1][randSpot2]==1){
            board[randSpot1][randSpot2]=5;
            filledSpace=true;
            turn=true;
            
        }
				
				
    }
    
    
}

char converter(int i){
switch (i) {
		case 5:
				return 'O'; 
				break;
		case 10:
				return 'X'; 
		default:
				return ' ';
				break;
}
		return ' '; 
}
Last edited on
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
bool checkLose(int board[3][3], int sums[]){

		 
		evaluate(board, sums);
		
		for (int a = 0; a < 8; a++) {
				if (sums[a]==15) {
						printBoard(board);
						std::cout << "\nYou lost! Better luck next time!\n\n";
						return true;
				}
		}
		
		return false;
}

void trollWin(int board[3][3]){
		
		std::cout << "\n\nTroll mode initiated.\n\n Loading";
		
		for(int i = 0; i < 10; i++){
				std::cout << "III";
				mywait(.1);
		}
		std::cout << '\n'; 
		
std::cout << "		      ▄▄▄▀▀▀▀▀▀▀▀▄▄▄▄▄▄\n";
std::cout << "		     █                  ▀▀▄\n";
std::cout << "		    █                      █\n";
std::cout << "		   █      ▄██▀▄▄     ▄▄▄    █\n";
std::cout << "		 ▄▀ ▄▄▄  █▀▀▀▀▄▄█   ██▄▄█    █\n";
std::cout << "		█  █ ▄ ▀▄▄▄▀        █         █\n";
std::cout << "		█  █ █▀▄▄     █▀    ▀▄  ▄▀▀▀▄ █\n";
std::cout << "		 █ ▀▄ █▄ █▀▄▄ ▀ ▀▀ ▄▄▀   █  █\n";
std::cout << "		  █   ▀▄▀█▄▄ █▀▀▀▄▄▄▄▀▀█▀██ █\n";
std::cout << "		   █    ██  ▀█▄▄▄█▄▄█▄████ █\n";
std::cout << "		    █    ▀▀▄ █   █ █▀██████ █\n";
std::cout << "		     ▀▄     ▀▀▄▄▄█▄█▄█▄█▄▀  █\n";
std::cout << "		       ▀▄▄                   █\n";
std::cout << "		          ▀▀▄▄               █\n";
std::cout << "								 ▀▄▄▄▄▄        █\n";		
		
		for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            board[i][j] = 5;
        }
    }
		
		printBoard(board);
		
		std::cout << "\nYou mad???\n\n";
		mywait(3);
		
		for(int i = 0; i < 1337; i++){
				std::cout << "TROLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOL\n";
		}
		exit(1337); 
}

void mywait(float seconds){
		clock_t endwait;
		endwait = clock() + seconds * CLOCKS_PER_SEC;
		while(clock() < endwait){}				
}
Topic archived. No new replies allowed.