• Articles
  • Animated card game (Rummy), user vs. PC
Dec 11, 2012 (last update: Dec 11, 2012)

Animated card game (Rummy), user vs. PC

Score: 4.0/5 (52 votes)
*****
This is another game which grew out of my effort to develop drag and drop objects for use in

games, or as a gui element. The first was checkers. See that article in source codes here.

I will offer a description of the gameplay first.
A description of the way I implemented the computers plays will follow.

The game begins with an animated dealing of cards to both hands. The computers hand is visible to enable effective program testing. If you are playing, try to resist the urge to cheat by looking at your opponents hand!

The user always starts the game by drawing from either the discard pile or the draw pile.
Simply click on the pile. A card will be dealt (animated).

Next, user makes a play if he can, then discards one card to the discard pile.
Cards to be played/discarded are selected by clicking on the card. A blue stripe will appear along the left edge of each card selected. To unselect a card just click on it a 2nd time (selection toggles).

To make a play after selecting cards simply click on an empty area and the cards will go there. Be carefull where you put them. They can wind up in the way of a lengthy discard pile, or your own hand if it gets long.All "piles" (ie draw, discard and both hands) are displayed "fanned out", so a large pile will take a lot of room.

To discard, select a single card in your hand, then click on the discard pile. An alert will appear if you have more than one card selected.

To draw multiple cards from the discard pile click on the deepest card in the pile that you wish to take. Note: There is a shortcoming here. If a player draws from the discard pile he must make a play and that play must use the bottom card drawn. The program does not check for this.

The computer will take its turn automatically once you have ended your turn by discarding (always required). There are some animation pauses built in so that there is time to see the

computer make a draw, then select cards, then make a play then finally discard.

Screenshot during play:
Each hand ends when a player discards his last card. Stats for the hand are shown:

The user clicks on the deck to start the next hand. The player ending the previous hand starts the next hand.

The game goes to 500 points. All scoring is done automatically.

*** Some description of the AI involved here ***

The computer( aka C) first examines the discard pile for any plays it could make using those.
Flaw: C will draw down to the bottom card for any play. This is poor strategy.

If C finds no play can be made from the cards in the discard pile it will draw.
C then makes any and all plays it can.
C does use some discretion in discarding. Point values are assigned to cards based on whether there is another of the same face value (save pairs) or suit (potential 3-kind) and whether

discarding a card will make a play in the discard pile. C keeps the hand with the highest of this type of score.

Several functions are used to support the computers plays. These are:

Main function:
bool c_findPlay( const discardStack* p_ds = NULL );

calls these functions:
int c_scorePlay( vector<sf_pair>& handVec );
bool c_find_3kind( vector<sf_pair>& handVec );
bool c_find_3straight( vector<sf_pair>& handVec );
bool c_find_addToExist( vector<sf_pair>& handVec );

bool c_makePlay(void);// return assigned to c_start
bool c_drawCards(void);// return assigned to c_start
bool c_discard(void);// return assigned to c_start

Finally, a link to a zip file containing all source files, images and an executable.
Unfortunately, the zip file would not upload with the required dlls in it (too big).
The reader will have to download these dll files from sfml.org (these are for v 1.6)
libgcc_s_dw2-1.dll
libstdc++-6.dll
sfml-system.dll
sfml-graphics.dll
sfml-window.dll

Attachments: [cards.zip]