One rainy afternoon, you sit at the kitchen table playing cards with your grandmother. The game is her take on Camicia.
At first it feels like just another friendly match: cards slapped down, laughter across the table, the occasional victorious grin from Nonna. But as the game stretches on, something strange happens. The same cards keep cycling back. You play card after card, yet the end never seems to come.
You start to wonder. Will this game ever finish? Or could we keep playing forever?
Later, driven by curiosity, you search online and to your surprise you discover that what happened wasn't just bad luck. You and your grandmother may have stumbled upon one of the longest possible sequences! Suddenly, you're hooked. What began as a casual game has turned into a quest: how long can such a game really last? Can you find a sequence even longer than the one you played at the kitchen table? Perhaps even long enough to set a new world record?
And so, armed with nothing but a deck of cards and some algorithmic ingenuity, you decide to investigate...
In this exercise, you will simulate a game very similar to the classic card game Camicia. Your program will receive the initial configuration of two players' decks and must simulate the game until it ends (or detect that it will never end).
A small example of a match that ends.
| Round | Player A | Player B | Pile | Penalty Due |
|---|---|---|---|---|
| 1 | 2 A 7 8 Q 10 | 3 4 5 6 K 9 J | - | |
| 1 | A 7 8 Q 10 | 3 4 5 6 K 9 J | 2 | - |
| 1 | A 7 8 Q 10 | 4 5 6 K 9 J | 2 3 | - |
| 1 | 7 8 Q 10 | 4 5 6 K 9 J | 2 3 A | Player B: 4 |
| 1 | 7 8 Q 10 | 5 6 K 9 J | 2 3 A 4 | Player B: 3 |
| 1 | 7 8 Q 10 | 6 K 9 J | 2 3 A 4 5 | Player B: 2 |
| 1 | 7 8 Q 10 | K 9 J | 2 3 A 4 5 6 | Player B: 1 |
| 1 | 7 8 Q 10 | 9 J | 2 3 A 4 5 6 K | Player A: 3 |
| 1 | 8 Q 10 | 9 J | 2 3 A 4 5 6 K 7 | Player A: 2 |
| 1 | Q 10 | 9 J | 2 3 A 4 5 6 K 7 8 | Player A: 1 |
| 1 | 10 | 9 J | 2 3 A 4 5 6 K 7 8 Q | Player B: 2 |
| 1 | 10 | J | 2 3 A 4 5 6 K 7 8 Q 9 | Player B: 1 |
| 1 | 10 | - | 2 3 A 4 5 6 K 7 8 Q 9 J | Player A: 1 |
| 1 | - | - | 2 3 A 4 5 6 K 7 8 Q 9 J 10 | - |
| 2 | - | 2 3 A 4 5 6 K 7 8 Q 9 J 10 | - | - |
status: "finished", cards: 13, tricks: 1
This is a small example of a match that loops.
| Round | Player A | Player B | Pile | Penalty Due |
|---|---|---|---|---|
| 1 | J 2 3 | 4 J 5 | - | - |
| 1 | 2 3 | 4 J 5 | J | Player B: 1 |
| 1 | 2 3 | J 5 | J 4 | - |
| 2 | 2 3 J 4 | J 5 | - | - |
| 2 | 3 J 4 | J 5 | 2 | - |
| 2 | 3 J 4 | 5 | 2 J | Player A: 1 |
| 2 | J 4 | 5 | 2 J 3 | - |
| 3 | J 4 | 5 2 J 3 | - | - |
| 3 | J 4 | 2 J 3 | 5 | - |
| 3 | 4 | 2 J 3 | 5 J | Player B: 1 |
| 3 | 4 | J 3 | 5 J 2 | - |
| 4 | 4 5 J 2 | J 3 | - | - |
The start of round 4 matches the start of round 2. Recall, the value of the number cards does not matter.
status: "loop", cards: 8, tricks: 3
"finished" or "loop"
For those who want to take on a more exciting challenge, the hunt for other records for the longest game with an end is still open. There are 653,534,134,886,878,245,000 (approximately 654 quintillion) possibilities, and we haven't calculated them all yet!
Sign up to Exercism to learn and master Python with 17 concepts146 exercises, and real human mentoring, all for free.