Below is my source code for a tictactoe program. However, my output after first move always states invalid choice, no matter what number I enter?

#include <iostream>
#include <string>
#include <cmath>

using namespace std;

class TicTacToe
{
	public:
		TicTacToe();    // constructor
		int Pick_Player();   // member function
		int Pick_Row();    // member functions
		int Pick_Column();  // member functions
		int Check_Board();  // member function
		void Choice_by_Player(int);  // member functions
		void Choice_of_Row(int);   // member functions
		void Choice_of_Column(int);  // member functions
		void Tic_Tac_Toe_Board();  // member functions
		bool Check_Move(int,int); // member functions
	private:
		int row;
		int column;
		int player;
		int board[3][3];
		char display_board[3][3];
};

TicTacToe::TicTacToe()//:row(0):column(0):player(1):board(0)(0):display_board(' ')(' ')
{
	row = 0;
	column = 0;
	player = 1;
	int i = 0;
	int j = 0;
	for ( i = 0; i < 3; i++)
	{
		for ( j = 0; j < 3; j++)
		{
			board[i][j] = 0;
			display_board[i][j] = ' ';
		}
	}	
}

int TicTacToe::Pick_Player()
{
	return player;
}

int TicTacToe::Pick_Row()
{
	return row;
}

int TicTacToe::Pick_Column()
{
	return column;
}

void TicTacToe::Choice_by_Player(int a)
{
	player = a;
}

void TicTacToe::Choice_of_Row(int b)
{
	row = b;
}

void TicTacToe::Choice_of_Column(int c)
{
	column = c;
}

bool TicTacToe::Check_Move(int row, int column)
{
	row = 0;
	column = 0;

	if ( row != 0  && row != 1 && row != 2 ) 
	{
		cout << " Invalid choice!!";
		cout << endl;
		return 0;
	}
	else if ( column != 0 && column != 1 && column != 2 )
	{
		cout << " Invalid choice!! " << endl;
		return 0;
	}
	else if ( board[row][column] == 1 || board[row][column] == 2)
	{
		cout << " Space already used. Try Again. " << endl;
		return 0;
	}
	else 
	{
		board[row][column] = player;
		return 1;
	}
}   // end of Check_Move

int TicTacToe::Check_Board()
{
	int i = 0;
	int j = 0;
	int sum = 0;
	int test = 0;
	int count = 0;

	for (i = 0; i < 3; i++)
	{
		sum = 0;
		for ( j = 0; j < 3; j++)
		{
			if (board[i][j] == 0)
			{
				count++;
			}	
			sum += (board[i][j] * board[i][j]);
		}

	 	if ( sum == 3 || sum == 12)
	 	{
			test = sum;
			break;
		}
		sum = 0;
	}    // end of for loop	

	for ( j = 0; j < 3; j++)
	{
		sum = 0;
		for ( i = 0; i < 3; i++)
		{
			sum += (board[i][j] * board[i][j]);
		}
		
		if ( sum == 3 || sum == 12)
		{
			test = sum;
			break;
		}
		
		sum = 0;
	}   // end of for loop
	
	if ( test != 3 || test != 12)
	{
		
			sum = (board[0][0] * board[0][0])+ (board[1][1] * board[1][1]) + (board[2][2] * board[2][2]);
		
		if ( sum == 3 || sum == 12)
		{
			test  = sum;
		
		}
	}  // end of if condition

	if (test != 3 || test != 12)
	{
		
			sum = (board[2][0] * board[2][0])+ (board[1][1] * board[1][1]) + (board[0][2] * board[0][2]);
			
			if ( sum == 3 || sum == 12 )
			{
				test = sum;
			}
	}  // end of if condition
		
//	}
	
	if ( test == 3)
	{
		test = 1;
	}
	
	else if ( test == 12)
	{
		test = 2;
	}
	
	else if ( count == 0)
	{
	 	test = 3;
	}
	else
	{
		test = 0;
	}
	return test;

} // end of Check_Board

