Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1103,6 +1103,14 @@
"prerequisites": [],
"difficulty": 5
},
{
"slug": "camicia",
"name": "Camicia",
"uuid": "7aaaf5fa-1a22-433b-9c0d-0e1c035eba3d",
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "rectangles",
"name": "Rectangles",
Expand Down
84 changes: 84 additions & 0 deletions exercises/practice/camicia/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Instructions

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).

## Rules

- The deck is split between **two players**.
The player's cards are read from left to right, where the leftmost card is the top of the deck.
- A round consists of both players playing at least one card.
- Players take turns placing the **top card** of their deck onto a central pile.
- If the card is a **number card** (2-10), play simply passes to the other player.
- If the card is a **payment card**, a penalty must be paid:
- **J** → opponent must pay 1 card
- **Q** → opponent must pay 2 cards
- **K** → opponent must pay 3 cards
- **A** → opponent must pay 4 cards
- If the player paying a penalty reveals another payment card, that player stops paying the penalty.
The other player must then pay a penalty based on the new payment card.
- If the penalty is fully paid without interruption, the player who placed the **last payment card** collects the central pile and places it at the bottom of their deck.
That player then starts the next round.
- If a player runs out of cards and is unable to play a card (either while paying a penalty or when it is their turn), the other player collects the central pile.
- The moment when a player collects cards from the central pile is called a **trick**.
- If a player has all the cards in their possession after a trick, the game **ends**.
- The game **enters a loop** as soon as the decks are identical to what they were earlier during the game, **not** counting number cards!

## Examples

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

## Your Task

- Using the input, simulate the game following the rules above.
- Determine the following information regarding the game:
- **Status**: `"finished"` or `"loop"`
- **Cards**: total number of cards played throughout the game
- **Tricks**: number of times the central pile was collected

~~~~exercism/advanced
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!
~~~~
24 changes: 24 additions & 0 deletions exercises/practice/camicia/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Introduction

One rainy afternoon, you sit at the kitchen table playing cards with your grandmother.
The game is her take on [Camicia][bmn].

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...

[bmn]: https://en.wikipedia.org/wiki/Beggar-my-neighbour
130 changes: 130 additions & 0 deletions exercises/practice/camicia/.meta/Camicia.example.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
using namespace System.Collections.Generic
using namespace System.Text

enum Status {
Finished
Loop
}

Function Invoke-Camicia() {
<#
.SYNOPSIS
Simulate a game very similar to the classic card game Camicia.

.DESCRIPTION
Given two hands of cards, simulate a game (similar to Camicia) until it ends (or detect if it is in a loop).
Read instruction for rules and game example.

.PARAMETER PlayerA
An array of string(s) represents the first player's cards.

.PARAMETER PlayerB
An array of string(s) represents the second player's cards.
#>
[CmdletBinding()]
Param(
[string[]]$PlayerA,
[string[]]$PlayerB
)
$gameState = [GameState]::New($PlayerA, $PlayerB)
$stateSet = [HashSet[string]]::new()
$resultStatus = [Status]::Loop

while ($true) {
$stateRepresent = $gameState.ToString()
if ($stateSet.Contains($stateRepresent)) {
break
}
if ($stateRepresent -match '[JQKA]') {
$stateSet.Add($stateRepresent) | Out-Null
}
PlayTurn($gameState)
if ($gameState.HasWinner()) {
$resultStatus = [Status]::Finished
break
}
}
[PSCustomObject]@{
Status = $resultStatus
Cards = $gameState.CardsPlayed
Tricks = $gameState.Tricks
}
}

Function PlayTurn ([GameState]$state) {
while ($true) {
$startDebt = $state.Debt
$player = $state.PlayerTurn -eq "A" ? $state.PAHand : $state.PBHand
$other = $state.PlayerTurn -eq "A" ? $state.PBHand : $state.PAHand
if ($player.Count -eq 0) {
$state.Piles | ForEach-Object { $other.Enqueue($_) }
$state.Piles.Clear()
$state.Tricks++
break
}

$top = $player.Dequeue()
$state.Piles.Add($top)
$state.CardsPlayed++

if ($state.PaymentCards -match $top)
{
$state.Debt = $state.PaymentCards.IndexOf($top) + 1
$state.SwitchPlayer()
} else {
$state.Debt = $state.Debt -gt 0 ? $state.Debt - 1 : 0
if ($startDebt -gt 0 -and $state.Debt -eq 0) {
$state.SwitchPlayer()
$state.Piles | ForEach-Object { $other.Enqueue($_) }
$state.Piles.Clear()
$state.Tricks++
break
}

if ($state.Debt -eq 0) {
$state.SwitchPlayer()
}
}
}
}

