TRING
A Modified Carom Billiards Game
Artificial Intelligence

The AI can be seen in Tring when the player selects '1' as the number of players. Player 2 is then a computer player. As the requirements state, the AI must be good enough to always hit a shot, which it is. Of course, this makes the game rather uninteresting, since after scoring, you get to shoot again. Once it's the computer's turn, it will eventually win. The AI is done in the following manner:

The basic idea is to test many angles until 1 works. It starts out looking in the direction of one of the other balls, then slowly changes the angle. If the search goes 360 degrees around and doesn't find a hit, the angle increment is decreased and the power of the shot is increased, then the search begins again. Because it works in this fashion, the computer will ALWAYS be able to score a point.

The relevant code can be seen below:

Vector Tring::aiShot(){

	GLfloat increment = .5f;
	GLfloat multiplier = 25.f;
	hitting = currentBallColor;
	while(true){

		int other = (hitting+1)%3;
		Vector hit = balls[other]->getPosition() - balls[hitting]->getPosition();
		
		hit.normalize();

		for(GLfloat a = 0 ; a < 360 ; a+=increment){		
	
			hit.rotate2(a, Vector(0, 0, 1));
			hit *= multiplier;
			hitLine = hit;

			if(ballPaths[0] != NULL){
				delete ballPaths[0];
				delete ballPaths[1];
				delete ballPaths[2];
				ballPaths[0] = NULL;
				ballPaths[1] = NULL;
				ballPaths[2] = NULL;
			}			

			hitting = currentBallColor;
			calcPaths();

			hitLine = hit;			

			if(scoreTime > 0)
				return(hit);
		}
		increment /= 2.f;
		multiplier *= 2;
		if(multiplier > 100)
			multiplier = 100;
	}	
}

navigate
about us
links