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 @@ -856,6 +856,14 @@
],
"difficulty": 5
},
{
"slug": "camicia",
"name": "Camicia",
"uuid": "8a02fe66-fd54-49ab-ae25-1722a1dc1620",
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "change",
"name": "Change",
Expand Down
1 change: 1 addition & 0 deletions exercises/Exercises.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<Project Path="practice/book-store/BookStore.vbproj" />
<Project Path="practice/bottle-song/BottleSong.vbproj" />
<Project Path="practice/bowling/Bowling.vbproj" />
<Project Path="practice/camicia/Camicia.vbproj" />
<Project Path="practice/change/Change.vbproj" />
<Project Path="practice/circular-buffer/CircularBuffer.vbproj" />
<Project Path="practice/clock/Clock.vbproj" />
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
89 changes: 89 additions & 0 deletions exercises/practice/camicia/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
###############################
# 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-2022
; https://docs.microsoft.com/en-us/visualstudio/ide/create-portable-custom-editor-options?view=vs-2022

root = true

[*]
end_of_line = lf
indent_style = space
trim_trailing_whitespace = true
insert_final_newline = true

[*.{vbproj,slnx}]
indent_size = 2

[*.vb]
indent_size = 4
charset = utf-8

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

# Organize imports
dotnet_sort_system_directives_first = true
dotnet_separate_import_directive_groups = true

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

###################################
# Visual Basic Coding Conventions #
###################################
visual_basic_preferred_modifier_order = Partial, Default, Private, Protected, Public, Friend, NotOverridable, Overridable, MustOverride, Overloads, Overrides, MustInherit, NotInheritable, Static, Shared, Shadows, ReadOnly, WriteOnly, Dim, Const, WithEvents, Widening, Narrowing, Custom, Async:suggestion
visual_basic_style_unused_variable_expression_statement_preference = unused_local_variable:suggestion
visual_basic_style_unused_value_assignment_preference = unused_local_variable:suggestion
visual_basic_style_prefer_isnot_expression = true:suggestion
visual_basic_style_prefer_simplified_object_creation = false:none
dotnet_diagnostic.IDE0090.severity = none
147 changes: 147 additions & 0 deletions exercises/practice/camicia/.meta/Example.vb
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
Public Enum GameStatus
Finished
[Loop]
End Enum

Public Structure GameResult
Public Sub New(ByVal status As GameStatus, ByVal tricks As Integer, ByVal cards As Integer)
Me.Status = status
Me.Tricks = tricks
Me.Cards = cards
End Sub

Public ReadOnly Property Status As GameStatus
Public ReadOnly Property Tricks As Integer
Public ReadOnly Property Cards As Integer
End Structure

Public Module Camicia
Private Enum Player
A
B
End Enum

Public Function SimulateGame(ByVal playerA As String(), ByVal playerB As String()) As GameResult
Dim handA As New Queue(Of Integer)(playerA.Select(AddressOf CardValue))
Dim handB As New Queue(Of Integer)(playerB.Select(AddressOf CardValue))
Dim pile As New List(Of Integer)
Dim seen As New HashSet(Of String)
Dim activePlayer = Player.A
Dim tricks = 0
Dim cards = 0
Dim debt = 0

While True
If IsRepeatedPosition(pile, handA, handB, activePlayer, seen) Then
Return New GameResult(GameStatus.[Loop], tricks, cards)
End If

Dim activeHand = HandFor(activePlayer, handA, handB)
Dim otherHand = HandFor(OtherPlayer(activePlayer), handA, handB)

If activeHand.Count = 0 Then
Return FinishedGame(tricks, cards, pile)
End If

Dim card = activeHand.Dequeue()
pile.Add(card)
cards += 1

If card > 0 Then
debt = card
activePlayer = OtherPlayer(activePlayer)
ElseIf debt > 1 Then
debt -= 1
ElseIf debt = 1 Then
AwardPile(pile, otherHand)
tricks += 1
debt = 0

If handA.Count = 0 OrElse handB.Count = 0 Then
Return New GameResult(GameStatus.Finished, tricks, cards)
End If

activePlayer = OtherPlayer(activePlayer)
Else
activePlayer = OtherPlayer(activePlayer)
End If
End While

Throw New InvalidOperationException("unreachable")
End Function

Private Function IsRepeatedPosition(
ByVal pile As List(Of Integer),
ByVal handA As Queue(Of Integer),
ByVal handB As Queue(Of Integer),
ByVal activePlayer As Player,
ByVal seen As HashSet(Of String)) As Boolean

If pile.Count > 0 Then
Return False
End If

Dim position = PositionKey(handA, handB, activePlayer)
Return Not seen.Add(position)
End Function

Private Function FinishedGame(
ByVal tricks As Integer,
ByVal cards As Integer,
ByVal pile As List(Of Integer)) As GameResult

Dim extraTrick = If(pile.Count = 0, 0, 1)
Return New GameResult(GameStatus.Finished, tricks + extraTrick, cards)
End Function

Private Function HandFor(
ByVal player As Player,
ByVal handA As Queue(Of Integer),
ByVal handB As Queue(Of Integer)) As Queue(Of Integer)

If player = Player.A Then
Return handA
End If

Return handB
End Function

Private Sub AwardPile(ByVal pile As List(Of Integer), ByVal winner As Queue(Of Integer))
For Each card In pile
winner.Enqueue(card)
Next

pile.Clear()
End Sub

Private Function OtherPlayer(ByVal player As Player) As Player
If player = Player.A Then
Return Player.B
End If

Return Player.A
End Function

Private Function CardValue(ByVal card As String) As Integer
Select Case card
Case "J"
Return 1
Case "Q"
Return 2
Case "K"
Return 3
Case "A"
Return 4
Case Else
Return 0
End Select
End Function

Private Function PositionKey(
ByVal handA As Queue(Of Integer),
ByVal handB As Queue(Of Integer),
ByVal activePlayer As Player) As String

Return $"{String.Join(",", handA)}|{String.Join(",", handB)}|{activePlayer}"
End Function
End Module
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": [
"BNAndras"
],
"files": {
"solution": [
"Camicia.vb"
],
"test": [
"CamiciaTests.vb"
],
"example": [
".meta/Example.vb"
]
},
"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"
}
Loading
Loading