Tictactoe code wont run more than once

Today i decided to make an AI for my tictactoe game that i had created a few months ago for a class. It took several hours and whatnot to make but i did and its awesome because I think AI is very interesting. Also I made it so that the AI could play against a clone of itself, which I also accomplished. But then my friend said that if i had them play many many times that they would come out to be about even in wins/losses (of course there would be ties but those dont really matter).

Then I tried simulating them battling in tictactoe by looping the program and having the output go to a text file. HERE is the problem though: if i run the game by looping it or making the whole game into a recursive function, it will only play about 1.5 games and nothing will appear in the .txt file.

Also keep in mind that I did the game part a while ago for homework and waited till the last minute to do it so looking back on it now, i could have done a lot of different things to the main program. But right now I'm only concerned with the AI part of it which is file four. And a lot of formatting was lost when i put this in by the way.

Any help would be very appreciated because I really want to have my dumb AI battle and also any tips on coding in general after seeing my code would be cool too thx!(also the code should work if you take out the loop in the first file)

Here's the Code(First File):
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
#include "tictacfuncs.h"
#include "computer_AI.h"
#include "tictacgame.h"

int main()
{  
	int x = 0;
	
	fstream output_file;
	output_file.open("C:\\Users\\RARMAN\\Desktop\\F\\New Folder\\TicTacToe_output.txt", ios::out | ios::app);

              while(x < 10)
              {
		tictactoe_game();
		++x;
              }

	output_file <<"	   \n\nRESULTS:			  "	<<endl		<<
						"\nComputer One Wins: " <<comp_1	<<endl<<
						"\nComputer Two Wins: " <<comp_2	<<endl<<
						"\nTIES:              " <<ties		<<endl<<
						"\nGames Played:	  " <<MAX_GAMES	<<endl;

	output_file.close();
	return 0;
}

Second File:
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
#ifndef tictacgame_h
#define tictacgame_h
#include "tictacfuncs.h"
#include "computer_AI.h"

int comp_1 = 0, comp_2 = 0, ties = 0, MAX_GAMES = 0;

int tictactoe_game()
	{
		if(MAX_GAMES == 0){srand(time(NULL));}
		//show_board	(board);                //shows board first

		//show_help	();							//shows players how to play
	
	//=============================MAIN GAME LOOP=====================================//
	while((count < 9) && !winner)
	{
		//-----------------------Player 1's turn--------------------------//
			player_one_turn(board);
				if(count > 3)
				{
					Xwin_condition(winner, board); //checks if winning condition was met
					if(winner){//cout<<"Congrats computron uno you win!"<<endl;  //human 1 ++comp_1
						cout<<"winner = comp 1\n\n"; ++comp_1;  goto new_game;}
				}
				++count;

		//-------------------Player 2's turn---------------------//
			if(count != 9)
				{player_two_turn(board);}

				if(count > 3)
				{
					Owin_condition(winner, board); //checks if winning condition was met
					if(winner){//cout<<"Congrats computron two you win!"<<endl; //human 2 ++comp_2
						cout<<"winner = comp 2\n\n"; ++comp_2; goto new_game;}
				}
		++count;
	}	
	
	//=================================END OF GAME LOOP===============================//

	when_ties(count,winner,ties);			//handles a tie
	system("pause");

new_game:

	++MAX_GAMES; 
	count = 0; 
	winner = false;  

    int xrar = 3, yrar = 3;

	for(xrar; xrar<3; --xrar)
 		for(yrar; yrar<3; --yrar)
            board[xrar][yrar] = '*';

		  

		 // if(MAX_GAMES <3)
		  //{tictactoe_game();}
	
	return 0; //MAX_GAMES, comp_1, comp_2, ties;
	}

#endif


Third File:

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
#ifndef tictacfuncs_h
#define tictacfuncs_h

#include <iostream>
#include <time.h>
#include <cstdlib>
#include <iomanip>
#include <fstream>
#include "computer_AI.H"

using namespace std;


	#define ROWS 3
	#define COLS 3

	int count		= 0;
	//int placement	= 0;
	bool winner		= false;

	char board[ROWS][COLS] =	{'*','*','*',
					 '*','*','*',
					 '*','*','*' };
	
void show_board			(char [][COLS]);
//void player_one_pick	(int, char[][COLS]);
//void player_two_pick	(int, char[][COLS]);
void player_one_turn	(char[][COLS]);
void player_two_turn	(char[][COLS]);
bool Xwin_condition		(bool &, char[][COLS]);
bool Owin_condition		(bool &, char[][COLS]);
//void show_help		();
int when_ties			(int, bool, int&);
	
void player_one_turn(char [][COLS])
{
	//cout<<"\nComputer player one please input where you want to place an X"<<endl;
		comp_x_pick(board);cout<<endl; 
	//show_board	   (board);
	cout<<endl;
}

void player_two_turn(char [][COLS])
{
	//cout<<"\nComputer player two please input where you want to place an O"<<endl;
		comp_O_pick(board);cout<<endl; 
	//show_board(board);
	cout<<endl;
}

void show_board(char board[][COLS])
{	
	cout<<"The Board"<<endl<< 
			board[0][0]<< board[0][1]<< board[0][2]<<endl<<
			board[1][0]<< board[1][1]<< board[1][2]<<endl<<
			board[2][0]<< board[2][1]<< board[2][2]<<setw(50);

}

