-
-
Notifications
You must be signed in to change notification settings - Fork 755
Implement Camicia Practice Exercise #3105
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
base: main
Are you sure you want to change the base?
Changes from 19 commits
3a5dba3
5a9b18f
6bafc45
df80e36
dcb0d76
2bbcd2d
a3b38ad
4fd0bb3
bee81c1
1f7a8b5
ec66e05
8b279ee
3e04726
a510e82
c15e3fe
ce2d56d
a170168
686cddb
b6257d3
937484f
cd1b266
59e777b
b99d44b
9209951
70ecce5
1918358
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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! | ||
| ~~~~ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| { | ||
| "authors": [], | ||
| "contributors": [ | ||
| "thibault2705" | ||
| ], | ||
| "files": { | ||
| "solution": [ | ||
| "src/main/java/Camicia.java" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we need to add |
||
| ], | ||
| "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" | ||
| } | ||
| 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) { | ||
| } | ||
| } | ||
|
thibault2705 marked this conversation as resolved.
Outdated
|
||
| 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"] | ||
| } | ||
| } |
| 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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks like an older version of Gradle (we have been using Gradle 9.3.1 on the other exercises). Perhaps copy the Gradle files from one of the other exercises.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree, Gradle version updated to 9.3.1.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I noticed the commit has updated
|
||
| validateDistributionUrl=true | ||
| zipStoreBase=GRADLE_USER_HOME | ||
| zipStorePath=wrapper/dists | ||
Uh oh!
There was an error while loading. Please reload this page.