So far, here's my code. It is running after I built it but it runs differently that I typically supposed to play tic tac toe with a partner ( two players only). Where do I go wrong on this code?

//Timing.
#include<iostream>
using namespace std;

int check( char g[3][3], int r, int c, int p)
{
	if ( g[r][c] !='*')
	{
		cout << "\n\tSorry, wrong entry.";
		return 0;
	}

	char ch;
	if (p == 0)
		ch= 'X';

	if (p == 1)
		ch= 'O';

	g[r][c] = ch;
	
	cout << "\n\n";
	for ( int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++ )
			cout << "   " << g[i][j];

		cout << "\n";
	}

	if(//Row Section
		(( g [0][0]==ch) && (g[0][1]==ch)
		&& (g[0][2]==ch)) || ((g[1][0]==ch)
		&& (g[1][1]==ch) && (g[1][2]==ch))
		||((g[2][0]==ch) && (g[2][1]==ch)
		&& (g[2][2]==ch))||
		//Column Section
		((g[0][0]==ch) && (g[1][0]==ch)
		&& (g[2][0]==ch))|| ((g[0][1]==ch)
		&& (g[1][1]==ch) && (g[2][1]==ch))||
		((g[0][2]==ch)&& (g[1][2]==ch)&&
						(g[2][2]==ch))||
		//Diagonal Sections 
		((g[0][0]==ch)&&(g[1][1]==ch)&&
		(g[2][2]==ch))||((g[0][2]==ch)&&
			(g[1][1]==ch)&& (g[2][0]==ch))
	)
		return -1;

	return -1;
}
//Main Execution 
int main( )
{
	char game[3][3];
	int row, col;
	int count=0;
	int result;
	int end;

	cout << "\t\t\tTic-Tac-Toe Game\n";

	for (int i = 0; i < 3; i++)
		for (int j = 0; j < 3; j++)
			game [i][j]='*';
			
	do
	{
		cout << "\n\n\tPlayer " << (( count % 2));
		cout << "\n\tEnter row:  ";
		cin >> row;

			cout << "\tEnter column:  ";
			cin >> col;

		//Play!
			result=check(game, row-1, col-1, count % 2);

			if ( result == -1)
			{
				cout << "\n\n\t Player " << (( count % 2) + 1)
					<< "Wins! Congratulaions =) \n";
				break;

		}
	}
	while (end == 0);

	return 0;
}

Recommended Answers

All 5 Replies

Your check function only returns -1 no matter what. Is that what you want?

Your check function only returns -1 no matter what. Is that what you want?

Not really sure what you mean but is that why I have that? So, taking off return -1 would run and play with two players? Then if I take off that, it would have c2059 on the end of the code. To solve that, what would be a safer way to end the code effectively and efficiently?

Not really sure what you mean but is that why I have that? So, taking off return -1 would run and play with two players? Then if I take off that, it would have c2059 on the end of the code. To solve that, what would be a safer way to end the code effectively and efficiently?

You have a function called "check". What is it checking? Right now it always returns -1, which presumably means the game is over:

if ( result == -1)
			{
				cout << "\n\n\t Player " << (( count % 2) + 1)
					<< "Wins! Congratulaions =) \n";
				break;

		}

Hence the game is considered over before it actually really is over. I see four possible game states after a move, so pick a number for each.

  1. Cat's game.
  2. Player 1 wins.
  3. Player 2 wins.
  4. No winner yet. Game still in progress.

"check" should determine which state the game is in and return that number. For states 1 to 3 above, the game is over. Display a message saying so and saying who won based on the return value.

For state 4, the game continues, so ask the next player to move, then call "check" again.

So do at least two things:

  1. Change "check" so that it will return one of four numbers based on what the game's state is.
  2. In "main" check that return type and act accordingly. If the game is over, display a message stating that and stating who won. If not, continue the game by prompting for the next move.

other than that, anything else further errors

Other than that, you should check to make sure that the user enters valid row and column values (1 to 3), plus your "count" variable never changes, so it isn't a variable, it's a constant. Either make it vary if it is supposed to vary or make it a constant, but if you have code that expects "count" to change, it isn't going to work because "count" doesn't change.

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.