bool Xwin_condition(bool &winner, char board[][COLS])

{				/*all the possible win scenarios for diagonal wins*/

	if(((board[0][0] == 'X' && board[1][1] == 'X') && (board[0][0] == 'X' && board[2][2] == 'X')) ||
	((board[0][2] == 'X' && board[1][1] == 'X') && (board[0][2] == 'X' && board[2][0] == 'X')) ||

				/*all the possible win scenarios for across and down wins*/
	((board[0][0] == 'X' && board[1][0] == 'X') && (board[0][0] == 'X' && board[2][0] == 'X')) ||
	((board[0][1] == 'X' && board[1][1] == 'X') && (board[0][1] == 'X' && board[2][1] == 'X')) ||
	((board[0][2] == 'X' && board[1][2] == 'X') && (board[0][2] == 'X' && board[2][2] == 'X')) ||
	((board[2][0] == 'X' && board[2][1] == 'X') && (board[2][0] == 'X' && board[2][2] == 'X')) ||
	((board[1][0] == 'X' && board[1][1] == 'X') && (board[1][0] == 'X' && board[1][2] == 'X')) ||
	((board[0][0] == 'X' && board[0][1] == 'X') && (board[0][0] == 'X' && board[0][2] == 'X')))	  
		{
            winner = true;
		}

	return winner;
}
bool Owin_condition(bool &winner, char board[][COLS])
{

	if(((board[0][0] == 'O' && board[1][1] == 'O') && (board[0][0] == 'O' && board[2][2] == 'O')) ||
	((board[0][2] == 'O' && board[1][1] == 'O') && (board[0][2] == 'O' && board[2][0] == 'O')) ||


	((board[0][0] == 'O' && board[1][0] == 'O') && (board[0][0] == 'O' && board[2][0] == 'O')) ||
	((board[0][1] == 'O' && board[1][1] == 'O') && (board[0][1] == 'O' && board[2][1] == 'O')) ||
	((board[0][2] == 'O' && board[1][2] == 'O') && (board[0][2] == 'O' && board[2][2] == 'O')) ||
	((board[2][0] == 'O' && board[2][1] == 'O') && (board[2][0] == 'O' && board[2][2] == 'O')) ||
	((board[1][0] == 'O' && board[1][1] == 'O') && (board[1][0] == 'O' && board[1][2] == 'O')) ||
	((board[0][0] == 'O' && board[0][1] == 'O') && (board[0][0] == 'O' && board[0][2] == 'O')))	  
		{
			winner = true;
		}

	  return winner;
}

int when_ties(int count, bool winner, int &ties)
{
	if(count >= 9 && (!winner))
	{
		cout<<"Tie Game! You are both losers!"<<endl;
		++ties;
	}
	return ties;
}

/*

void show_help()
{
		  cout<<"Each place corresponds to a number, 1-9."<<setw(30)<<endl<<
				"1 2 3"									  <<setw(30)<<endl<<
				"4 5 6"								      <<setw(30)<<endl<<
				"7 8 9"									  <<endl;

*/
}
#endif 


Last File (but my fav one):

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

#ifndef computer_AI_h
#define computer_AI_h

#include <iostream>
#include <time.h>
#include <cstdlib>
#include <iomanip>
#include "tictacfuncs.h"
using namespace std;

#define ROWS 3
#define COLS 3


void comp_x_pick(char[][COLS]);
void comp_O_pick(char[][COLS]);


void comp_x_pick(char board[][COLS])
{
									
	bool stopper = true;

	while(stopper)
{
	int xrand = rand() % 3;
	int yrand = rand() % 3;	

		if((board[xrand][yrand] != 'X') && (board[xrand][yrand] != 'O'))
			{
				board[xrand][yrand] = 'X';
				stopper = false;
			}	
	
	
}		
}

void comp_O_pick(char board[][COLS])
{
									
	bool stopper = true;

	while(stopper)
{
	int wrand = rand() % 3;
	int zrand = rand() % 3;	

		if((board[wrand][zrand] != 'X') && (board[wrand][zrand] != 'O'))
			{
				board[wrand][zrand] = 'O';
				stopper = false;
			}	
}		
}

#endif 
Last edited on
For one thing count is incrementing by twos I notice. If thats not what you intended try changing that I have a feeling you are in an infinite loop in while((count < 9) && !winner)
Count is supposed to increment after each player's turn because it is only possible to win after a certain amount of turns.

But after reviewing my code just now i think the function that checks for ties should actually be in the main game loop.

Hmm i wonder if i had the when_ties function inside the main loop at some point and just took it out for whatever reason. Thanks for the help!

EDIT: i found even with changing where the when ties function is called doesnt matter. And i tried looping the program again but for some reason it just doesnt want to run more than once.
Last edited on
The problem is that the board doesn't reset

line 52 second file
1
2
3
4
5
int xrar = 3, yrar = 3;

	for(xrar; xrar<3; --xrar)
 		for(yrar; yrar<3; --yrar)
            board[xrar][yrar] = '*';

xrar starts at 3 so the for loop will never be entered.
That means that the old O's and X's are still on the board.
That means that the ai will enter an infinite loop trying to find an empty spot
for his next move.
Topic archived. No new replies allowed.