void TicTacToe::Tic_Tac_Toe_Board()
{
	for ( int row = 0; row < 3; row ++)
	{

		for ( int column = 0; column < 3; column++)
		{

			if ( board[row][column] == 0)
			{
				display_board[row][column] = ' ';
			}
			if ( board[row][column] == 1)
			{
				display_board[row][column] = 'X';
			}
			if ( board[row][column] == 2)
			{
				display_board[row][column] = 'O';
			}
		}  // end of inner for loop
	}  // end of outer for loop

	cout << " 		Let's Play Tic-Tac-Toe!			" << endl;
	cout << "	Current Player: X		Current Player: O	" << endl;
	cout << endl;
	cout << "		|		|		"  << endl;
	cout << " 		|		|		"  << endl;
	cout << 				   display_board[0][0] << " 		|   "  << display_board[0][1]  <<  " 		| "  << display_board[0][2] << "   "  << endl;
	cout << "		|		|		" << endl;
	cout << "-----------------------------------------------" << endl;
	cout << "		|		|		" << endl;
	cout << "		|		|		" << endl;
	cout << 		  		   display_board[1][0] << " 		|   "  << display_board[1][1]  <<  " 		| "  << display_board[1][2] << "   "  << endl;
	cout << "		|		|		" << endl;
        cout << "-----------------------------------------------" << endl;
	cout << " 		|		|		" << endl;
	cout <<			  	       	display_board[2][0] << " 		|   "  << display_board[2][1]  <<  " 		| "  << display_board[2][2] << "   "  << endl;
 	cout << "		|		|		" << endl;
	cout << "		|		|		" << endl;

}   // end of Tic_Tac_Toe_Board




int main()
{


	TicTacToe game;
	bool test;
	bool more = true;
	int row = 0;
	int column= 0;
	int player;
	int check = 0;
	

	TicTacToe();
	
	while ( more )
	{
		game.Tic_Tac_Toe_Board();
		player = game.Pick_Player();
		

		cout << " Current Player " << player;
		cout << endl;
		cout << " Enter Row Index ( 0,1,2):  " ;
		cout << endl;
		cin >> row;
		cout << " Enter Column Index (0,1,2): " << endl;
		cin >> column;

		game.Choice_of_Row(row);
		game.Choice_of_Column(column);

		test = game.Check_Move( game.Pick_Row(), game.Pick_Column());

		if ( test == 1)
		{

			check = game.Check_Board();
		}


		else
		{
			while ( test == 0 ) 
			{
	
				cout << " Current Player " << game.Pick_Player() <<"  Invalid Choice" << endl;
				
				cout << " Enter Row Index ( 0,1,2): " ;
				
				cin >> row;
				cout << endl;
				cout << " Enter Column Index ( 0,1,2): " ;

				cin >> column;
				cout << endl;
				game.Choice_of_Row(row);
				game.Choice_of_Column(column);

				test = game.Check_Move(game.Pick_Row(),game.Pick_Column());
			} // end of while loop
			check = game.Check_Board();
		}
				
		if ( check == 1 || check == 2)
		{	
			break;
		}
		
		else if ( check == 3 ) 
		{
			game.Tic_Tac_Toe_Board();
			cout << " The game is tied. " << endl;
			
		}
		
		if ( player == 1)
		{
			player = 2;
		}
		else
		{
			player = 1;
		}
		game.Choice_by_Player(player);
		
	} // end of outer while loop

	game.Tic_Tac_Toe_Board();
		
	cout << " Player " << check << " wins. " << endl;
		
	return 0;
} // end of main function

                Let's Play Tic-Tac-Toe!
        Current Player: X               Current Player: O

                |               |
                |               |
                |               |
                |               |
-----------------------------------------------
                |               |
                |               |
                |               |
                |               |
-----------------------------------------------
                |               |
                |               |
                |               |
                |               |
 Current Player 1
 Enter Row Index ( 0,1,2):
1
 Enter Column Index (0,1,2):
1
                Let's Play Tic-Tac-Toe!
        Current Player: X               Current Player: O

                |               |
                |               |
X               |               |
                |               |
