I have this tic tac toe code, it`s almost working, can anybody take a look, I think the only thing wrong is in the else statement in main, but I`m not sure, I think I need to place the checkToWin and checkToBlock functions properly in main

// Laelzio Mosca
// 12/09/09
// Tic Tac Toe game

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <iomanip>
using std::setw;

#include <string>
using std::string;

#include <cstdlib>
using std::rand;
using std::srand;

#include <ctime>
using std::time;

//global variables
const int row = 3; //size of array
const int column = 3; // size of array
string BLANK = " ";
string ticTac[row][column];

static int nspots = 9; // number of possible spots

void ticTacToe(const string ticTacToe[][column]); //Function prototypes
void player1(string ticTacToe[][column]);
void player2(string ticTac[][column]);
void generateNumber(string ticTac[][column]);
void checkWinner(string ticTac[][column]);
void checkToBlock(string ticTac[][column]);
void checkTowin(string ticTac[][column]);

// program execution begins
int main()
{
	srand( time(0));
	int numberOfPlayers;

	string ticTac[row][column] = {{BLANK,BLANK,BLANK}, {BLANK,BLANK,BLANK}, {BLANK,BLANK,BLANK}}; //number display for rows and columns 
	cout << "Welcome to the tic tac toe game "<<endl << endl; // welcome message
	cout << "Enter the number of players with a max of 2 and press enter: ";
	cin >> numberOfPlayers;
	if (numberOfPlayers == 2)
	{
		ticTacToe( ticTac ); // function call

			   while (nspots > 0) 	
				{		
					player1(ticTac);	
					checkWinner(ticTac);

					if (nspots < 0) break;
					if (nspots == 0) {
						cout << "DRAW" << endl;
						break;
					}
					player2(ticTac);

					checkWinner(ticTac);
					// the following should never happen
					if (nspots == 0) {
						cout << "DRAW" << endl;
						break;
					}
				//	cout << "nspots: " << nspots << endl;
				}
			   cout << endl;
			
			   fflush(stdin); // clear the buffer, and wait to exit.
			   cout << "\nPress enter to exit " << endl;
			   getc(stdin);
	}
	else
	{
		ticTacToe( ticTac ); // function call

			   while (nspots > 0) 	
				{		
					player1(ticTac);	
					checkWinner(ticTac);

					if (nspots < 0) break;
					if (nspots == 0)
					{
						cout << "DRAW" << endl;
						break;
					}
					if(nspots < 8)
					{
						checkToBlock(ticTac);
						checkWinner(ticTac);
					}
					else
					{
						generateNumber(ticTac);	
						checkWinner(ticTac);
					}
					// the following should never happen
					if (nspots < 0) break;
					if (nspots == 0) {
						cout << "DRAW" << endl;
						break;
					}
				}
			   cout << endl;
			
			   fflush(stdin); // clear the buffer, and wait to exit.
			   cout << "\nPress enter to exit " << endl;
			   getc(stdin);
	}

	return 0;
}

//Function ticTacToe
void ticTacToe(const string ticTacToe[][column])
{
	cout << setw(5)<<"|"<<setw(5)<< "|" <<endl;

	//for loop-continuation condition and increment
	for (int i = 0; i < row; i ++)
	{
		for (int j = 0; j < column; j++)
			//check to output '|' only in between the numbers, not at the end
			if ( j < column - 1)
			{
				cout << setw(3)<< ticTacToe[i][j] << setw(2)<<"|";
			}
			else					
				cout << setw(3)<<ticTacToe[i][j];
							
			cout << endl;

			// check to make sure the horizontal line in only printed in between
			// the 3 rows, not on the bottom fo the picture
			if ( i < row -1)
			{
				cout << "____|____|____"<<endl;
				cout << setw(5) << "|" << setw(5) << "|" << endl;
			}
			else
				cout << setw(5) << "|" << setw(5) << "|" << endl << endl;		
			
	}
}

