Skip to content
Open
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
13 changes: 13 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1275,6 +1275,19 @@
],
"difficulty": 6
},
{
"slug": "camicia",
"name": "Camicia",
"uuid": "b4f7c3b0-6d3c-45e2-a328-05e09c7467f4",
"practices": [],
"prerequisites": [
"strings",
"for-loops",
"arrays",
"if-else-statements"
],
"difficulty": 6
},
{
"slug": "etl",
"name": "ETL",
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 **CamiciaGame**.
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!
~~~~
23 changes: 23 additions & 0 deletions exercises/practice/camicia/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"authors": [],
"contributors": [
"thibault2705"
],
"files": {
"solution": [
"src/main/java/Camicia.java"
],
"test": [
"src/test/java/CamiciaTest.java"
],
"example": [
".meta/src/reference/java/Camicia.java"
],
"invalidator": [
"build.gradle"
]
},
"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"
}
172 changes: 172 additions & 0 deletions exercises/practice/camicia/.meta/src/reference/java/Camicia.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Camicia {

private Status status;
private int cards;
private int tricks;

private enum Player {
PLAYER_A, PLAYER_B
}

private enum Status {
FINISHED, LOOP
}

private static int penaltyOf(String card) {
return switch (card) {
case "J" -> 1;
case "Q" -> 2;
case "K" -> 3;
case "A" -> 4;
default -> 0;
};
}

private static boolean isPaymentCard(String card) {
return penaltyOf(card) > 0;
}

/**
* Return a snapshot of the current state
*/
private static String stateKey(Deque<String> deckA, Deque<String> deckB) {
StringBuilder sb = new StringBuilder();

for (String card : deckA) {
sb.append(isPaymentCard(card) ? card : "N");
}

sb.append('|');

for (String card : deckB) {
sb.append(isPaymentCard(card) ? card : "N");
}

return sb.toString();
}

private static Player otherPlayer(Player player) {
return player == Player.PLAYER_A ? Player.PLAYER_B : Player.PLAYER_A;
}

private static Deque<String> deckOf(Player player, Deque<String> deckA, Deque<String> deckB) {
return player == Player.PLAYER_A ? deckA : deckB;
}

public void simulateGame(List<String> playerA, List<String> playerB) {
Deque<String> deckA = new ArrayDeque<>(playerA);
Deque<String> deckB = new ArrayDeque<>(playerB);

int cardsPlayed = 0;
int tricksCount = 0;
Player current = Player.PLAYER_A;

Set<String> seenStates = new HashSet<>();

while (true) {
String key = stateKey(deckA, deckB);
// Key already exists, which means this is a loop
if (!seenStates.add(key)) {
finishGame(Status.LOOP, cardsPlayed, tricksCount);
return;
}

// Otherwise, play next round
RoundResult result = playRound(deckA, deckB, current);

cardsPlayed += result.pileSize();
tricksCount++;

// Check if someone wins and finish the game
if (hasWinner(deckA, deckB)) {
finishGame(Status.FINISHED, cardsPlayed, tricksCount);
return;
}

// Otherwise, play next round
current = result.nextStarter();
}
}

private RoundResult playRound(Deque<String> deckA, Deque<String> deckB, Player startingPlayer) {
Deque<String> pile = new ArrayDeque<>(); // cards played in this round
Player currentPlayer = startingPlayer;
int pendingPenalty = 0;

while (true) {
Deque<String> currentPlayerDeck = deckOf(currentPlayer, deckA, deckB);
Player opponent = otherPlayer(currentPlayer);
Deque<String> opponentDeck = deckOf(opponent, deckA, deckB);

// Current player deck is empty, opponent collects all pile, end this round
if (currentPlayerDeck.isEmpty()) {
opponentDeck.addAll(pile);
return new RoundResult(opponent, pile.size());
}

// Otherwise, current player plays 1 card, add to pile
String card = currentPlayerDeck.poll();
pile.addLast(card);

// Current player must pay off pending penalty
if (pendingPenalty > 0) {
// And player reveals a payment card
if (isPaymentCard(card)) {
// reset penalty based on new card, switch turn
pendingPenalty = penaltyOf(card);
currentPlayer = opponent;
} else {
// Otherwise, deduct penalty
pendingPenalty--;
// No pending penalty
if (pendingPenalty == 0) {
// Opponent collects all pile and win the round
deckOf(opponent, deckA, deckB).addAll(pile);
return new RoundResult(opponent, pile.size());
}
}
} else {
// Normal gameplay, without pending penalty
// If player reveals a payment card, update penalty
if (isPaymentCard(card)) {
pendingPenalty = penaltyOf(card);
}
currentPlayer = opponent;
}
}
}

private void finishGame(Status status, int cardsPlayed, int tricksCount) {
this.status = status;
this.cards = cardsPlayed;
this.tricks = tricksCount;
}

private boolean hasWinner(Deque<String> deckA, Deque<String> deckB) {
return deckA.isEmpty() || deckB.isEmpty();
}

public String getStatus() {
return status != null ? status.name().toLowerCase() : "";
}

public int getCards() {
return cards;
}

public int getTricks() {
return tricks;
}

/**
* Immutable round result
*/
private record RoundResult(Player nextStarter, int pileSize) {
}
}
25 changes: 25 additions & 0 deletions exercises/practice/camicia/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
plugins {
id "java"
}

repositories {
mavenCentral()
}

dependencies {
testImplementation platform("org.junit:junit-bom:5.10.0")
testImplementation "org.junit.jupiter:junit-jupiter"
testImplementation "org.assertj:assertj-core:3.25.1"

testRuntimeOnly "org.junit.platform:junit-platform-launcher"
}

test {
useJUnitPlatform()

testLogging {
exceptionFormat = "full"
showStandardStreams = true
events = ["passed", "failed", "skipped"]
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading
Loading