class GameState {
[Queue[string]]$PAHand
[Queue[string]]$PBHand
[List[string]]$Piles
[string]$PlayerTurn
[int]$Tricks
[int]$Debt
[string]$PaymentCards
[int]$CardsPlayed

GameState([string[]]$PlayerA, [string[]]$PlayerB) {
$this.PAHand = [Queue[string]]::New($PlayerA)
$this.PBHand = [Queue[string]]::New($PlayerB)
$this.Piles = [List[string]]::New()
$this.PlayerTurn = "A"
$this.Tricks = 0
$this.Debt = 0
$this.PaymentCards = "JQKA"
}

[bool]HasWinner() {
return ($this.Piles.Count -eq 0) -and ($this.PAHand.Count -eq 0 -or $this.PBHand.Count -eq 0)
}

[string]ToString() {
$sb = [StringBuilder]::new()
foreach ($card in $this.PAHand) {
$sb.Append( $card -match '[JQKA]' ? $card : '_')
}
$sb.Append(' ')
foreach ($card in $this.PBHand) {
$sb.Append( $card -match '[JQKA]' ? $card : '_')
}
return $sb.ToString()
}

[void] SwitchPlayer() {
$this.PlayerTurn = $this.PlayerTurn -eq "A" ? "B" : "A"
}
}
19 changes: 19 additions & 0 deletions exercises/practice/camicia/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"glaxxie"
],
"files": {
"solution": [
"Camicia.ps1"
],
"test": [
"Camicia.tests.ps1"
],
"example": [
".meta/Camicia.example.ps1"
]
},
"blurb": "Simulate the card game and determine whether the match ends or enters an infinite loop.",
"source": "Beggar-My-Neighbour",
"source_url": "https://www.richardpmann.com/beggar-my-neighbour-records.html"
}
94 changes: 94 additions & 0 deletions exercises/practice/camicia/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[0b7f737c-3ecd-4a55-b34d-e65c62a85c28]
description = "two cards, one trick"

[27c19d75-53a5-48e5-b33b-232c3884d4f3]
description = "three cards, one trick"

[9b02dd49-efaf-4b71-adca-a05c18a7c5b0]
description = "four cards, one trick"

[fa3f4479-466a-4734-a001-ab79bfe27260]
description = "the ace reigns supreme"

[07629689-f589-4f54-a6d1-8ce22776ce72]
description = "the king beats ace"

[54d4a1c5-76fb-4d1e-8358-0e0296ac0601]
description = "the queen seduces the king"

[c875500c-ff3d-47a4-bd1e-b60b90da80aa]
description = "the jack betrays the queen"

[436875da-96ca-4149-be22-0b78173b8125]
description = "the 10 just wants to put on a show"

[5be39bb6-1b34-4ce6-a1cd-0fcc142bb272]
description = "simple loop with decks of 3 cards"

[2795dc21-0a2a-4c38-87c2-5a42e1ff15eb]
description = "the story is starting to get a bit complicated"

[6999dfac-3fdc-41e2-b64b-38f4be228712]
description = "two tricks"

[83dcd4f3-e089-4d54-855a-73f5346543a3]
description = "more tricks"

[3107985a-f43e-486a-9ce8-db51547a9941]
description = "simple loop with decks of 4 cards"

[dca32c31-11ed-49f6-b078-79ab912c1f7b]
description = "easy card combination"

[1f8488d0-48d3-45ae-b819-59cedad0a5f4]
description = "easy card combination, inverted decks"

[98878d35-623a-4d05-b81a-7bdc569eb88d]
description = "mirrored decks"

[3e0ba597-ca10-484b-87a3-31a7df7d6da3]
description = "opposite decks"

[92334ddb-aaa7-47fa-ab36-e928a8a6a67c]
description = "random decks #1"

[30477523-9651-4860-84a3-e1ac461bb7fa]
description = "random decks #2"

[20967de8-9e94-4e0e-9010-14bc1c157432]
description = "Kleber 1999"

[9f2fdfe8-27f3-4323-816d-6bce98a9c6f7]
description = "Collins 2006"

[c90b6f8d-7013-49f3-b5cb-14ea006cca1d]
description = "Mann and Wu 2007"

[a3f1fbc5-1d0b-499a-92a5-22932dfc6bc8]
description = "Nessler 2012"

[9cefb1ba-e6d1-4ab7-9d8f-76d8e0976d5f]
description = "Anderson 2013"

[d37c0318-5be6-48d0-ab72-a7aaaff86179]
description = "Rucklidge 2014"

[4305e479-ba87-432f-8a29-cd2bd75d2f05]
description = "Nessler 2021"

[252f5cc3-b86d-4251-87ce-f920b7a6a559]
description = "Nessler 2022"

[b9efcfa4-842f-4542-8112-8389c714d958]
description = "Casella 2024, first infinite game found"
Loading