void player1(string ticTac[][column])
{
	int x = 0;
	int y = 0;
	string p1 = "X";

	cout << "Player one can enter your coordinates for X." << endl;
		cin >> x >> y ;
		x--, y--;
		while (x >= row || y >= column)
		{
			cout << "Invalit entry, try a number from 1 to 3.";
			cin >> x >> y;
			x--, y--;
		}
		if (ticTac[x][y] != BLANK)
		{
			cout << "That spot is taken." << "\nEnter different coordinates for 'X'." << endl;
			player1(ticTac);
		}
		else {
			ticTac[x][y] = p1;
			nspots--;
		}
		ticTacToe( ticTac );
		
}

void player2(string ticTac[][column])
{
	int x = 0;
	int y = 0;
	string p2 = "O";

	cout << "Player two can enter you coordinates for 'O'. ";
		cin >> x >> y;
	x--,y--;
		while  (x >= row || y >= column)
		{
			cout << "Invalit entry, try a number from 1 to 3.";
			cin >> x >> y;
			x--, y--;
		}
		if (ticTac[x][y] != BLANK)
		{
			cout << "That spot is taken." << "\nEnter different coordinates for 'O'." << endl;
			player2(ticTac);
		}
		else {
			ticTac[x][y] = p2;
			nspots--;
		}
		ticTacToe( ticTac );
}

void generateNumber(string ticTac[][column])
{

	int a; // number 1
	int b; // number 2
	string gn = "O";
	
	
	do {
		a = (rand() % 3); // generate number 1
	} while (a > 2);
	do {
		b = (rand() % 3); // generate number 2
	} while (b > 2);


		if (ticTac[a][b] != BLANK)
		{
			generateNumber(ticTac);
		}
		else
		{
			ticTac[a][b] = gn;	
			nspots--;
		}
	ticTacToe( ticTac );
}
void checkWinner(string ticTac[][column])
{
			//check columns for winner // column 1
		   if ((ticTac[0][0] == ticTac[1][0]) && (ticTac[1][0] == ticTac[2][0]) && (ticTac[0][0] != BLANK))
		   {
			   cout << "YOU ARE THE WINNER!";
			   nspots = -1;
		   } // column 2
		   else if (ticTac[0][1] == ticTac[1][1] && ticTac[1][1] == ticTac[2][1] && (ticTac[0][1] != BLANK))
		   {
			   cout << "YOU ARE THE WINNER!";
			   nspots = -1;
		   } // column 3
		   else if (ticTac[0][2] == ticTac[1][2] && ticTac[1][2] == ticTac[2][2] && (ticTac[0][2] != BLANK))
		   {
				cout << "YOU ARE THE WINNER!";
				nspots = -1;
		   }
		   //check for winner diagnally
		   else if (ticTac[2][0] == ticTac[1][1] && ticTac[1][1] == ticTac[0][2] && (ticTac[2][0] != BLANK))
		   {
				cout << "YOU ARE THE WINNER!";
				nspots = -1;
		   }
		   else if (ticTac[0][0] == ticTac[1][1] && ticTac[1][1] == ticTac[2][2] && (ticTac[0][0] != BLANK))
		   {
				cout << "YOU ARE THE WINNER!";
				nspots = -1;
		   }
			//check rows for winner  // row 1
		   else if (ticTac[0][0] == ticTac[0][1] && ticTac[0][1] == ticTac[0][2] && (ticTac[0][0] != BLANK))
		   {
				cout << "YOU ARE THE WINNER!";
				nspots = -1;
		   } // row 2 
		   else if (ticTac[1][0] == ticTac[1][1] && ticTac[1][1] == ticTac[1][2] && (ticTac[1][0] != BLANK))
		   {
				cout << "YOU ARE THE WINNER!";
				nspots = -1;
		   } // row 3
		   else if (ticTac[2][0] == ticTac[2][1] && ticTac[2][1] == ticTac[2][2] && (ticTac[2][0] != BLANK))
		   {
				cout << "YOU ARE THE WINNER!";
				nspots = -1;
		   }
}

