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
22 changes: 22 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2371,6 +2371,28 @@
],
"difficulty": 6
},
{
"slug": "camicia",
"name": "Camicia",
"uuid": "61976949-e1a6-4494-9ca0-d6d771e2e8e8",
"practices": [],
"prerequisites": [
"strings",
"tuples",
"mapsets",
"maps",
"structs",
"lists",
"enum",
"case",
"if",
"cond",
"pattern-matching",
"recursion",
"tail-call-recursion"
],
"difficulty": 6
},
{
"slug": "game-of-life",
"name": "Game of Life",
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
4 changes: 4 additions & 0 deletions exercises/practice/camicia/.formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
24 changes: 24 additions & 0 deletions exercises/practice/camicia/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# The directory Mix will write compiled artifacts to.
/_build/

# If you run "mix test --cover", coverage assets end up here.
/cover/

# The directory Mix downloads your dependencies sources to.
/deps/

# Where third-party dependencies like ExDoc output generated docs.
/doc/

# Ignore .fetch files in case you like to edit your project deps locally.
/.fetch

# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump

# Also ignore archive artifacts (built via "mix archive.build").
*.ez

# Ignore package tarball (built via "mix hex.build").
camicia-*.tar

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": [
"FraSanga"
],
"files": {
"solution": [
"lib/camicia.ex"
],
"test": [
"test/camicia_test.exs"
],
"example": [
".meta/example.ex"
]
},
"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"
}
150 changes: 150 additions & 0 deletions exercises/practice/camicia/.meta/example.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
defmodule Camicia do
@doc """
Simulate a card game between two players.
Each player has a deck of cards represented as a list of strings.
Returns a tuple with the result of the game:
- `{:finished, cards, tricks}` if the game finishes with a winner
- `{:loop, cards, tricks}` if the game enters a loop
`cards` is the number of cards played.
`tricks` is the number of central piles collected.

## Examples

iex> Camicia.simulate(["2"], ["3"])
{:finished, 2, 1}

iex> Camicia.simulate(["J", "2", "3"], ["4", "J", "5"])
{:loop, 8, 3}
"""
@penalties %{"J" => 1, "Q" => 2, "K" => 3, "A" => 4}
@payment_cards Map.keys(@penalties)

@spec simulate(list(String.t()), list(String.t())) ::
{:finished | :loop, non_neg_integer(), non_neg_integer()}
def simulate(player_a, player_b) do
initial_state = %{seen: MapSet.new(), cards: 0, tricks: 0, current_player: :player_a}
do_simulate(format_deck(player_a), format_deck(player_b), initial_state)
end

defp do_simulate([], _player_b, %{cards: cards, tricks: tricks}),
do: {:finished, cards, tricks}

defp do_simulate(_player_a, [], %{cards: cards, tricks: tricks}),
do: {:finished, cards, tricks}

defp do_simulate(player_a, player_b, %{
seen: seen,
cards: cards,
tricks: tricks,
current_player: current_player
}) do
hands = {player_a, player_b}

if MapSet.member?(seen, hands) do
{:loop, cards, tricks}
else
new_seen = MapSet.put(seen, hands)
state = %{cards: cards, tricks: tricks, current_player: current_player}

{winner, loser, %{current_player: next_player} = new_state} =
case current_player do
:player_a -> round(player_a, player_b, state)
:player_b -> round(player_b, player_a, state)
end

case next_player do
:player_a -> do_simulate(winner, loser, new_state |> Map.put(:seen, new_seen))
:player_b -> do_simulate(loser, winner, new_state |> Map.put(:seen, new_seen))
end
end
end

defp round(player, opponent, state),
do: do_round(player, opponent, state, %{type: :number, central_pile: []})

defp do_round([], opponent, %{cards: cards, tricks: tricks, current_player: current_player}, %{
type: _,
central_pile: central_pile
}) do
{opponent ++ Enum.reverse(central_pile), [],
%{cards: cards, tricks: tricks + 1, current_player: change_player(current_player)}}
end

defp do_round(
[card | rest],
opponent,
%{cards: cards, tricks: tricks, current_player: current_player},
%{type: :number, central_pile: central_pile}
) do
new_state = %{cards: cards + 1, tricks: tricks, current_player: change_player(current_player)}

case card do
"-" ->
do_round(
opponent,
rest,
new_state,
%{type: :number, central_pile: [card | central_pile]}
)

payment_card when payment_card in @payment_cards ->
penalty = Map.get(@penalties, payment_card)

do_round(
opponent,
rest,
new_state,
%{type: {:payment, penalty}, central_pile: [card | central_pile]}
)
end
end

defp do_round(
player,
opponent,
%{cards: cards, tricks: tricks, current_player: current_player},
%{type: {:payment, 0}, central_pile: central_pile}
) do
{opponent ++ Enum.reverse(central_pile), player,
%{cards: cards, tricks: tricks + 1, current_player: change_player(current_player)}}
end

defp do_round(
[card | rest],
opponent,
%{cards: cards, tricks: tricks, current_player: current_player},
%{type: {:payment, penalty}, central_pile: central_pile}
) do
case card do
"-" ->
do_round(
rest,
opponent,
%{cards: cards + 1, tricks: tricks, current_player: current_player},
%{type: {:payment, penalty - 1}, central_pile: [card | central_pile]}
)

payment_card when payment_card in @payment_cards ->
penalty = Map.get(@penalties, payment_card)

do_round(
opponent,
rest,
%{cards: cards + 1, tricks: tricks, current_player: change_player(current_player)},
%{type: {:payment, penalty}, central_pile: [card | central_pile]}
)
end
end

defp format_deck(deck) do
Enum.map(deck, fn card ->
cond do
card in @payment_cards -> card
true -> "-"
end
end)
end

defp change_player(:player_a), do: :player_b
defp change_player(:player_b), do: :player_a
end
Loading
Loading