This article presents a simple game card, programmed with MFC. For graphics and user feedback, and at a lower level, I used STL of C++ to manage the classes for the deck, the players, and the game. The same low level structure could be reused for a different future game (Poker? Bridge...is too difficult I guess!). I used a dialog based application in the MFC wizard. I've updated the game to version 2.0 because I added some other features. The game is a freeware and a full version is available with all the source code in C++ and MFC.
The Italian game card "Sette e mezzo" or "Seven and half" is a typical Christmas game. You have to bet your "virtual" dollars and then take a card from the deck until you feel that your points are enough and under seven and half, that is the maximum allowed point. Then it is the PC's turn to play and it will take cards until it will arrive over your points or until it will lose, because over seven and half! The rules are explained in the game with the Help dialog box. Your first card is hidden so the PC can only count the points by the other cards and you can also use only one card to try to beat the PC that will "learn" from your style of playing!
I used two classes for the MFC dialog boxes and classes for the lower level C++/STL engine.
CDeck for the deck of cards,
CGameCard for the 40 cards,
CPlayer for the two players: you and your PC, and
GameNapoletano for the whole game.
I used a function to load .jpg and .bmp from files and to render them to the DC:
void myUtilities::LoadPictureFile(LPCTSTR szFile, LPPICTURE &gpPic)
And this function uses the OLE technology.
To map a card's picture with the key code that identifies the card, I used
std::map. For each card, I built a key code using its value and its family (there are 40 cards, 10 cards, and 4 families).
typedef std::map<int, LPPICTURE, std::less<int> > PicCardType;
So, in the
OnPaint function of the main dialog box, I show all the current cards in the game, using a
std::vector, where I pushed the pointer to the picture card:
Before that, every random card taken from the deck has to be found in the map already built, by its key code.
int idCard = card.GetIdCard();
m_itMyCard = m_CardMap.find(idCard);
When the 40 cards are all gone, I have to reshuffle the deck but I cannot use the card still on the table! So I have to use another deck where I copied all the cards used and reshuffle that one...
New features for version 2.0
I added some features suggested to me by many players: first of all, the game needed to follow the official rules of the game, so I added the jolly card that is the king of gold, and I added to the first hidden card to the player, so the PC now cannot count all the points and it is more difficult to beat us! There are sounds for every action and it is possible to disable them. Bets are only allowed after we get the hidden card.
I wrote this code as a hobby. The next step will be a poker card game, hopefully reusing the same structure and inheriting from the same classes... Feel free to suggest your code, and your opinion is always appreciated. Tell me where I have to change, where the code really sucks, and what you would do instead.