void checkToBlock(string ticTac[][column])
{
	for (int i = 0; i < row; i++)
	{
		int nx = 0;
		for(int j = 0; j < column; j++)
		{
			if (ticTac[i][j]=="X")
			nx++;
		}
		if(nx==2)
		{
			if(ticTac[i][0]==BLANK)
			{
				ticTac[i][0] = "O";
			}
			else if(ticTac[i][1]==BLANK)
			{
				ticTac[i][1]="O";
			}
			else if(ticTac[i][2]==BLANK)
			{
				ticTac[i][2]="O";
			}
		}
	}
	for (int j = 0; j < column; j++)
	{
		int nx = 0;
		for(int i = 0; i < row; i++)
		{
			if (ticTac[i][j]=="X")
			nx++;
		}
		if(nx==2)
		{
			if(ticTac[0][j]==BLANK)
			{
				ticTac[0][j] = "O";
			}
			else if(ticTac[1][j]==BLANK)
			{
				ticTac[1][j] = "O";
			}
			else if(ticTac[2][j]==BLANK)
			{
				ticTac[2][j] = "O";
			}
		}
	}
	 if(ticTac[0][0]=="X" && ticTac[1][1] == "X")
	{
		ticTac[2][2] = "O";
	}
	else if(ticTac[1][1]=="X" && ticTac[2][2] == "X")
	{
		ticTac[0][0] = "O";
	}
	else if(ticTac[0][0]=="X" && ticTac[2][2] == "X")
	{
		ticTac[1][1] = "O";
	}

	else if(ticTac[2][0]=="X" && ticTac[1][1] == "X")
	{
		ticTac[0][2] = "O";
	}
	else if(ticTac[1][1]=="X" && ticTac[0][2] == "X")
	{
		ticTac[2][0] = "O";
	}
	else if(ticTac[2][0]=="X" && ticTac[0][2] == "X")
	{
		ticTac[1][1] = "O";
	}


}
void checkTowin(string ticTac[][column])
{
	for (int i = 0; i < row; i++)
	{
		int no = 0;
		for(int j = 0; j < column; j++)
		{
			if (ticTac[i][j]=="O")
			no++;
		}
		if(no==2)
		{
			if(ticTac[i][0]==BLANK)
			{
				ticTac[i][0] = "O";
			}
			else if(ticTac[i][1]==BLANK)
			{
				ticTac[i][1]="O";
			}
			else if(ticTac[i][2]==BLANK)
			{
				ticTac[i][2]="O";
			}
		}
	}
	for (int j = 0; j < column; j++)
	{
		int no = 0;
		for(int i = 0; i < row; i++)
		{
			if (ticTac[i][j]=="O")
			no++;
		}
		if(no==2)
		{
			if(ticTac[0][j]==BLANK)
			{
				ticTac[0][j] = "O";
			}
			else if(ticTac[1][j]==BLANK)
			{
				ticTac[1][j] = "O";
			}
			else if(ticTac[2][j]==BLANK)
			{
				ticTac[2][j] = "O";
			}
		}
	}
	 if(ticTac[0][0]=="O" && ticTac[1][1] == "O")
	{
		ticTac[2][2] = "O";
	}
	else if(ticTac[1][1]=="O" && ticTac[2][2] == "O")
	{
		ticTac[0][0] = "O";
	}
	else if(ticTac[0][0]=="O" && ticTac[2][2] == "O")
	{
		ticTac[1][1] = "O";
	}

	else if(ticTac[2][0]=="O" && ticTac[1][1] == "O")
	{
		ticTac[0][2] = "O";
	}
	else if(ticTac[1][1]=="O" && ticTac[0][2] == "O")
	{
		ticTac[2][0] = "O";
	}
	else if(ticTac[2][0]=="O" && ticTac[0][2] == "O")
	{
		ticTac[1][1] = "O";
	}


}

thanks

Recommended Answers

All 4 Replies

I have this tic tac toe code, it`s almost working, can anybody take a look, I think the only thing wrong is in the else statement in main, but I`m not sure, I think I need to place the checkToWin and checkToBlock functions properly in main

What make you think that? Does it do something wrong? You need to explain the problem you see and not make us guess what's going on.

