I am having trouble with writing a tic tac toe program. My code executes just fine if the first move results in a win. If it doesn't the code just stops. It doesn't finish but it does not continue to run. Can someone help? Must start with the game board already at:

- x o
- - -
o - -

Here is my code:

#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;


int winner(char tic[][3],int size, bool& finished);
//determines winner.

void currentGame(char tic[][3], int size);
//inputs current game board.

void displayGame(char tic[][3],int size);
//displays current game board.

void opponentPlay(char tic[][3], int size);
//human's move.

void nextPlay(char tic[][3], int size);
//computer's move.

unsigned int pseudoRand (unsigned int prime);
//generates random computer move.



int main()
{
	int i,j;
	char tic[3][3];
	bool finished=false;
	currentGame(tic,9);
	cout<<"Welcome to Tic Tac Toe.  I will be O'x, you can be X's. You may play first."<<endl;
	while(finished!=true)
	{
		bool keep_playing=false;
		for(i=0;i<3;i++)
		{
			for(j=0;j<3;j++)
			{
				if(tic[i][j]=='-')
				{
					bool keep_playing=true;
				}
			}
		}
		if(keep_playing=true)
		{
			opponentPlay(tic,9);
			winner(tic,9,finished);
			nextPlay(tic,9);
			winner(tic,9,finished);
		}
		else
			cout<<"The game ends in a draw."<<endl;
	}
	return 0;
}



void currentGame(char tic[][3], int size)
{
	int i,j;
	for(i=0;i<3;i++)
	{
		for(j=0;j<3;j++)
		{
			cout<<"Enter current game board. ";
			cin>>tic[i][j];
		}
	}
}

void displayGame(char tic[][3],int size)
{
	int i,j;
	for(i=0;i<3;i++)
	{
		for(j=0;j<3;j++)
		{
			cout<<tic[i][j]<<" ";
		}
		cout<<endl;
	}
	cout<<endl;
}

void opponentPlay(char tic[][3], int size)
{
	int i,j;
	displayGame(tic,9);
	cout<<"Enter the row and column you wish to play in (0-2): "<<endl;
	cin>>i>>j;
	if(i<0||i>2||j<0||j>2)
	{
		cout<<"Invalid move"<<endl;
		cin>>i>>j;
	}
	while(tic[i][j]!='-')
	{
		cout<<"Already taken."<<endl;
		cin>>i>>j;
	}
	tic[i][j]='x';
	displayGame(tic,9);
}

int winner(char tic[][3],int size, bool &finished)
{
	int i;
	char x,o;

		//Check row win for X.
		for(i=0;i<3;i++)
		{
			if((tic[i][0]==tic[i][1]) && (tic[i][0]==tic[i][2]) && (tic[i][0]=='x'))
			{
				cout<<"Congratulations! You win!"<<endl;
				finished=true;
			}
		}

		//Check row win for O.
		for(i=0;i<3;i++)
		{
			if((tic[i][0]==tic[i][1]) && (tic[i][0]==tic[i][2]) && (tic[i][0]=='o'))
			{
				cout<<"I win, nice try!"<<endl;
				finished=true;
			}
		}


		//Check column win for X.
		for(i=0;i<3;i++)
		{
			if((tic[0][i]==tic[1][i]) && (tic[0][i]==tic[2][i]) && (tic[0][i]=='x'))
			{
				cout<<"Congratulations! You win!"<<endl;
				finished=true;
			}
		}

		//Check column win for O.
		for(i=0;i<3;i++)
		{
			if((tic[0][i]==tic[1][i]) && (tic[0][i]==tic[2][i]) && (tic[0][i]=='o'))
			{
				cout<<"I win, nice try."<<endl;
				finished=true;
			}
		}

		//Check diagonal win for X.
		if((tic[0][0]==tic[1][1]) && (tic[0][0]==tic[2][2]) && (tic[0][0]=='x'))
		{
			cout<<"Congratulations! You win!"<<endl;
			finished=true;
		}

		if((tic[0][2]==tic[1][1]) && (tic[0][2]==tic[2][0]) && (tic[0][2]=='x'))
		{
			cout<<"Congratulations! You win!"<<endl;
			finished=true;
		}

		//Check diagonal win for O.
		if((tic[0][0]==tic[1][1]) && (tic[0][0]==tic[2][2]) && (tic[0][0]=='o'))
		{
			cout<<"I win, nice try."<<endl;
			finished=true;
		}

		if((tic[0][2]==tic[1][1]) && (tic[0][2]==tic[2][0]) && (tic[0][2]=='o'))
		{
			cout<<"I win, nice try."<<endl;
			finished=true;
		}
		return finished;
}

unsigned int pseudoRand (unsigned int prime)
{
	unsigned int a=22695477,c=1,next;
	next=(a*prime)+c;
	next=next%3;
	return next;
}

void nextPlay(char tic[][3], int size)
{
	int i,j,numI=75,numJ=18;
	while(tic[i][j]!='-')
	{
		i=pseudoRand(numI);
		j=pseudoRand(numJ);
	}
	cout<<"I will play row "<<i<<" and column "<<j<<"."<<endl;
	tic[i][j]='o';
	displayGame(tic,9);
}

there would be a segmentation fault at line 195 cause you did not initialize the values of i and j

Also shouldn't you use some kind of heuristic algorithm for making the computer play. You can do it with simple if statements and loops, or if you up for a challenge a minimax algorithm with alpha beta pruning allows for a variable AI.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.