Monday, October 10, 2016

Gin Rummy Code Report

When making a Gin Rummy AI, there are two ways to go, make it simple or make it complex. I chose the latter. There are really only two choices to make, what pile to pick up from and what card to discard. It is hard to test to see how good the AI actually runs because the default AI just throws out the highest value card. Although my AI did beat it.

The AI algorithm I used was very similar to the one I mentioned in the planning post. The first decision that the AI has to make is what pile to pick up from. Using the knowledge of the top card in the discard pile. It can make the intended choice. In my algorithm my AI will only pick up from the discard pile. If the card will complete or continue a completed run/set. This way if the opponent is trying to use probability on my hard. It would be useless. It is also better to pick from the deck instead of the discard pile. It could give you and your opponent potentially more helpful cards. These cards are unknown unlike all the known cards of the discard pile

The second decision my AI has to make is what card to discard. It am utilizing a vector to keep track of all the cards in the discard pile. This way it can make logical decisions. It won't try to keep two kings if the other two are in the discard pile. This vector is the most important part of my discard algorithm. 

When it calls the function to decide a card to discard. It removes all the completed sets/runs first. Then it checks to see if it has two cards of a set. If it has two cards it checks to make sure the other needed cards aren't in the discard pile. If they are, it adds the cards to the potential discard vector. If they aren't in the discard pile. It adds the cards to the two card set vector. When it is finished going through all the cards in the hand, excluding completed runs/sets, it removes the two card set vector from its hand. It then go through the same process looking for runs. It is a little bit different because the AI has to see if the card it is missing is in the front, middle or end. Once it runs through all of the cards in the hand,excluding completed runs/sets, it removes the valid two card runs from its hand. 

Then it picks the card with the highest point value from the remaining cards. If there is no remaining cards left in the hand after removing the completed run/sets and the almost completed run/sets. Then it picks the highest value from the potential discard vector. If that vector has zero elements. Then it checks the two cards run/sets vectors. If those fail, the AI probably has gin rummy but my AI knocks as soon as the total hand score is less than 10.