this program is blocking twice if there`s two winning possibilities, and it ends before the square are all filled
I thing the problem is in main
can you help me please

// Laelzio Mosca
// 12/09/09
// Tic Tac Toe game

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <iomanip>
using std::setw;

#include <string>
using std::string;

#include <cstdlib>
using std::rand;
using std::srand;

#include <ctime>
using std::time;

//global variables
const int row = 3; //size of array
const int column = 3; // size of array
string BLANK = " ";
string ticTac[row][column];

static int nspots = 9; // number of possible spots

void ticTacToe(const string ticTacToe[][column]); //Function prototypes
void player1(string ticTacToe[][column]);
void player2(string ticTac[][column]);
void generateNumber(string ticTac[][column]);
void checkWinner(string ticTac[][column]);
void checkToBlock(string ticTac[][column]);
void checkToWin(string ticTac[][column]);

// program execution begins
int main()
{
	srand( time(0));
	int numberOfPlayers;

	string ticTac[row][column] = {{BLANK,BLANK,BLANK}, {BLANK,BLANK,BLANK}, {BLANK,BLANK,BLANK}}; //number display for rows and columns 
	cout << "Welcome to the tic tac toe game "<<endl << endl; // welcome message
	cout << "Enter the number of players with a max of 2 and press enter: ";
	cin >> numberOfPlayers;
	if (numberOfPlayers == 2)
	{
		ticTacToe( ticTac ); // function call

			   while (nspots > 0) 	
				{		
					player1(ticTac);	
					checkWinner(ticTac);

					if (nspots < 0) break;
					if (nspots == 0) {
						cout << "DRAW" << endl;
						break;
					}
					player2(ticTac);

					checkWinner(ticTac);
					// the following should never happen
					if (nspots == 0) {
						cout << "DRAW" << endl;
						break;
					}
				//	cout << "nspots: " << nspots << endl;
				}
			   cout << endl;
			
			   fflush(stdin); // clear the buffer, and wait to exit.
			   cout << "\nPress enter to exit " << endl;
			   getc(stdin);
	}
	else
	{
		ticTacToe( ticTac ); // function call

			   while (nspots > 0) 	
				{		
					player1(ticTac);	
					checkWinner(ticTac);

					if (nspots < 0) break;
					if (nspots == 0)
					{
						cout << "DRAW" << endl;
						break;
					}
					if(nspots < 8)
					{
						checkToWin(ticTac);
						checkToBlock(ticTac);
						checkWinner(ticTac);
					}
					else
					{
						generateNumber(ticTac);	
						checkWinner(ticTac);
					}
					// the following should never happen
					if (nspots < 0) break;
					if (nspots == 0) {
						cout << "DRAW" << endl;
						break;
					}
				}
			   cout << endl;
			
			   fflush(stdin); // clear the buffer, and wait to exit.
			   cout << "\nPress enter to exit " << endl;
			   getc(stdin);
	}

	return 0;
}

//Function ticTacToe
void ticTacToe(const string ticTacToe[][column])
{
	cout << setw(5)<<"|"<<setw(5)<< "|" <<endl;

	//for loop-continuation condition and increment
	for (int i = 0; i < row; i ++)
	{
		for (int j = 0; j < column; j++)
			//check to output '|' only in between the numbers, not at the end
			if ( j < column - 1)
			{
				cout << setw(3)<< ticTacToe[i][j] << setw(2)<<"|";
			}
			else					
				cout << setw(3)<<ticTacToe[i][j];
							
			cout << endl;

			// check to make sure the horizontal line in only printed in between
			// the 3 rows, not on the bottom fo the picture
			if ( i < row -1)
			{
				cout << "____|____|____"<<endl;
				cout << setw(5) << "|" << setw(5) << "|" << endl;
			}
			else
				cout << setw(5) << "|" << setw(5) << "|" << endl << endl;		
			
	}
}

void player1(string ticTac[][column])
{
	int x = 0;
	int y = 0;
	string p1 = "X";

	cout << "Player one can enter your coordinates for X." << endl;
		cin >> x >> y ;
		x--, y--;
		while (x >= row || y >= column)
		{
			cout << "Invalit entry, try a number from 1 to 3.";
			cin >> x >> y;
			x--, y--;
		}
		if (ticTac[x][y] != BLANK)
		{
			cout << "That spot is taken." << "\nEnter different coordinates for 'X'." << endl;
			player1(ticTac);
		}
		else {
			ticTac[x][y] = p1;
			nspots--;
		}
		ticTacToe( ticTac );
		
}

void player2(string ticTac[][column])
{
	int x = 0;
	int y = 0;
	string p2 = "O";

	cout << "Player two can enter you coordinates for 'O'. ";
		cin >> x >> y;
	x--,y--;
		while  (x >= row || y >= column)
		{
			cout << "Invalit entry, try a number from 1 to 3.";
			cin >> x >> y;
			x--, y--;
		}
		if (ticTac[x][y] != BLANK)
		{
			cout << "That spot is taken." << "\nEnter different coordinates for 'O'." << endl;
			player2(ticTac);
		}
		else {
			ticTac[x][y] = p2;
			nspots--;
		}
		ticTacToe( ticTac );
}

void generateNumber(string ticTac[][column])
{

	int a; // number 1
	int b; // number 2
	string gn = "O";
	
	
	do {
		a = (rand() % 3); // generate number 1
	} while (a > 2);
	do {
		b = (rand() % 3); // generate number 2
	} while (b > 2);


		if (ticTac[a][b] != BLANK)
		{
			generateNumber(ticTac);
		}
		else
		{
			ticTac[a][b] = gn;	
			nspots--;
		}
	ticTacToe( ticTac );
}
void checkWinner(string ticTac[][column])
{
			//check columns for winner // column 1
		   if ((ticTac[0][0] == ticTac[1][0]) && (ticTac[1][0] == ticTac[2][0]) && (ticTac[0][0] != BLANK))
		   {
			   cout << "YOU ARE THE WINNER!";
			   nspots = -1;
		   } // column 2
		   else if (ticTac[0][1] == ticTac[1][1] && ticTac[1][1] == ticTac[2][1] && (ticTac[0][1] != BLANK))
		   {
			   cout << "YOU ARE THE WINNER!";
			   nspots = -1;
		   } // column 3
		   else if (ticTac[0][2] == ticTac[1][2] && ticTac[1][2] == ticTac[2][2] && (ticTac[0][2] != BLANK))
		   {
				cout << "YOU ARE THE WINNER!";
				nspots = -1;
		   }
		   //check for winner diagnally
		   else if (ticTac[2][0] == ticTac[1][1] && ticTac[1][1] == ticTac[0][2] && (ticTac[2][0] != BLANK))
		   {
				cout << "YOU ARE THE WINNER!";
				nspots = -1;
		   }
		   else if (ticTac[0][0] == ticTac[1][1] && ticTac[1][1] == ticTac[2][2] && (ticTac[0][0] != BLANK))
		   {
				cout << "YOU ARE THE WINNER!";
				nspots = -1;
		   }
			//check rows for winner  // row 1
		   else if (ticTac[0][0] == ticTac[0][1] && ticTac[0][1] == ticTac[0][2] && (ticTac[0][0] != BLANK))
		   {
				cout << "YOU ARE THE WINNER!";
				nspots = -1;
		   } // row 2 
		   else if (ticTac[1][0] == ticTac[1][1] && ticTac[1][1] == ticTac[1][2] && (ticTac[1][0] != BLANK))
		   {
				cout << "YOU ARE THE WINNER!";
				nspots = -1;
		   } // row 3
		   else if (ticTac[2][0] == ticTac[2][1] && ticTac[2][1] == ticTac[2][2] && (ticTac[2][0] != BLANK))
		   {
				cout << "YOU ARE THE WINNER!";
				nspots = -1;
		   }
}

void checkToBlock(string ticTac[][column])
{
	for (int i = 0; i < row; i++)
	{
		int nx = 0;
		for(int j = 0; j < column; j++)
		{
			if (ticTac[i][j]=="X")
			nx++;
		}
		if(nx==2)
		{
			if(ticTac[i][0]==BLANK)
			{
				ticTac[i][0] = "O";
			}
			else if(ticTac[i][1]==BLANK)
			{
				ticTac[i][1]="O";
			}
			else if(ticTac[i][2]==BLANK)
			{
				ticTac[i][2]="O";
			}
			break;
		}
	}
	for (int j = 0; j < column; j++)
	{
		int nx = 0;
		for(int i = 0; i < row; i++)
		{
			if (ticTac[i][j]=="X")
			nx++;
		}
		if(nx==2)
		{
			if(ticTac[0][j]==BLANK)
			{
				ticTac[0][j] = "O";
			}
			else if(ticTac[1][j]==BLANK)
			{
				ticTac[1][j] = "O";
			}
			else if(ticTac[2][j]==BLANK)
			{
				ticTac[2][j] = "O";
			}
			break;
		}
	}
	 if(ticTac[0][0]=="X" && ticTac[1][1] == "X")
	{
		ticTac[2][2] = "O";
	}
	else if(ticTac[1][1]=="X" && ticTac[2][2] == "X")
	{
		ticTac[0][0] = "O";
	}
	else if(ticTac[0][0]=="X" && ticTac[2][2] == "X")
	{
		ticTac[1][1] = "O";
	}

	else if(ticTac[2][0]=="X" && ticTac[1][1] == "X")
	{
		ticTac[0][2] = "O";
	}
	else if(ticTac[1][1]=="X" && ticTac[0][2] == "X")
	{
		ticTac[2][0] = "O";
	}
	else if(ticTac[2][0]=="X" && ticTac[0][2] == "X")
	{
		ticTac[1][1] = "O";
	}
	nspots--;
	ticTacToe( ticTac );
}
void checkToWin(string ticTac[][column])
{
	for (int i = 0; i < row; i++)
	{
		int no = 0;
		for(int j = 0; j < column; j++)
		{
			if (ticTac[i][j]=="O")
			no++;
		}
		if(no==2)
		{
			if(ticTac[i][0]==BLANK)
			{
				ticTac[i][0] = "O";
			}
			else if(ticTac[i][1]==BLANK)
			{
				ticTac[i][1]="O";
			}
			else if(ticTac[i][2]==BLANK)
			{
				ticTac[i][2]="O";
			}
			break;
		}
	}
	for (int j = 0; j < column; j++)
	{
		int no = 0;
		for(int i = 0; i < row; i++)
		{
			if (ticTac[i][j]=="O")
			no++;
		}
		if(no==2)
		{
			if(ticTac[0][j]==BLANK)
			{
				ticTac[0][j] = "O";
			}
			else if(ticTac[1][j]==BLANK)
			{
				ticTac[1][j] = "O";
			}
			else if(ticTac[2][j]==BLANK)
			{
				ticTac[2][j] = "O";
			}
			break;
		}
	}
	 if(ticTac[0][0]=="O" && ticTac[1][1] == "O")
	{
		ticTac[2][2] = "O";
	}
	else if(ticTac[1][1]=="O" && ticTac[2][2] == "O")
	{
		ticTac[0][0] = "O";
	}
	else if(ticTac[0][0]=="O" && ticTac[2][2] == "O")
	{
		ticTac[1][1] = "O";
	}

	else if(ticTac[2][0]=="O" && ticTac[1][1] == "O")
	{
		ticTac[0][2] = "O";
	}
	else if(ticTac[1][1]=="O" && ticTac[0][2] == "O")
	{
		ticTac[2][0] = "O";
	}
	else if(ticTac[2][0]=="O" && ticTac[0][2] == "O")
	{
		ticTac[1][1] = "O";
	}
	nspots--;
	ticTacToe( ticTac );
}

Is this

Player one can enter your coordinates for X
2 1
    |    |
  X |  X |  O
____|____|____
    |    |
  X |  X |
____|____|____
    |    |
    |  O |  O
    |    |

N spots 0
DRAW

the condition you are talking about?

Problem is right in here in main:

if(nspots < 8)
{
	checkToWin(ticTac);
	checkToBlock(ticTac);
	checkWinner(ticTac);
					
}

Both checkToWin and checkToBlock make a move (there was some untangling to figure out which was in fact deducting from nspots). You need to call one or the other but not both. So when N should be 2, it was zero if it took the wrong sequence of events.
My suggestion would be to return a boolean from checkToWin if it "succeeds" and you could use that to a) skip over the call to checkToBlock and b) end your while loop in main() and declare a winner and if not call checkToBlock.
Or give checkWinner a more instrumental role in interrupting your loop if you find a winner and just call that in between checkToWin/checkToBlock. It seems like in the first code you put up you did something similar to that.
Even though it's late in the game (and I've seen the evolution of this program, you have put in a great effort) be thinking about ways to better arrange your functions so that you're not duplicating the work for yourself over and over.

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.