Jump to content
  • Advertisement
Sign in to follow this  
Chrono1081

Why isnt this working?

This topic is 3901 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have code for an already finished homework assignment that im adding some embellishments to, namely some extra error correction. Its a tic tac toe game and I discovered that you can accidentally rewrite over someone elses X or O. I thought the error correction would be easy, boy was I wrong. Here is the error correcting method:
[source lang = "cpp"]
void TicTacToe::spaceCheck(int row, int column, bool &spaceAvailable)
{
	if(theBoard[row][column] == ' ')
	{
		spaceAvailable = true;
	}

	else
	{
		cout<<"\nThat space is already taken!\n";
		spaceAvailable = false;
	}
}

That is the method that checks to see if the space is available. Here is the method that calls it.
[source lang = "cpp"]
void TicTacToe::playOneGame(void)
{
	bool spaceAvailable = NULL;
	char currentPlayer  = 'O';
	char theWinner      = ' ';
	const int MaxMoves  = 9;
	int row             = 0;
	int column          = 0;
	int numberOfMoves   = 0;  //keeps track of number of moves, max is 9

	do{
		switchPlayer(currentPlayer);
		showBoard();
		do
		{
			cout<<"\nPlayer "<<currentPlayer<<endl;
			playerInput(row, "row");
			playerInput(column, "column");
			spaceCheck(row,column, spaceAvailable);
			cout<<endl;
		}while(spaceAvailable != true);

		postMove(row-1,column-1,currentPlayer); //post the move to the board
		theWinner = determineWinner(); //see if anyone won the game
		numberOfMoves++; //keep track of the number of moves
	}while((theWinner == 'D') && (numberOfMoves < MaxMoves));

	showBoard(); //shows the ending board

I dont understand why my spaceCheck method isnt catching the error. It looks correct to me but obviously it isnt : /

Share this post


Link to post
Share on other sites
Advertisement
You have:
postMove(row-1,column-1,currentPlayer);
But you don't subtract 1 from the row and column when calling spaceCheck(). Is that intentional?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!