Problem with scoring system in tic-tac-toe

Started by
12 comments, last by tnutty 14 years, 11 months ago
I have everything working. The problem is exactly when this sequence occurs. Player 1 wins, and then Player1 looses. When this happens, the winning percentage does not output correctly. But when I debug it with Microsoft express it outputs fine. I am using c++/opengl/glut. Any clues on why this might be happening? And say, if its a draw or a lost first and a win or whatever next, then the program works fine. Any ideas on why or how this might be caused?
Our whole life is a opengl application.
Advertisement
It's caused by something in your code. We're not psychic; show the code.
I thought there might be a common mistake.

Here is the code for the winning percentage :
Note this code makes some assumptions like the player wont play more than 1000 games.

GLvoid Player::PrintWinprctg(){			float total = 0; 	//check if there is a division by 0. 	if(Player1.NumberOfLosses+Player1.NumberOfWins == 0.0f)	{		Player1.winPrctg[0] = 0;		Player1.winPrctg[1] = Player1.winPrctg[2] = 0;		strcpy(Player1.Rating,"INVALID");		return;	}	else 		total = Player1.NumberOfWins/(Player1.NumberOfLosses+Player1.NumberOfWins);	//if there are no loss then there is 100% win rate	if(total == 1)	{		Player1.winPrctg[0] = 1;		Player1.winPrctg[1] = Player1.winPrctg[2] = 0;		strcpy(Player1.Rating,"PERFECT");		return;	}	total*= 1000;	int copyTot = total;		//Take each integer and convert it into a char and put it into winPercentage array	for(int i=0;i<5;i++)	{		Player1.winPrctg = copyTot % 10 + '0';		copyTot/=10;			}	int cntr=0;	//reverse the order	for(int i=4;i>=0;i--)	{		if(Player1.winPrctg == '0' && Player1.winPrctg[i+1] != '0' && i<=4) ++cntr;		cout<<"res["<<i<<"]"<<" = "<< Player1.winPrctg<<endl;		 	}	if(cntr==2 || cntr == 1) //assumption made here, win percentage are over 1 percent		Player1.winPrctg[3] = '.';	for(int i=4;i>=0;i--)	{				cout<<"Player1.winPrctg["<<i<<"]"<<" = "<< Player1.winPrctg<<endl;			}}
Our whole life is a opengl application.
How about the code responsible for incrementing the number of wins. Did you check that?
I trust exceptions about as far as I can throw them.
You are really complicating your code. You appear to be mixing C and C++. In C++, we use std::string for text, not strcpy and char arrays. We also use library functions to convert between numbers and strings.

Here is a sample implementation. Note how the winPrctg isn't stored explicitly, we just use
#include <string>#include <sstream>class Player{	std::string rating;	// ...};// Why GLvoid and not simply "void"?void Player::PrintWinprctg(){	// Why are we referencing a *particular* player here?	// Why not the object the function was called on?	// Is the function "static"?	int wins = Player1.NumberOfWins;	int totalGames = wins + Player1.NumberOfLosses;	if(totalGames == 0)	{		Player1.rating = "INVALID";	}	else if(wins == totalGames)	{		Player1.rating = "PERFECT";	}	else	{		float percentage = (100.0f * wins) / totalGames; 		std::stringstream stream;		stream << "WINS: " << percentage;		Player1.rating = stream.str();	}}

The standard library type "stringstream" converts data to and from a string. You can access it by including <sstream>. It acts like any other stream you are used to (std::cin and std::cout), but it also has the handy "str()" function, which returns the current contents as a std::string instance (it can also be used to set the current contents).
I will look into sstream but for now, If I change the code it will complicate things. Here is the code for incrementing PlayerNumberWins. Its basically hard coded way to check for win.
bool MainGame::checkPlayerWin(){	if(Won[0] && Won[1] && Won[2]) {Player1.NumberOfWins++; whichState = stateEnd; p1Win = true; return true;} else//Check win top row across	if(Won[0] && Won[3] && Won[6]) {Player1.NumberOfWins++; whichState = stateEnd; p1Win = true; return true;} else//Check win top left row down	if(Won[0] && Won[4] && Won[8]) {Player1.NumberOfWins++; whichState = stateEnd; p1Win = true; return true;} else//Check top left diagonally	if(Won[1] && Won[4] && Won[7]) {Player1.NumberOfWins++; whichState = stateEnd; p1Win = true; return true;} else//Check middle row down	if(Won[2] && Won[5] && Won[8]) {Player1.NumberOfWins++; whichState = stateEnd; p1Win = true; return true;} else//Check Top right doing down	if(Won[2] && Won[4] && Won[6]) {Player1.NumberOfWins++; whichState = stateEnd; p1Win = true; return true;} else//Check right Diagnonal	if(Won[3] && Won[4] && Won[5]) {Player1.NumberOfWins++; whichState = stateEnd; p1Win = true; return true;} else//Check middle right	if(Won[6] && Won[7] && Won[8]) {Player1.NumberOfWins++; whichState = stateEnd; p1Win = true; return true;} //Check down right			 	return false;	}


Our whole life is a opengl application.
What happens in the case of a tie? Are NumberofWins and NumberofLosses both increased by .5?
I trust exceptions about as far as I can throw them.
NumberOfLosses and NumberOfWins don't happen to be integers, do they? Do you know what happens when you divide an integer by another integer?

If there is a tie the I have this function :
bool MainGame::checkIfGameisDraw(bool gameBrd[]){	int cntr =  0;	int i=0;	while(i<9)  //check if the game board is full 	{		if(cGame.gameBoard == Player1.playerChoice ||			cGame.gameBoard == cAI.AI_ID)			cntr++;		else break;		++i;	}	if(cntr == 9) //if everyhing is filled in the gameBorad.		if(!(cGame.checkCompWin(gameBrd) || cGame.checkPlayerWin() )) //check if player or comp has won		{			DrawGame = true; 			whichState = stateEnd;			Player1.NumberOfDrawGames++;			return true;				}		return false;}



quote : NumberOfLosses and NumberOfWins don't happen to be integers, do they? Do you know what happens when you divide an integer by another integer?

Not sure what you are saying, but it gets truncated, if thats what you mean.
Our whole life is a opengl application.
Thanks. I found the problem. For some reason the code was not updating the
winning percentage. Not sure why because it in correct order. Whatever. Thanks
guys.Almost done!
Our whole life is a opengl application.

This topic is closed to new replies.

Advertisement