Gin and Rummy AI – Code Report


It’s been a while since I spoke about this, but the due date for this assignment is finally up! My AI performed better than I expected it to, so I’ll take some time to explain my AI’s general strategy. If you’re interested in this, please refer to my previous post for a solid introduction on Gin Rummy:


I ended up using the value based system I discussed in my introduction post. The bulk of my AI took place in valuing each card I had in my hand. I first started by keeping a reference of the each card I currently have in my hand, while also assigning a parallel list of integers – with the purpose of storing a card’s value. The first step was to determine my current hand’s score. When determining a hand’s score, you could either prioritize matches or runs. My AI prioritizes whatever method gives my hand a lower score.

I then run through all the runs and matches. Any card that is part of set or match, gets a value of one (A card I won’t be getting rid of).  A card that forms part of a match receives a value of two. Any card that is standalone receives a value of three, plus whatever the value of the card is. Long story short, a garbage card with a high value will be the first to be discarded.


On the discard, I run my little algorithm to determine the card with the least purpose in my hand (the card with the highest value). I simply discard that card.


For drawing cards, I “fake” drawing the discard card into my hand and see if that card can be used in my hand to bring my overall score lower. If it can be used,  then I draw that card. If all other possibilities only make my score higher, then I take a chance and draw from the deck.


My AI completely obliterates the “dummy” AI we were supplied with to test against, and it seems to have been doing so-so in class. My AI has had a lot of close games with other students’ test AIs. I’ll probably go deeper into this into my post mortem, but there are two things off the top of my head that I could add to my valuing system.

The first, being that sometimes my algorithm causes my AI to hold on to two high valued cards, in an attempt to wait for that third card to come by. This is risky however, because if the opponent catches me in the state, I’ll be giving them a lot more points than I’d be wanting to. To remedy this, I’d take into account the amount of cards left in the deck, and change the value system so that higher valued cards that aren’t in a complete set are more likely to be discarded if the game has a higher chance of coming to an end soon.

The other change I’d want to make is card counting, and taking into account the possibility of drawing one last card to complete a set or run into my value system.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s