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 @@ -2793,6 +2793,14 @@
"string-formatting"
],
"difficulty": 6
},
{
"slug": "camicia",
"name": "Camicia",
"uuid": "7747b8bf-a535-42c2-9e51-5b007419bccb",
"practices": [],
"prerequisites": [],
"difficulty": 5
}
],
"foregone": [
Expand Down
15 changes: 15 additions & 0 deletions exercises/Exercises.sln
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RelativeDistance", "practic
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FlowerField", "practice\flower-field\FlowerField.csproj", "{88E165C6-5E27-4F48-942A-0D41A9A6326D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Camicia", "practice\camicia\Camicia.csproj", "{637DEB61-9832-42B0-9907-0671611A92EB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -2553,6 +2555,18 @@ Global
{88E165C6-5E27-4F48-942A-0D41A9A6326D}.Release|x64.Build.0 = Release|Any CPU
{88E165C6-5E27-4F48-942A-0D41A9A6326D}.Release|x86.ActiveCfg = Release|Any CPU
{88E165C6-5E27-4F48-942A-0D41A9A6326D}.Release|x86.Build.0 = Release|Any CPU
{637DEB61-9832-42B0-9907-0671611A92EB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{637DEB61-9832-42B0-9907-0671611A92EB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{637DEB61-9832-42B0-9907-0671611A92EB}.Debug|x64.ActiveCfg = Debug|Any CPU
{637DEB61-9832-42B0-9907-0671611A92EB}.Debug|x64.Build.0 = Debug|Any CPU
{637DEB61-9832-42B0-9907-0671611A92EB}.Debug|x86.ActiveCfg = Debug|Any CPU
{637DEB61-9832-42B0-9907-0671611A92EB}.Debug|x86.Build.0 = Debug|Any CPU
{637DEB61-9832-42B0-9907-0671611A92EB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{637DEB61-9832-42B0-9907-0671611A92EB}.Release|Any CPU.Build.0 = Release|Any CPU
{637DEB61-9832-42B0-9907-0671611A92EB}.Release|x64.ActiveCfg = Release|Any CPU
{637DEB61-9832-42B0-9907-0671611A92EB}.Release|x64.Build.0 = Release|Any CPU
{637DEB61-9832-42B0-9907-0671611A92EB}.Release|x86.ActiveCfg = Release|Any CPU
{637DEB61-9832-42B0-9907-0671611A92EB}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -2740,6 +2754,7 @@ Global
{9A073412-4B64-48EB-A346-A56DE6847CCC} = {FA726133-BC1C-D753-28C5-C9ACB44C0776}
{2C37454E-8624-47B8-A09B-ADE201C2B04C} = {E276EF69-669A-43E0-88AC-8ABB17A9C026}
{88E165C6-5E27-4F48-942A-0D41A9A6326D} = {E276EF69-669A-43E0-88AC-8ABB17A9C026}
{637DEB61-9832-42B0-9907-0671611A92EB} = {E276EF69-669A-43E0-88AC-8ABB17A9C026}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {AB4EA6C9-5461-4024-BDC7-2AE0C3A85CD1}
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
141 changes: 141 additions & 0 deletions exercises/practice/camicia/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
###############################
# Core EditorConfig Options #
###############################

; This file is for unifying the coding style for different editors and IDEs.
; More information at:
; https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference?view=vs-2017
; https://docs.microsoft.com/en-us/visualstudio/ide/create-portable-custom-editor-options?view=vs-2017

root = true

[*]
indent_style = space

[Camicia.cs]
indent_size = 4

###############################
# .NET Coding Conventions #
###############################

# Organize usings
dotnet_sort_system_directives_first = true
dotnet_separate_import_directive_groups = true

# this. preferences
dotnet_style_qualification_for_field = false:suggestion
dotnet_style_qualification_for_property = false:suggestion
dotnet_style_qualification_for_method = false:suggestion
dotnet_style_qualification_for_event = false:suggestion

# Language keywords vs BCL types preferences
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion
dotnet_style_predefined_type_for_member_access = true:suggestion

# Parentheses preferences
dotnet_style_parentheses_in_arithmetic_binary_operators = never_if_unnecessary:none
dotnet_style_parentheses_in_relational_binary_operators = never_if_unnecessary:none
dotnet_style_parentheses_in_other_binary_operators = never_if_unnecessary:none
dotnet_style_parentheses_in_other_operators = never_if_unnecessary:suggestion

# Modifier preferences
dotnet_style_require_accessibility_modifiers = always:suggestion
dotnet_style_readonly_field = true:suggestion

# Expression-level preferences
dotnet_style_object_initializer = true:suggestion
dotnet_style_collection_initializer = true:suggestion
dotnet_style_explicit_tuple_names = true:suggestion
dotnet_style_prefer_inferred_tuple_names = true:suggestion
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
dotnet_style_prefer_auto_properties = true:suggestion
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion
dotnet_style_prefer_conditional_expression_over_assignment = true:suggestion
dotnet_style_prefer_conditional_expression_over_return = true:suggestion
dotnet_style_coalesce_expression = true:suggestion
dotnet_style_null_propagation = true:suggestion

###############################
# Naming Conventions #
###############################

# Style Definitions
dotnet_naming_style.pascal_case_style.capitalization = pascal_case

# Use PascalCase for constant fields
dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.constant_fields_should_be_pascal_case.symbols = constant_fields
dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style
dotnet_naming_symbols.constant_fields.applicable_kinds = field
dotnet_naming_symbols.constant_fields.applicable_accessibilities = *
dotnet_naming_symbols.constant_fields.required_modifiers = const

###############################
# C# Code Style Rules #
###############################

# var preferences
csharp_style_var_for_built_in_types = true:none
csharp_style_var_when_type_is_apparent = true:none
csharp_style_var_elsewhere = true:none

# Expression-bodied members
csharp_style_expression_bodied_methods = true:suggestion
csharp_style_expression_bodied_constructors = true:suggestion
csharp_style_expression_bodied_operators = true:suggestion
csharp_style_expression_bodied_properties = true:suggestion
csharp_style_expression_bodied_indexers = true:suggestion
csharp_style_expression_bodied_accessors = true:suggestion

# Pattern-matching preferences
csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion
csharp_style_pattern_matching_over_as_with_null_check = true:suggestion

# Null-checking preferences
csharp_style_throw_expression = true:suggestion
csharp_style_conditional_delegate_call = true:suggestion

# Modifier preferences
csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:suggestion

# Expression-level preferences
csharp_prefer_braces = true:none
csharp_prefer_simple_default_expression = true:suggestion
csharp_style_deconstructed_variable_declaration = true:suggestion
csharp_style_pattern_local_over_anonymous_function = true:suggestion
csharp_style_inlined_variable_declaration = true:suggestion

###############################
# C# Formatting Rules #
###############################

# New line preferences
csharp_new_line_before_open_brace = all
csharp_new_line_before_else = true
csharp_new_line_before_catch = true
csharp_new_line_before_finally = true
csharp_new_line_before_members_in_object_initializers = false
csharp_new_line_before_members_in_anonymous_types = false
csharp_new_line_between_query_expression_clauses = true

# Indentation preferences
csharp_indent_case_contents = true
csharp_indent_switch_labels = true
csharp_indent_labels = flush_left

# Space preferences
csharp_space_after_cast = false
csharp_space_after_keywords_in_control_flow_statements = true
csharp_space_between_method_declaration_parameter_list_parentheses = false
csharp_space_between_method_call_parameter_list_parentheses = false
csharp_space_before_colon_in_inheritance_clause = true
csharp_space_after_colon_in_inheritance_clause = true
csharp_space_around_binary_operators = before_and_after
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
csharp_space_between_method_call_name_and_opening_parenthesis = false
csharp_space_between_method_call_empty_parameter_list_parentheses = false

# Wrapping preferences
csharp_preserve_single_line_blocks = true
csharp_preserve_single_line_statements = true
94 changes: 94 additions & 0 deletions exercises/practice/camicia/.meta/Example.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using System;
using System.Collections.Generic;
using System.Linq;

public static class Camicia
{
public enum GameStatus
{
Finished,
Loop
}

public record GameResult(GameStatus Status, int Tricks, int Cards);

public static GameResult SimulateGame(string[] playerA, string[] playerB)
{
Func<string, int> GetValue = (string card) => card switch
{
"J" => 1,
"Q" => 2,
"K" => 3,
"A" => 4,
_ => 0
};
Queue<int> handA = new(playerA.Select(GetValue));
Queue<int> handB = new(playerB.Select(GetValue));
var turn = "A";
List<int> pile = new();
HashSet<string> seen = new();
int totalTricks = 0;
int cardsPlayed = 0;
int currentDebt = 0;

while (true)
{
if (pile.Count == 0)
{
var state = $"{string.Join(",", handA)}|{string.Join(",", handB)}|{turn}";
if (seen.Contains(state))
{
return new GameResult(GameStatus.Loop, totalTricks, cardsPlayed);
}
seen.Add(state);
}

var activeHand = turn == "A" ? handA : handB;
var otherHand = turn == "A" ? handB : handA;

if (activeHand.Count == 0)
{
int extraTrick = pile.Count > 0 ? 1 : 0;
return new GameResult(GameStatus.Finished, totalTricks + extraTrick, cardsPlayed);
}

int cardVal = activeHand.Dequeue();
pile.Add(cardVal);
cardsPlayed++;

if (cardVal > 0)
{
currentDebt = cardVal;
turn = turn == "A" ? "B" : "A";
}
else
{
if (currentDebt > 0)
{
currentDebt--;
if (currentDebt == 0)
{
foreach (int card in pile)
{
otherHand.Enqueue(card);
}
pile.Clear();
totalTricks++;
currentDebt = 0;

if (handA.Count == 0 || handB.Count == 0)
{
return new GameResult(GameStatus.Finished, totalTricks, cardsPlayed);
}

turn = turn == "A" ? "B" : "A";
}
}
else
{
turn = turn == "A" ? "B" : "A";
}
}
}
}
}
Loading
Loading