-----------------------------------------------
                |               |
                |               |
                |               |
                |               |
-----------------------------------------------
                |               |
                |               |
                |               |
                |               |
 Current Player 2
 Enter Row Index ( 0,1,2):
2
 Enter Column Index (0,1,2):
2
 Space already used. Try Again.
 Current Player 2  Invalid Choice
 Enter Row Index ( 0,1,2): 1

 Enter Column Index ( 0,1,2): 2

 Space already used. Try Again.
 Current Player 2  Invalid Choice
 Enter Row Index ( 0,1,2): 00

 Enter Column Index ( 0,1,2): 0

 Space already used. Try Again.
 Current Player 2  Invalid Choice
 Enter Row Index ( 0,1,2):

Recommended Answers

All 11 Replies

You have too many posts to be completely ignorant about code tags. Next time I won't bother adding them for you, and you can live with nobody helping because your code is unreadable. Or I might just delete the thread.

Well, I found out what code tags are so from now on I will make sure I include those. I also printed up my code and in the process of cleaning it up because I found that it was even hard for me to follow. So thanks for the input.

Peace.

hi hello im a friend seeking for some help in my C++ class. My assignment is to create a tic tac toe and i have no idea about how to start.

I searched over the internet and i found you're coding. It is very frustrating reading your coding and i think the real problem is the function which you declare as below... i think if you erase the
row=0;
column=0;
will help to solve your problem. Hope you get it but i think you're already aware about that.
Well, okay. Hope we can work together with our assignment... im from malaysia.. kindly reply if necessary... Peace

bool TicTacToe::Check_Move(int row, int column)
{
	 :o row = 0;
	 :o column = 0;

	if ( row != 0  && row != 1 && row != 2 ) 
	{
		cout << " Invalid choice!!";
		cout << endl;
		return 0;
	}
	else if ( column != 0 && column != 1 && column != 2 )
	{
		cout << " Invalid choice!! " << endl;
		return 0;
	}
	else if ( board[row][column] == 1 || board[row][column] == 2)
	{
		cout << " Space already used. Try Again. " << endl;
		return 0;
	}
	else 
	{
		board[row][column] = player;
		return 1;
	}
}   // end of Check_Move

<< moderator edit: added [code][/code] tags >>

You have too many posts to be completely ignorant about code tags. Next time I won't bother adding them for you, and you can live with nobody helping because your code is unreadable. Or I might just delete the thread.

Completely OFF-topic and I really don't care if you delete this or lock it: I think that Narue should stop being so insolent and thinking she is superior to others. And I also think that she should get the carrot out of her ass (as in is always angry and impatient). People don't think that a moderator should call any poster a retard for ANY reason. :mad: That's just why this forum is very unsuccesfull and I will just stop posting (I will be banned anyway).

So, just be nice: is it so hard?? And no, you are not a perfect person, in fact you should consider your language and I also think that you should no longer be a moderator. :evil:

Narue knows her stuff. She is very helpful. And I understand why she gets aggravated. The guy had 40 posts and..AND there is a thread at the top of the lists about code tags... so she has the right. And since she is so active and so helpful she deserves every bit of the moderator title.

>People don't think that a moderator should call any poster a retard for ANY reason.
Don't carry assumed insults into another thread please.

If you have a problem with me, take it up with me, in a private message, where such complaints belong. I welcome criticism, but not like this. I will not have you spamming the forum with silly flames just because you were insulted by something I said to you elsethread.

don't be impolite to narue

dcs_jr84 - You are right, that fixes the problem up :)

Let's Play Tic-Tac-Toe!
        Current Player: X               Current Player: O

                |               |
                |               |
X               |   X           |
                |               |
-----------------------------------------------
                |               |
                |               |
O               |   O           | O
                |               |
-----------------------------------------------
                |               |
X               |   O           | X
                |               |
                |               |
 Player 2 wins.

I am the WINNER !!

um, hello, how about making tictactoe using push and pops of arrays n stacks? it's what we need to do, pls help

Please don't resurrect ancient threads.

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.