# Creating an AI Which Plays Pool

In this blog post we will discuss how to develop an AI which can play pool against an opponent. For simplicity we will start with a simple AI and then modify it into a more sophisticated AI.

To create a simple AI the steps are as follows:

1. Find the distance between each ball and its closest pocket.
2. Find the ball that lies closest to a pocket.
3. Point the cue straight towards the center of the ball and hit it.

As we can see, the above AI has a big disadvantage and is not very optimal, in that it cannot aim the ball towards the pocket - it can only hit the ball straight-on. To make this AI more sophisticated, we need it to aim at a pocket.

In the previous post we found that when the cue ball hits a ball at rest, the second ball will move in the direction which separates their centers. Thus if we want the ball to move towards the pocket, we need the centers to align with the displacement vector which points to the pocket. This can be accomplished by finding the displacement vector from the pocket to the ball in question, and aiming for a spot along that vector. Note that when the cue ball and the ball are colliding, they will be exactly one ball diameter apart, so the point we want to aim for is along the ball-to-pocket displacement vector, and separated from the ball by a full diameter.

To create a more sophisticated AI the steps are as follows:

1. Proceed the same as before by creating a function that finds the ball that is closest to a pocket.
2. Find the closest pocket.
3. Find the displacement vector from the center of the pocket to the center of the ball and extend it by one ball diameter.
4. Aim the center of the cue ball at the point found in 3. (See picture below.)