-
-
Notifications
You must be signed in to change notification settings - Fork 405
Add new practice exercise: Camicia #1593
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e1eb073
added new practice exercise camicia
FraSanga 16984e7
checked practice exercise order
FraSanga a1d0dca
fix typo in tests
FraSanga d13d83b
implemented example.ex & formatted tests
FraSanga 33eea8c
fixed example.ex format
FraSanga 6acba00
added practices & prerequisites
FraSanga e8c2226
Update exercises/practice/camicia/test/camicia_test.exs
FraSanga 9ea9320
Update exercises/practice/camicia/test/camicia_test.exs
FraSanga 350f3fa
Update exercises/practice/camicia/lib/camicia.ex
FraSanga f9b413d
improved example.ex & formatted camicia.ex
FraSanga ab8f2e3
Update config.json
FraSanga fc02e38
final adjustments (syntax & practices)
FraSanga 9cac951
edited practices in config.json
FraSanga ac41d75
Also rename variables in tests
angelikatyborska File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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! | ||
| ~~~~ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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}"] | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.