Gin Rummy Code Review

Starting off with my code I tried a very complicated set up of checking for runs or sets and keeping them all in vectors. The code started off fairly simply, but escalated fast. By the time the DLL was tested in class all it would do is check for definite things and only that.

A lot of the code required for loops and sorting the hand early on, then sorting it differently and adding it to another. The core function being the populateHand function. In this function you were taking the hand putting it into two vectors, one for sets and sorted by value and the other for runs sorted by suit and value.

populate-hand
Create Your Own Hand to use throughout

The runCount and setCount functions are checking through to see if there are sets and runs in the current hand. It will check the full hand each where runCount has a a slightly more involved aspect to it than setCount.

The runCount function starts by retrieving the suit then as it continues checking if the next card value minus the current card value is equal to one (excluding the starting card) assuming it is it will keep track of the positions. Once it is finished checking the hand for that suit it will add the run to the run vector, IF there is three or more. Pictures of these functions can be seen at the end of the post.

The setCount function worked relatively similarly, but focused only on the values being the same. After setCount ran checkOverlap would remove anything in the run from the sets, along with the rest of the set members if there was less than three after one removal. After that draw would be decided by whether the discard could be added to a run or set, if not it chose to draw from the deck.

 

After a bit of testing I realized an easier way to achieve a similar idea.

draw
Draw from Deck or Discard Decision

Changing draw to factor in if the score went down did exactly as I needed with less work, although I left the checks there to be safe should the score not go down for some reason. After implementing this I realized discard could be simplified as well.

discard
Pick Discard

This way it removes the card that will leave the score lowest. Although these methods are simple it is a solid way to have an AI play this game. At the end you can simplify the DLL aspect to needing solely a function to draw and discard focused on just score changes. Improvements could be made, but for now this works strangely well and can win at a massive difference between this score focused AI and one with more detail.

 

runcount
RunCount Function
setcount
Set Count Function
overlap
Check Overlap between Runs and Sets (Favoring runs)
Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s