-
-
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
Open
thibault2705
wants to merge
26
commits into
exercism:main
Choose a base branch
from
thibault2705:camicia-exercice
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
3a5dba3
Initial setup
thibault2705 5a9b18f
Add Camicia class & Implement tests
thibault2705 6bafc45
Implement solution
thibault2705 df80e36
Implement solution
thibault2705 dcb0d76
Implement solution
thibault2705 2bbcd2d
Implement solution
thibault2705 a3b38ad
Implement solution
thibault2705 4fd0bb3
Implement solution
thibault2705 bee81c1
Implement solution
thibault2705 1f7a8b5
Implement solution
thibault2705 ec66e05
Implement solution
thibault2705 8b279ee
Implement solution
thibault2705 3e04726
Implement solution
thibault2705 a510e82
Implement solution
thibault2705 c15e3fe
Move solution to the .meta package
thibault2705 ce2d56d
Merge branch 'main' into camicia-exercice
thibault2705 a170168
Move to .meta/src/reference/java/
thibault2705 686cddb
Merge remote-tracking branch 'origin/camicia-exercice' into camicia-e…
thibault2705 b6257d3
Config Camicia exercise
thibault2705 937484f
End file with a new line
thibault2705 cd1b266
Update author
thibault2705 59e777b
Make simulateGame static and return result record
thibault2705 b99d44b
Update solution and disable more tests
thibault2705 9209951
Merge branch 'main' into camicia-exercice
thibault2705 70ecce5
Update gradle version
thibault2705 1918358
Merge remote-tracking branch 'origin/camicia-exercice' into camicia-e…
thibault2705 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 **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! | ||
| ~~~~ |
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,23 @@ | ||
| { | ||
| "authors": [ | ||
| "thibault2705" | ||
| ], | ||
| "contributors": [], | ||
| "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" | ||
| } | ||
156 changes: 156 additions & 0 deletions
156
exercises/practice/camicia/.meta/src/reference/java/Camicia.java
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,156 @@ | ||
| 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 static CamiciaResult 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)) { | ||
| return finishGame(Status.LOOP, cardsPlayed, tricksCount); | ||
| } | ||
|
|
||
| // 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)) { | ||
| return finishGame(Status.FINISHED, cardsPlayed, tricksCount); | ||
| } | ||
|
|
||
| // Otherwise, play next round | ||
| current = result.nextStarter(); | ||
| } | ||
| } | ||
|
|
||
| private static 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 static CamiciaResult finishGame(Status status, int cardsPlayed, int tricksCount) { | ||
| return new CamiciaResult(status == null ? null : status.toString().toLowerCase(), cardsPlayed, tricksCount); | ||
| } | ||
|
|
||
| private static boolean hasWinner(Deque<String> deckA, Deque<String> deckB) { | ||
| return deckA.isEmpty() || deckB.isEmpty(); | ||
| } | ||
|
|
||
| /** | ||
| * Immutable round result | ||
| */ | ||
| private record RoundResult(Player nextStarter, int pileSize) { | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
exercises/practice/camicia/.meta/src/reference/java/CamiciaResult.java
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,28 @@ | ||
| /** | ||
| * {@link CamiciaResult} shows the result of a Camicia game | ||
| */ | ||
| public record CamiciaResult(String status, int cards, int tricks) { | ||
|
|
||
| /** | ||
| * @return the game status | ||
| */ | ||
| public String status() { | ||
| return status; | ||
| } | ||
|
|
||
| /** | ||
| * @return the total played cards | ||
| */ | ||
| public int cards() { | ||
| return cards; | ||
| } | ||
|
|
||
| /** | ||
| * @return the total tricks used | ||
| */ | ||
| public int tricks() { | ||
| return tricks; | ||
| } | ||
| } | ||
|
|
||
|
|
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,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.
6 changes: 6 additions & 0 deletions
6
exercises/practice/camicia/gradle/wrapper/gradle-wrapper.properties
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,6 @@ | ||
| distributionBase=GRADLE_USER_HOME | ||
| distributionPath=wrapper/dists | ||
| distributionUrl=https\://services.gradle.org/distributions/gradle-9.3.1-bin.zip | ||
| validateDistributionUrl=true | ||
| zipStoreBase=GRADLE_USER_HOME | ||
| zipStorePath=wrapper/dists |
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.