In this programming challenge, you will create a program to rank hands of cards according to the rules of Poker. In the game of Poker, like in many other, players have to build simple patterns: four identical cards, three identical cards, contiguous increasing series of cards, etc. Computers can be programmed to recognise those patterns. And it is your challenge to make such a program!
Simple rules, simple program
There are several rules for the game of Poker, but the ranking of hands is always the same: five of a kind, straight flush, four of a kind, full house, etc. You can check the Wikipedia page on the topic for full details.
For simplicity, you can ignore wild cards and use a 52 cards deck. This excludes the “five of a kind” hand.
The challenge for you is to write a single function that compares two hands of five cards. Check the replies below for skeleton code and hints on where to start.
Suits in Unicode
The four suits of a game of card – clubs, diamond, hearts, and spades – are common markers, for cards and other things. So much so that they’ve been added to the Unicode character set. Depending on the fonts installed on your system, these characters might not display correctly: , , , .
Unicode is a collection of character that aims to be universal and to contain all the characters of most writing systems. Unicode also contains many symbols from mathematics and music notations. And games: the suits of cards, the chess pieces, the 6-sided dice, etc.
Unicode is important because it helps everyone use computers – without having to learn a new language.
Unicode and UTF-8
Unicode is a list of characters. The Unicode standard is a big list of entries that all look like so: “the character number 26149 is ‘春’.” This is useful if you want to wish people a Happy Chinese New Year (春节).
However, it is not practical to tell people to look up that gigantic table that contains over 136,000 characters. Instead, you can type the characters on your keyboard, or at least copy-paste them from the Internet. This works because your computer encodes the number corresponding to each character into a series of 0s and 1s.
A common way to encode Unicode characters into a series of 0s and 1s is UTF-8. In the UTF-8 encoding, the 26149th character, “春”, is encoded as 111001101001100010100101. Fortunately, you do not need to read all those numbers because your computer can simply display them on your screen… provided you have the necessary fonts installed.
(Note that the entries in the Unicode character set have a bit more information than just the number and character. For example, it indicates the type of character: a number, a letter, a diacritic, a symbol, an emoticon, etc.)
Other pattern games
If poker is not your thing, or if you finish the challenge quickly and want to do more, you can try to make programs that recognise the patterns of the following games.
Yatzee is a game in which players build patterns of dice rolls: four of a kind, five of a kind, contiguous increasing sequence, etc. You can find the scoring rules on https://en.wikipedia.org/wiki/Yahtzee#Rules
Mahjong is a game in which players build patterns of tiles: three of a kind, four of a kind, contiguous increasing sequence, etc. You can find the scoring rules on https://en.wikipedia.org/wiki/Mahjong#Legal_hand