24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Compiled class file
2 | *.class
3 |
4 | # Log file
5 | *.log
6 |
7 | # BlueJ files
8 | *.ctxt
9 |
10 | # Mobile Tools for Java (J2ME)
11 | .mtj.tmp/
12 |
13 | # Package Files #
14 | *.jar
15 | *.war
16 | *.nar
17 | *.ear
18 | *.zip
19 | *.tar.gz
20 | *.rar
21 |
22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
23 | hs_err_pid*
24 | replay_pid*
25 |
26 | ### Maven ###
27 | target/
28 | pom.xml.tag
29 | pom.xml.releaseBackup
30 | pom.xml.versionsBackup
31 | pom.xml.next
32 | release.properties
33 | dependency-reduced-pom.xml
34 | buildNumber.properties
35 | .mvn/timing.properties
36 | # https://github.com/takari/maven-wrapper#usage-without-binary-jar
37 | .mvn/wrapper/maven-wrapper.jar
38 |
39 | # Eclipse m2e generated files
40 | # Eclipse Core
41 | .project
42 | # JDT-specific (Eclipse Java Development Tools)
43 | .classpath
44 |
45 | # Java 17 Maven project specific exclusions
46 | *.iml
47 | *.ipr
48 | *.iws
49 | .idea/
50 | /.idea/
51 | /.gitignore
52 | /target/
53 | /out/
54 | *.bak
55 | *.swp
56 | .DS_Store
57 | Thumbs.db
58 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2023 lyudaio
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/docs/legal/ASSEMBLY_EXCEPTION:
--------------------------------------------------------------------------------
1 |
2 | OPENJDK ASSEMBLY EXCEPTION
3 |
4 | The OpenJDK source code made available by Oracle America, Inc. (Oracle) at
5 | openjdk.java.net ("OpenJDK Code") is distributed under the terms of the GNU
6 | General Public License version 2
7 | only ("GPL2"), with the following clarification and special exception.
8 |
9 | Linking this OpenJDK Code statically or dynamically with other code
10 | is making a combined work based on this library. Thus, the terms
11 | and conditions of GPL2 cover the whole combination.
12 |
13 | As a special exception, Oracle gives you permission to link this
14 | OpenJDK Code with certain code licensed by Oracle as indicated at
15 | http://openjdk.java.net/legal/exception-modules-2007-05-08.html
16 | ("Designated Exception Modules") to produce an executable,
17 | regardless of the license terms of the Designated Exception Modules,
18 | and to copy and distribute the resulting executable under GPL2,
19 | provided that the Designated Exception Modules continue to be
20 | governed by the licenses under which they were offered by Oracle.
21 |
22 | As such, it allows licensees and sublicensees of Oracle's GPL2 OpenJDK Code
23 | to build an executable that includes those portions of necessary code that
24 | Oracle could not provide under GPL2 (or that Oracle has provided under GPL2
25 | with the Classpath exception). If you modify or add to the OpenJDK code,
26 | that new GPL2 code may still be combined with Designated Exception Modules
27 | if the new code is made subject to this exception by its copyright holder.
28 |
--------------------------------------------------------------------------------
/docs/jquery-ui.overrides.css:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved.
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 | *
5 | * This code is free software; you can redistribute it and/or modify it
6 | * under the terms of the GNU General Public License version 2 only, as
7 | * published by the Free Software Foundation. Oracle designates this
8 | * particular file as subject to the "Classpath" exception as provided
9 | * by Oracle in the LICENSE file that accompanied this code.
10 | *
11 | * This code is distributed in the hope that it will be useful, but WITHOUT
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 | * version 2 for more details (a copy is included in the LICENSE file that
15 | * accompanied this code).
16 | *
17 | * You should have received a copy of the GNU General Public License version
18 | * 2 along with this work; if not, write to the Free Software Foundation,
19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 | *
21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 | * or visit www.oracle.com if you need additional information or have any
23 | * questions.
24 | */
25 |
26 | .ui-state-active,
27 | .ui-widget-content .ui-state-active,
28 | .ui-widget-header .ui-state-active,
29 | a.ui-button:active,
30 | .ui-button:active,
31 | .ui-button.ui-state-active:hover {
32 | /* Overrides the color of selection used in jQuery UI */
33 | background: #F8981D;
34 | border: 1px solid #F8981D;
35 | }
36 |
--------------------------------------------------------------------------------
/src/test/java/PlayerTest.java:
--------------------------------------------------------------------------------
1 | import io.lyuda.jcards.Card;
2 | import io.lyuda.jcards.Hand;
3 | import io.lyuda.jcards.Rank;
4 | import io.lyuda.jcards.Suit;
5 | import io.lyuda.jcards.game.Player;
6 | import org.junit.jupiter.api.Test;
7 |
8 | import static org.junit.jupiter.api.Assertions.assertEquals;
9 |
10 | /**
11 | * The PlayerTest class provides test cases for the Player class.
12 | *
13 | * @author lyudaio
14 | * @since 0.0.2
15 | */
16 | public class PlayerTest {
17 |
18 | /**
19 | * Test case for the {@link Player#getName()} method.
20 | */
21 | @Test
22 | void testGetName() {
23 | Player player = new Player("John Doe");
24 | assertEquals("John Doe", player.getName());
25 | }
26 |
27 | /**
28 | * Test case for the {@link Player#getHand()} method.
29 | */
30 | @Test
31 | void testGetHand() {
32 | Player player = new Player("Jane Doe");
33 | Hand hand = player.getHand();
34 | assertEquals(Hand.class, hand.getClass());
35 | }
36 |
37 | /**
38 | * Test case for the {@link Player#addCard(Card)} method.
39 | */
40 | @Test
41 | void testAddCard() {
42 | Player player = new Player("Jim Doe");
43 | Card card = new Card(Rank.ACE, Suit.CLUBS);
44 | player.addCard(card);
45 | assertEquals(1, player.getHand().getCards().size());
46 | }
47 |
48 | /**
49 | * Test case for the {@link Player#removeCard(Card)} method.
50 | */
51 | @Test
52 | void testRemoveCard() {
53 | Player player = new Player("Jack Doe");
54 | Card card = new Card(Rank.ACE, Suit.CLUBS);
55 | player.addCard(card);
56 | player.removeCard(card);
57 | assertEquals(0, player.getHand().getCards().size());
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/docs/script-dir/jquery-ui.min.css:
--------------------------------------------------------------------------------
1 | /*! jQuery UI - v1.13.1 - 2022-05-12
2 | * http://jqueryui.com
3 | * Includes: core.css, autocomplete.css, menu.css
4 | * Copyright jQuery Foundation and other contributors; Licensed MIT */
5 |
6 | .ui-helper-hidden{display:none}.ui-helper-hidden-accessible{border:0;clip:rect(0 0 0 0);height:1px;margin:-1px;overflow:hidden;padding:0;position:absolute;width:1px}.ui-helper-reset{margin:0;padding:0;border:0;outline:0;line-height:1.3;text-decoration:none;font-size:100%;list-style:none}.ui-helper-clearfix:before,.ui-helper-clearfix:after{content:"";display:table;border-collapse:collapse}.ui-helper-clearfix:after{clear:both}.ui-helper-zfix{width:100%;height:100%;top:0;left:0;position:absolute;opacity:0;-ms-filter:"alpha(opacity=0)"}.ui-front{z-index:100}.ui-state-disabled{cursor:default!important;pointer-events:none}.ui-icon{display:inline-block;vertical-align:middle;margin-top:-.25em;position:relative;text-indent:-99999px;overflow:hidden;background-repeat:no-repeat}.ui-widget-icon-block{left:50%;margin-left:-8px;display:block}.ui-widget-overlay{position:fixed;top:0;left:0;width:100%;height:100%}.ui-autocomplete{position:absolute;top:0;left:0;cursor:default}.ui-menu{list-style:none;padding:0;margin:0;display:block;outline:0}.ui-menu .ui-menu{position:absolute}.ui-menu .ui-menu-item{margin:0;cursor:pointer;list-style-image:url("data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7")}.ui-menu .ui-menu-item-wrapper{position:relative;padding:3px 1em 3px .4em}.ui-menu .ui-menu-divider{margin:5px 0;height:0;font-size:0;line-height:0;border-width:1px 0 0 0}.ui-menu .ui-state-focus,.ui-menu .ui-state-active{margin:-1px}.ui-menu-icons{position:relative}.ui-menu-icons .ui-menu-item-wrapper{padding-left:2em}.ui-menu .ui-icon{position:absolute;top:0;bottom:0;left:.2em;margin:auto 0}.ui-menu .ui-menu-icon{left:auto;right:0}
--------------------------------------------------------------------------------
/docs/copy.svg:
--------------------------------------------------------------------------------
1 |
2 |
3 |
27 |
28 |
34 |
--------------------------------------------------------------------------------
/src/main/java/io/lyuda/jcards/game/Player.java:
--------------------------------------------------------------------------------
1 | package io.lyuda.jcards.game;
2 |
3 | import io.lyuda.jcards.Hand;
4 | import io.lyuda.jcards.Card;
5 |
6 | /**
7 | * The Player class represents a player in a card game.
8 | *
9 | * This class makes use of the Card and Hand classes to keep track of a player's cards and hand, respectively.
10 | * It includes methods for adding a card to the player's hand, showing the player's hand, and discarding a card.
11 | *
12 | * @author lyudaio
13 | * @since 0.0.2
14 | */
15 |
16 | public class Player {
17 |
18 | /**
19 | * The name of the player.
20 | */
21 | private String name;
22 |
23 | /**
24 | * The player's hand, which contains the cards they have in their possession.
25 | */
26 | private Hand hand;
27 |
28 | /**
29 | * Creates a new {@link Player} with the given name.
30 | *
31 | * @param name The name of the player.
32 | */
33 | public Player(String name) {
34 | this.name = name;
35 | this.hand = new Hand();
36 | }
37 |
38 | /**
39 | * Returns the name of the {@link Player}.
40 | *
41 | * @return The name of the player.
42 | */
43 | public String getName() {
44 | return name;
45 | }
46 |
47 | /**
48 | * Returns the player's current {@link Hand}.
49 | *
50 | * @return The player's current hand.
51 | */
52 | public Hand getHand() {
53 | return hand;
54 | }
55 |
56 | /**
57 | * Adds a {@link Card} to the player's hand.
58 | *
59 | * @param card The card to add to the hand.
60 | */
61 | public void addCard(Card card) {
62 | hand.addCard(card);
63 | }
64 |
65 | /**
66 | * Removes a {@link Card} from the player's {@link Hand}.
67 | *
68 | * @param card The card to remove from the hand.
69 | */
70 | public void removeCard(Card card) {
71 | hand.removeCard(card);
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/docs/legal/jqueryUI.md:
--------------------------------------------------------------------------------
1 | ## jQuery UI v1.13.1
2 |
3 | ### jQuery UI License
4 | ```
5 | Copyright jQuery Foundation and other contributors, https://jquery.org/
6 |
7 | This software consists of voluntary contributions made by many
8 | individuals. For exact contribution history, see the revision history
9 | available at https://github.com/jquery/jquery-ui
10 |
11 | The following license applies to all parts of this software except as
12 | documented below:
13 |
14 | ====
15 |
16 | Permission is hereby granted, free of charge, to any person obtaining
17 | a copy of this software and associated documentation files (the
18 | "Software"), to deal in the Software without restriction, including
19 | without limitation the rights to use, copy, modify, merge, publish,
20 | distribute, sublicense, and/or sell copies of the Software, and to
21 | permit persons to whom the Software is furnished to do so, subject to
22 | the following conditions:
23 |
24 | The above copyright notice and this permission notice shall be
25 | included in all copies or substantial portions of the Software.
26 |
27 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 |
35 | ====
36 |
37 | Copyright and related rights for sample code are waived via CC0. Sample
38 | code is defined as all source code contained within the demos directory.
39 |
40 | CC0: http://creativecommons.org/publicdomain/zero/1.0/
41 |
42 | ====
43 |
44 | All files located in the node_modules and external directories are
45 | externally maintained libraries used by this software which have their
46 | own licenses; we recommend you read them, as their terms may differ from
47 | the terms above.
48 |
49 | ```
50 |
--------------------------------------------------------------------------------
/src/test/java/RankTest.java:
--------------------------------------------------------------------------------
1 | import io.lyuda.jcards.Rank;
2 | import org.junit.jupiter.api.Test;
3 |
4 | import static org.junit.jupiter.api.Assertions.assertEquals;
5 |
6 | /**
7 | * Tests the {@code Rank} enum.
8 | *
9 | * @author lyudaio
10 | * @since 0.0.4
11 | */
12 | class RankTest {
13 |
14 | /**
15 | * Tests the {@link Rank#getValue()} method.
16 | * Verifies that the returned value is correct for each rank.
17 | */
18 | @Test
19 | void testGetValue() {
20 | assertEquals(1, Rank.ACE.getValue());
21 | assertEquals(2, Rank.TWO.getValue());
22 | assertEquals(3, Rank.THREE.getValue());
23 | assertEquals(4, Rank.FOUR.getValue());
24 | assertEquals(5, Rank.FIVE.getValue());
25 | assertEquals(6, Rank.SIX.getValue());
26 | assertEquals(7, Rank.SEVEN.getValue());
27 | assertEquals(8, Rank.EIGHT.getValue());
28 | assertEquals(9, Rank.NINE.getValue());
29 | assertEquals(10, Rank.TEN.getValue());
30 | assertEquals(10, Rank.JACK.getValue());
31 | assertEquals(10, Rank.QUEEN.getValue());
32 | assertEquals(10, Rank.KING.getValue());
33 | }
34 |
35 | /**
36 | * Tests the {@link Rank#getSymbol()} method.
37 | * Verifies that the returned symbol is correct for each rank.
38 | */
39 | @Test
40 | void testGetSymbol() {
41 | assertEquals("\uD83C\uDCA1", Rank.ACE.getSymbol());
42 | assertEquals("\uD83C\uDCA2", Rank.TWO.getSymbol());
43 | assertEquals("\uD83C\uDCA3", Rank.THREE.getSymbol());
44 | assertEquals("\uD83C\uDCA4", Rank.FOUR.getSymbol());
45 | assertEquals("\uD83C\uDCA5", Rank.FIVE.getSymbol());
46 | assertEquals("\uD83C\uDCA6", Rank.SIX.getSymbol());
47 | assertEquals("\uD83C\uDCA7", Rank.SEVEN.getSymbol());
48 | assertEquals("\uD83C\uDCA8", Rank.EIGHT.getSymbol());
49 | assertEquals("\uD83C\uDCA9", Rank.NINE.getSymbol());
50 | assertEquals("\uD83C\uDCAA", Rank.TEN.getSymbol());
51 | assertEquals("\uD83C\uDCAB", Rank.JACK.getSymbol());
52 | assertEquals("\uD83C\uDCAD", Rank.QUEEN.getSymbol());
53 | assertEquals("\uD83C\uDCAE", Rank.KING.getSymbol());
54 | }
55 | }
56 |
57 |
--------------------------------------------------------------------------------
/docs/legal/ADDITIONAL_LICENSE_INFO:
--------------------------------------------------------------------------------
1 | ADDITIONAL INFORMATION ABOUT LICENSING
2 |
3 | Certain files distributed by Oracle America, Inc. and/or its affiliates are
4 | subject to the following clarification and special exception to the GPLv2,
5 | based on the GNU Project exception for its Classpath libraries, known as the
6 | GNU Classpath Exception.
7 |
8 | Note that Oracle includes multiple, independent programs in this software
9 | package. Some of those programs are provided under licenses deemed
10 | incompatible with the GPLv2 by the Free Software Foundation and others.
11 | For example, the package includes programs licensed under the Apache
12 | License, Version 2.0 and may include FreeType. Such programs are licensed
13 | to you under their original licenses.
14 |
15 | Oracle facilitates your further distribution of this package by adding the
16 | Classpath Exception to the necessary parts of its GPLv2 code, which permits
17 | you to use that code in combination with other independent modules not
18 | licensed under the GPLv2. However, note that this would not permit you to
19 | commingle code under an incompatible license with Oracle's GPLv2 licensed
20 | code by, for example, cutting and pasting such code into a file also
21 | containing Oracle's GPLv2 licensed code and then distributing the result.
22 |
23 | Additionally, if you were to remove the Classpath Exception from any of the
24 | files to which it applies and distribute the result, you would likely be
25 | required to license some or all of the other code in that distribution under
26 | the GPLv2 as well, and since the GPLv2 is incompatible with the license terms
27 | of some items included in the distribution by Oracle, removing the Classpath
28 | Exception could therefore effectively compromise your ability to further
29 | distribute the package.
30 |
31 | Failing to distribute notices associated with some files may also create
32 | unexpected legal consequences.
33 |
34 | Proceed with caution and we recommend that you obtain the advice of a lawyer
35 | skilled in open source matters before removing the Classpath Exception or
36 | making modifications to this package which may subsequently be redistributed
37 | and/or involve the use of third party software.
38 |
--------------------------------------------------------------------------------
/src/test/java/DiceTest.java:
--------------------------------------------------------------------------------
1 | import io.lyuda.jcards.Dice;
2 | import org.junit.jupiter.api.Test;
3 |
4 | import static org.junit.jupiter.api.Assertions.*;
5 |
6 |
7 | /**
8 | * Test class for the {@link Dice} class.
9 | *
10 | * @author KonnosBaz
11 | */
12 | public class DiceTest {
13 |
14 | /**
15 | * Tests constructor for valid (positive) sides.
16 | */
17 | @Test
18 | public void testDice_validInput() {
19 | Dice dice = new Dice(6);
20 |
21 | assertNotNull(dice);
22 | }
23 |
24 | /**
25 | * Test if constructor throws exception for invalid (negative) sides.
26 | */
27 | @Test
28 | public void testDice_invalidInput() {
29 | assertThrows(IllegalArgumentException.class, () -> new Dice(-6));
30 | }
31 |
32 | /**
33 | * Tests the default consturctor.
34 | */
35 | @Test
36 | public void testDice_DefaultConstructor() {
37 | Dice dice = new Dice();
38 |
39 | assertNotNull(dice);
40 | assertEquals(dice.getSides(), 6);
41 | }
42 |
43 | /**
44 | * Tests getSides.
45 | */
46 | @Test
47 | public void testDice_getSides() {
48 | Dice dice = new Dice(5);
49 | assertEquals(5, dice.getSides());
50 | }
51 |
52 | /**
53 | * Tests roll.
54 | */
55 | @Test
56 | public void testDice_roll() {
57 | Dice dice = new Dice(4);
58 | //Repeating the test here might be redundant, but I included it since the roll() function uses
59 | //a random number generator
60 | for (int i = 0; i < 10; i++) {
61 | int result = dice.roll();
62 | assertTrue(result >= 1 & result <= dice.getSides());
63 |
64 | }
65 | }
66 |
67 | /**
68 | * Tests that the getLastRoll method throws an exception if the dice has not been rolled yet.
69 | */
70 | @Test
71 | public void testDice_getLastRoll_beforeRolling() {
72 | Dice dice = new Dice();
73 | assertThrows(IllegalStateException.class, dice::getLastRoll);
74 | }
75 |
76 | /**
77 | * Tests that the getLastRoll method works and that the lastRoll gets updated between rolls.
78 | */
79 | @Test
80 | public void testDice_getLastRoll() {
81 | Dice dice = new Dice();
82 | int result = dice.roll();
83 | assertEquals(result, dice.getLastRoll());
84 |
85 | result = dice.roll();
86 | assertEquals(result, dice.getLastRoll());
87 | }
88 | }
--------------------------------------------------------------------------------
/docs/legal/jquery.md:
--------------------------------------------------------------------------------
1 | ## jQuery v3.6.0
2 |
3 | ### jQuery License
4 | ```
5 | jQuery v 3.6.0
6 | Copyright OpenJS Foundation and other contributors, https://openjsf.org/
7 |
8 | Permission is hereby granted, free of charge, to any person obtaining
9 | a copy of this software and associated documentation files (the
10 | "Software"), to deal in the Software without restriction, including
11 | without limitation the rights to use, copy, modify, merge, publish,
12 | distribute, sublicense, and/or sell copies of the Software, and to
13 | permit persons to whom the Software is furnished to do so, subject to
14 | the following conditions:
15 |
16 | The above copyright notice and this permission notice shall be
17 | included in all copies or substantial portions of the Software.
18 |
19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 |
27 | ******************************************
28 |
29 | The jQuery JavaScript Library v3.6.0 also includes Sizzle.js
30 |
31 | Sizzle.js includes the following license:
32 |
33 | Copyright JS Foundation and other contributors, https://js.foundation/
34 |
35 | This software consists of voluntary contributions made by many
36 | individuals. For exact contribution history, see the revision history
37 | available at https://github.com/jquery/sizzle
38 |
39 | The following license applies to all parts of this software except as
40 | documented below:
41 |
42 | ====
43 |
44 | Permission is hereby granted, free of charge, to any person obtaining
45 | a copy of this software and associated documentation files (the
46 | "Software"), to deal in the Software without restriction, including
47 | without limitation the rights to use, copy, modify, merge, publish,
48 | distribute, sublicense, and/or sell copies of the Software, and to
49 | permit persons to whom the Software is furnished to do so, subject to
50 | the following conditions:
51 |
52 | The above copyright notice and this permission notice shall be
53 | included in all copies or substantial portions of the Software.
54 |
55 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
56 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
57 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
58 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
59 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
60 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
61 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
62 |
63 | ====
64 |
65 | All files located in the node_modules and external directories are
66 | externally maintained libraries used by this software which have their
67 | own licenses; we recommend you read them, as their terms may differ from
68 | the terms above.
69 |
70 | *********************
71 |
72 | ```
73 |
--------------------------------------------------------------------------------
/docs/io/lyuda/jcards/game/package-use.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Uses of Package io.lyuda.jcards.game (jcards 0.0.6 API)
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
21 |
24 |
76 |
77 |
78 |
--------------------------------------------------------------------------------
/src/main/java/io/lyuda/jcards/Dice.java:
--------------------------------------------------------------------------------
1 | package io.lyuda.jcards;
2 |
3 | import java.security.SecureRandom;
4 |
5 | /**
6 | * The {@code Dice} class represents a dice with an arbitrary amount of sides, defined in the constructor (six by default).
7 | * This class contains methods to access the number of sides, as well as rolling the dice to generate a random number.
8 | * The class uses a cryptographically secure random number generator to generate the random number. The class also
9 | * keeps track of the last result of the roll() method.
10 | *
11 | * @author KonnosBaz, lyudaio
12 | * @see java.security.SecureRandom
13 | * @since 0.0.6
14 | */
15 | public class Dice {
16 |
17 | /**
18 | * The amount of sides on the dice.
19 | */
20 | private final int sides;
21 | /**
22 | * A cryptographically secure random number generator used for rolling the dice.
23 | * Initialized in the constructor.
24 | */
25 | private final SecureRandom secureRandom;
26 | /**
27 | * A variable that holds the last result of the roll() method.
28 | */
29 | private int lastRoll;
30 |
31 | /**
32 | * Default constructor that Constructs a new {@code Dice} with six sides.
33 | * This constructor is called when no arguments are passed to the constructor.
34 | * It is equivalent to {@code Dice(6)}.
35 | * ‘this’ keyword is used to call another constructor from the same class.
36 | * @see #Dice(int)
37 | * @see #getSides()
38 | * @see #roll()
39 | */
40 | public Dice() {
41 | this(6);
42 | }
43 |
44 | /**
45 | * Constructs a new {@code Dice} with a specified number of sides.
46 | * The number of sides must be positive. If it is not, an {@code IllegalArgumentException} is thrown.
47 | * A cryptographically secure random number generator is used for rolling the dice. It is initialized in the constructor.
48 | * The lastRoll field is initialized to 0. It is assigned a value in the roll() method.
49 | *
50 | * @see #getSides()
51 | * @see #roll()
52 | * @see #getLastRoll()
53 | * @see java.security.SecureRandom
54 | * @see java.lang.IllegalArgumentException
55 | * @see java.lang.IllegalStateException
56 | *
57 | * @param sides the number of sides.
58 | * @throws IllegalArgumentException if sides are non-possitive.
59 | */
60 | public Dice(int sides) {
61 | if (sides <= 0) {
62 | throw new IllegalArgumentException("Sides can only be positive");
63 | }
64 | this.sides = sides;
65 | this.secureRandom = new SecureRandom();
66 | }
67 |
68 | /**
69 | * Returns the number of sides on the dice.
70 | *
71 | * @return the number of sides on the dice.
72 | */
73 | public int getSides() {
74 | return sides;
75 | }
76 |
77 | /**
78 | * Returns a random number between 1 and the number of {@code sides} on the dice,
79 | * using a cryptographically secure random number generator.
80 | * The result of the roll is assigned to the lastRoll field before the method returns.
81 | *
82 | * @return random number between 1 and the number of {@code sides} on the dice.
83 | */
84 | public int roll() {
85 | lastRoll = 1 + secureRandom.nextInt(sides);
86 | return lastRoll;
87 | }
88 |
89 | /**
90 | * Returns the result of the last dice roll.
91 | *
92 | * @return the result of the last dice roll.
93 | * @throws IllegalStateException if the dice has not been rolled yet.
94 | */
95 | public int getLastRoll() {
96 | if (lastRoll == 0) {
97 | throw new IllegalStateException("This dice has not yet been rolled");
98 | }
99 |
100 | return lastRoll;
101 | }
102 |
103 | }
--------------------------------------------------------------------------------
/src/main/java/io/lyuda/jcards/Suit.java:
--------------------------------------------------------------------------------
1 | package io.lyuda.jcards;
2 |
3 | /**
4 | * The {@code Suit} enum represents the suit of a playing card.
5 | * Each suit is associated with a unicode symbol, a color and a path.
6 | *
7 | * @author lyudaio, Narkoleptika (Creator of the Paths)
8 | * @since 0.0.4
9 | */
10 | public enum Suit {
11 |
12 |
13 | /**
14 | * Represents the Hearts suit with a symbol of "\u2665", a color, and a path
15 | */
16 | HEARTS("\u2665", "#f00", "M12.038 24q-.593-2.257-1.702-4.226-1.09-1.99-4.245-6.158-2.314-3.06-2.85-3.882-.88-1.339-1.281-2.448-.383-1.128-.383-2.276 0-2.122 1.416-3.557Q4.408.02 6.493.02q2.103 0 3.652 1.492 1.166 1.109 1.893 3.308.631-2.161 1.778-3.29Q15.404 0 17.488 0q2.065 0 3.5 1.435 1.434 1.415 1.434 3.385 0 1.721-.841 3.595-.842 1.855-3.251 4.877-3.137 3.958-4.57 6.502Q12.63 21.8 12.037 24z"),
17 |
18 | /**
19 | * Represents the Diamonds suit with a symbol of "\u2666", a color, and a path
20 | */
21 | DIAMONDS("\u2666", "#f00", "M11.962 0q1.702 2.888 4.188 6.196 2.869 3.825 4.628 5.794-1.415 1.492-4.628 5.776-2.6 3.48-4.169 6.234-.516-.994-1.415-2.314-1.549-2.295-3.844-5.335-.497-.67-3.5-4.36 1.932-2.2 4.973-6.273Q10.45 2.696 11.962 0z"),
22 |
23 | /**
24 | * Represents the Clubs suit with a symbol of "\u2663", a color, and a path
25 | */
26 | CLUBS("\u2663", "#000", "M20.302 24H3.796l.156-.663q3.211-.682 4.263-1.345 1.576-.994 2.569-2.944 1.012-1.95 1.012-4.114 0-.312-.02-.897-1.187 2.418-2.9 3.53-1.693 1.11-3.464 1.11-2.258 0-3.835-1.579T0 13.218q0-2.261 1.44-3.801 1.441-1.56 3.271-1.56 1.168 0 3.173.994-.818-1.364-1.07-2.047-.234-.702-.234-1.5 0-2.223 1.537-3.764T11.952 0t3.854 1.54q1.576 1.54 1.576 3.666 0 1.735-1.343 3.645 1.635-.78 1.966-.877.526-.156 1.188-.156 1.946 0 3.367 1.54Q24 10.88 24 13.141q0 2.34-1.576 3.938-1.557 1.58-3.757 1.58-1.226 0-2.53-.586-1.285-.604-2.239-1.599-.681-.72-1.693-2.437.078 3.1.934 4.914.876 1.793 2.667 2.983 1.207.799 4.34 1.403z"),
27 |
28 | /**
29 | * Represents the Spades suit with a symbol of "\u2660", a color, and a path
30 | */
31 | SPADES("\u2660", "#000", "M12.019 0q.507 2.106 1.56 3.763 1.052 1.657 3.743 4.114 2.71 2.437 3.431 3.743t.722 2.651q0 1.872-1.248 3.12t-3.041 1.248q-1.521 0-2.769-.936-1.228-.956-2.184-2.769.02 2.866.741 4.562.741 1.677 2.34 2.652 1.618.955 4.29 1.13l.136.722H4.279l.156-.721q3.587 0 5.556-2.087 1.989-2.105 1.91-6.258-.877 1.852-2.183 2.788-1.287.917-2.924.917-1.755 0-3.022-1.268-1.248-1.267-1.248-3.041 0-1.423.643-2.574.878-1.598 2.847-3.217 1.969-1.618 2.905-2.67 1.423-1.619 2.066-2.886Q11.648 1.716 12.02 0z");
32 |
33 |
34 | public static final String WHITE = "#fff";
35 | /**
36 | * The unicode symbol representing the suit.
37 | */
38 | private final String symbol;
39 |
40 | /**
41 | * The color of the suit.
42 | */
43 | private final String color;
44 |
45 | /**
46 | * The path representing the suit.
47 | */
48 | private final String path;
49 |
50 | /**
51 | * Constructs a {@code Suit} with a given symbol, color and path.
52 | *
53 | * @param symbol the unicode symbol of the suit.
54 | * @param color the color of the suit.
55 | * @param path the path representing the suit.
56 | */
57 | Suit(String symbol, String color, String path) {
58 | this.symbol = symbol;
59 | this.color = color;
60 | this.path = path;
61 | }
62 |
63 | /**
64 | * Returns the unicode symbol of the suit.
65 | *
66 | * @return the unicode symbol of the suit.
67 | */
68 | public String getSymbol() {
69 | return symbol;
70 | }
71 |
72 | /**
73 | * Returns the color of the suit.
74 | *
75 | * @return the color of the suit.
76 | */
77 | public String getColor() {
78 | return color;
79 | }
80 |
81 | /**
82 | * Returns the path of the suit.
83 | *
84 | * @return the path of the suit.
85 | */
86 | public String getPath() {
87 | return path;
88 | }
89 | }
90 |
--------------------------------------------------------------------------------
/docs/search.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Search (jcards 0.0.6 API)
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
21 |
24 |
The help page provides an introduction to the scope and syntax of JavaDoc search.
58 |
You can use the <ctrl> or <cmd> keys in combination with the left and right arrow keys to switch between result tabs in this page.
59 |
The URL template below may be used to configure this page as a search engine in browsers that support this feature. It has been tested to work in Google Chrome and Mozilla Firefox. Note that other browsers may not support this feature or require a different URL format.
60 | link
61 |
62 |
63 |
64 |
65 |
Loading search index...
66 |
67 |
68 |
69 |
70 |
71 |
75 |
76 |
77 |
78 |
79 |
--------------------------------------------------------------------------------
/src/main/java/io/lyuda/jcards/Card.java:
--------------------------------------------------------------------------------
1 | package io.lyuda.jcards;
2 |
3 | import java.util.Objects;
4 |
5 |
6 | /**
7 | * The {@code Card} class represents a standard playing card with a rank and a suit.
8 | * This class provides methods to access the rank and suit of a card, as well as to compare
9 | * cards for equality and to obtain a string representation of a card.
10 | *
11 | * @author lyudaio
12 | * @since 0.0.1
13 | */
14 | public class Card implements Comparable {
15 | private final Rank rank;
16 | private final Suit suit;
17 |
18 | /**
19 | * Constructs a new {@code Card} with the specified rank and suit.
20 | *
21 | * @param rank the rank of the card, cannot be {@code null}
22 | * @param suit the suit of the card, cannot be {@code null}
23 | * @throws IllegalArgumentException if rank or suit is {@code null}
24 | */
25 | public Card(Rank rank, Suit suit) {
26 | if (rank == null) {
27 | throw new IllegalArgumentException("Rank cannot be null");
28 | }
29 | if (suit == null) {
30 | throw new IllegalArgumentException("Suit cannot be null");
31 | }
32 | this.rank = rank;
33 | this.suit = suit;
34 | }
35 |
36 | /**
37 | * Returns the rank of the card.
38 | *
39 | * @return the rank of the card
40 | */
41 | public Rank getRank() {
42 | return rank;
43 | }
44 |
45 |
46 | /**
47 | * Returns the suit of the card.
48 | *
49 | * @return The suit of the card.
50 | */
51 | public Suit getSuit() {
52 | return suit;
53 | }
54 |
55 | /**
56 | * This method returns a String representation of an SVG image built from the
57 | * {@link CardImage} class.
58 | *
59 | * @return a String representation of an SVG image.
60 | * @see CardImage
61 | */
62 | public String getSVG() {
63 | return CardImage.makeCard(suit.getPath(), rank.getPath(), CardImage.Card.CARD.getColor(), suit.getColor());
64 | }
65 |
66 | /**
67 | * Returns a string representation of the card.
68 | * The string representation is of the format "rank of suit".
69 | *
70 | * @return a string representation of the card
71 | */
72 | @Override
73 | public String toString() {
74 | return rank + " of " + suit;
75 | }
76 |
77 |
78 | /**
79 | * Overrides the default hashCode method and returns a hash code based on the rank and suit values of this Card.
80 | *
81 | * @return an int value representing the hash code of this Card object.
82 | */
83 | @Override
84 | public int hashCode() {
85 | return Objects.hash(rank, suit);
86 | }
87 |
88 |
89 | /**
90 | * Compares this card with the specified object for equality.
91 | * Two cards are considered equal if and only if their rank and suit are equal.
92 | *
93 | * @param obj the object to compare with this card
94 | * @return {@code true} if the specified object is equal to this card,
95 | * {@code false} otherwise.
96 | */
97 | @Override
98 | public boolean equals(Object obj) {
99 | if (this == obj) {
100 | return true;
101 | }
102 |
103 | if (!(obj instanceof Card other)) {
104 | return false;
105 | }
106 |
107 | return rank == other.rank && suit == other.suit;
108 | }
109 |
110 | /**
111 | * Compares this card with the specified card for order.
112 | *
113 | * @param other the card to be compared
114 | * @return a negative integer, zero, or a positive integer as this card is less than, equal to, or greater than the specified card
115 | */
116 | @Override
117 | public int compareTo(Card other) {
118 | int rankCompare = Integer.compare(rank.getValue(), other.rank.getValue());
119 | if (rankCompare != 0) {
120 | return rankCompare;
121 | }
122 | return suit.compareTo(other.suit);
123 | }
124 | }
125 |
--------------------------------------------------------------------------------
/docs/constant-values.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Constant Field Values (jcards 0.0.6 API)
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
21 |
24 |
84 |
85 |
86 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |    
2 |
3 |
4 |
5 |
6 |
7 | # jCards - A Lightweight Java Library for manipulating Playing Cards
8 |
9 | jCards is a Java library for creating and manipulating a deck of playing cards. It is designed to be simple, intuitive, and easy to extend. With jCards, you can quickly and easily create and manipulate a deck of playing cards in your Java projects while retaining agnosticism towards the type of card game.
10 |
11 | ## Features
12 |
13 | - Programmatically generated card images to SVG
14 | - Enumerated types for Rank and Suit, providing a convenient and efficient way to represent the various ranks and suits of a deck of cards.
15 | - A `Card` class for representing individual cards, with convenient methods for getting the rank and suit of a card.
16 | - Overridden `toString()` method for easy printing of cards, making it simple to visualize the contents of a deck or individual cards.
17 | - Overridden `hashCode()` and `equals()` methods for efficient collections handling, allowing you to easily add, remove, and manipulate cards within collections.
18 | - Shuffles deck of cards using a unique SecureRandom seed each time it is called
19 | - A Player class to represent a player in a card game, providing methods to manage their hand, add and remove cards, and access their name.
20 | - Agnostic design to allow for use in various types of card games.
21 | - DeckFactory class to manage multiple decks at a time.
22 |
23 | ## Getting Started
24 |
25 | Make sure you have `Java 17 LTS` installed
26 |
27 | To use jCards in your project, simply include the latest version of the library in your classpath. You can download the latest version from the releases page on GitHub.
28 |
29 | Here is a simple example of how to use jCards:
30 |
31 | ```java
32 | import io.lyuda.jCards.Card;
33 | import io.lyuda.jCards.Rank;
34 | import io.lyuda.jCards.Suit;
35 |
36 | public class Main {
37 | public static void main(String[] args) {
38 | Card aceOfSpades = new Card(Rank.ACE, Suit.SPADES);
39 | System.out.println(aceOfSpades);
40 | }
41 | }
42 | ```
43 |
44 | This will output `ACE of SPADES`.
45 |
46 | # Documentation
47 |
48 | Documentation is automatically generated as Javadocs. We try to keep the library up to Javadoc standards so that the generated documentation is informative. [You can read the Javadocs here](https://jcards.lyuda.io).
49 |
50 | # Contributing
51 |
52 | We welcome and encourage contributions to jCards! Whether you're fixing a bug, adding a new feature, or simply improving the documentation, your help is greatly appreciated.
53 |
54 | ## Submitting a Pull Request
55 |
56 | If you would like to contribute code to jCards, you can do so by submitting a pull request on GitHub.
57 |
58 | 1. Fork the repository on GitHub.
59 | 2. Clone your fork to your local machine.
60 | 3. Create a new branch for your changes.
61 | 4. Make the necessary changes in the code.
62 | 5. Push the changes to your fork on GitHub.
63 | 6. Submit a pull request, including a detailed description of your changes and why they are needed.
64 |
65 | ## Raising an Issue
66 |
67 | If you find a bug in jCards, or if you have an idea for a new feature, you can raise an issue on GitHub. When raising an issue, please include a clear and concise description of the problem, and any relevant details such as error messages, screenshots, and/or code snippets.
68 |
69 | We look forward to your contributions!
70 |
71 |
72 | ## License
73 |
74 | jCards is licensed under the MIT license. This means that you are free to use, modify, and distribute the library for any purpose, as long as you include the license and copyright notice in your distribution.
75 |
76 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 | 4.0.0
7 |
8 |
9 |
10 |
11 | io.lyuda
12 | jcards
13 | 0.0.6
14 | jar
15 |
16 | https://github.com/lyudaio/jcards
17 | A Java library for creating and manipulating a deck of playing cards.
18 |
19 |
20 |
21 | MIT License
22 | https://opensource.org/licenses/MIT
23 |
24 |
25 |
26 |
27 | scm:git:https://github.com/lyuda/jcards.git
28 | scm:git:git@github.com:lyuda/jcards.git
29 | https://github.com/lyuda/jcards
30 |
31 |
32 |
33 |
34 | lyuda
35 | Lyuda
36 | developers@lyuda.io
37 |
38 |
39 |
40 |
41 |
42 | org.junit.jupiter
43 | junit-jupiter-api
44 | 5.9.3
45 | test
46 |
47 |
48 |
49 |
50 |
51 |
52 | org.junit.jupiter
53 | junit-jupiter-api
54 | 5.9.3
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 | org.apache.maven.plugins
63 | maven-compiler-plugin
64 | 3.11.0
65 |
66 | 17
67 | 17
68 |
69 |
70 |
71 |
72 |
73 | org.apache.maven.plugins
74 | maven-jar-plugin
75 | 3.3.0
76 |
77 |
78 |
79 | true
80 | lib/
81 | io.lyuda.jcards.Main
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 | default
92 |
93 | true
94 |
95 |
96 |
97 |
98 | org.apache.maven.plugins
99 | maven-javadoc-plugin
100 | 3.5.0
101 |
102 |
103 | attach-javadocs
104 |
105 | jar
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
--------------------------------------------------------------------------------
/src/main/java/io/lyuda/jcards/Hand.java:
--------------------------------------------------------------------------------
1 | package io.lyuda.jcards;
2 |
3 | import java.util.*;
4 |
5 | /**
6 | * The {@code Hand} class represents a hand of cards in a card game.
7 | * This class provides methods to add or remove cards from the hand, as well as to
8 | * obtain a list of the cards in the hand, get the size of the hand, sort the cards,
9 | * and find a specific card in the hand.
10 | *
11 | * @author lyudaio
12 | * @since 0.0.1
13 | */
14 | public class Hand {
15 |
16 | /**
17 | * The list of cards in this hand.
18 | */
19 | private final List cards;
20 |
21 | /**
22 | * Constructs a new {@code Hand} with an empty list of cards.
23 | */
24 | public Hand() {
25 | cards = new ArrayList<>();
26 | }
27 |
28 | /**
29 | * Constructs a new {@code Hand} with the given list of cards.
30 | *
31 | * @param cards the list of cards to include in this hand, must not be {@code null}
32 | * @throws NullPointerException if the provided list of cards is {@code null}
33 | */
34 | public Hand(List cards) {
35 | this.cards = Objects.requireNonNull(cards, "Cards cannot be null");
36 | }
37 |
38 | /**
39 | * Adds a card to the hand.
40 | *
41 | * @param card the card to add, must not be {@code null}
42 | * @throws NullPointerException if the provided card is {@code null}
43 | */
44 | public void addCard(Card card) {
45 | Objects.requireNonNull(card, "Card cannot be null");
46 | cards.add(card);
47 | }
48 |
49 | /**
50 | * Removes the specified {@link Card} from this {@link Hand}.
51 | *
52 | * @param card the card to be removed, must not be {@code null}
53 | * @return {@code true} if the card was present and removed successfully,
54 | * {@code false} otherwise
55 | * @throws NullPointerException if the specified card is {@code null}
56 | */
57 | public boolean removeCard(Card card) {
58 | Objects.requireNonNull(card, "Card cannot be null");
59 | return cards.remove(card);
60 | }
61 |
62 | /**
63 | * Gets the size of this hand.
64 | *
65 | * @return the number of cards in this hand
66 | */
67 | public int size() {
68 | return cards.size();
69 | }
70 |
71 | /**
72 | * Gets a list of all the cards in this hand.
73 | *
74 | * @return a modifiable list of the cards in this hand
75 | */
76 | public List getCards() {
77 | return cards;
78 | }
79 |
80 | /**
81 | * Sorts the cards in this hand.
82 | *
83 | * This method uses the Collections framework to sort the cards stored in the `cards` list based on their natural ordering,
84 | * as determined by the `compareTo()` method implemented in the `Card` class.
85 | *
86 | * @see java.util.Collections#sort(java.util.List)
87 | * @see io.lyuda.jcards.Card#compareTo(io.lyuda.jcards.Card)
88 | */
89 | public void sort() {
90 | Collections.sort(cards);
91 | }
92 |
93 |
94 | /**
95 | * Finds a card with the specified rank and suit in this hand.
96 | *
97 | * @param rank the rank of the card to find
98 | * @param suit the suit of the card to find
99 | * @return an {@link Optional} containing the found card, or an empty {@link Optional} if no such card was found
100 | */
101 | public Optional findCard(Rank rank, Suit suit) {
102 | return cards.stream().filter(c -> c.getRank() == rank && c.getSuit() == suit).findFirst();
103 | }
104 |
105 | /**
106 | * Returns a string representation of this hand, listing the rank and suit of each card in the hand.
107 | * The format of the returned string is "Hand: [Rank1 of Suit1], [Rank2 of Suit2], ... [RankN of SuitN]".
108 | *
109 | * @return a string representation of this hand
110 | */
111 | @Override
112 | public String toString() {
113 | StringBuilder sb = new StringBuilder();
114 | sb.append("Hand: ");
115 | for (Card card : cards) {
116 | sb.append(card.toString());
117 | sb.append(", ");
118 | }
119 | if (cards.size() > 0) {
120 | sb.delete(sb.length() - 2, sb.length());
121 | }
122 | return sb.toString();
123 | }
124 |
125 | }
--------------------------------------------------------------------------------
/src/test/java/DeckFactoryTest.java:
--------------------------------------------------------------------------------
1 | import io.lyuda.jcards.Deck;
2 | import io.lyuda.jcards.DeckFactory;
3 | import org.junit.jupiter.api.Test;
4 |
5 | import java.util.List;
6 |
7 | import static org.junit.jupiter.api.Assertions.*;
8 |
9 | public class DeckFactoryTest {
10 |
11 | /**
12 | * Test case for the {@link DeckFactory#createDeck()} method.
13 | * This test ensures that the createDeck() method correctly creates a new non-null {@link Deck} object to be managed by
14 | * {@link DeckFactory}.
15 | */
16 | @Test
17 | public void testCreateDeck() {
18 | DeckFactory factory = new DeckFactory();
19 | Deck deck = factory.createDeck();
20 | List decks = factory.getDecks();
21 |
22 | assertNotNull(deck);
23 | assertEquals(1, decks.size());
24 | assertEquals(deck, decks.get(0));
25 | }
26 |
27 |
28 | /**
29 | * Test case for the {@link DeckFactory#getDecks()} method.
30 | * This test ensures that the getDecks method correctly returns a list {@link Deck} objects manged by the factory
31 | */
32 | @Test
33 | public void testGetDecks() {
34 | DeckFactory factory = new DeckFactory();
35 | Deck deck1 = factory.createDeck();
36 | Deck deck2 = factory.createDeck();
37 | List decks = factory.getDecks();
38 |
39 | assertEquals(2, decks.size());
40 | assertEquals(deck1, decks.get(0));
41 | assertEquals(deck2, decks.get(1));
42 | }
43 |
44 |
45 | /**
46 | * Test case for the {@link DeckFactory#getDeck(int)} method.
47 | * This test ensures that the getDeck method correctly returns a {@link Deck} object at the specified index..
48 | */
49 | @Test
50 | public void testGetDeck() {
51 | DeckFactory factory = new DeckFactory();
52 | Deck deck1 = factory.createDeck();
53 | Deck deck2 = factory.createDeck();
54 | Deck deck3 = factory.createDeck();
55 | Deck returnedDeck1 = factory.getDeck(0);
56 | Deck returnedDeck2 = factory.getDeck(1);
57 | Deck returnedDeck3 = factory.getDeck(2);
58 | Deck returnedDeck4 = factory.getDeck(3);
59 |
60 | assertEquals(deck1, returnedDeck1);
61 | assertEquals(deck2, returnedDeck2);
62 | assertEquals(deck3, returnedDeck3);
63 | assertNull(returnedDeck4);
64 | }
65 |
66 | /**
67 | * Test case for the {@link DeckFactory#removeDeck(int)} method.
68 | * This test ensures that the removeDeck method correctly removes a Deck object from the list of Decks in DeckFactory.
69 | * It also verifies that the method returns the expected boolean value indicating success or failure of the removal operation.
70 | */
71 | @Test
72 | public void testRemoveDeck() {
73 | DeckFactory factory = new DeckFactory();
74 | Deck deck1 = factory.createDeck();
75 | Deck deck2 = factory.createDeck();
76 | Deck deck3 = factory.createDeck();
77 | List decks = factory.getDecks();
78 |
79 | assertTrue(factory.removeDeck(1));
80 | assertEquals(2, decks.size());
81 | assertEquals(deck1, decks.get(0));
82 | assertEquals(deck3, decks.get(1));
83 | assertFalse(factory.removeDeck(3));
84 | }
85 |
86 | /**
87 | * Test case for the {@link DeckFactory#shuffleAllDecks()} method.
88 | * This test ensures that the shuffleAllDecks method correctly shuffles all the Deck objects in the list of Decks in DeckFactory.
89 | */
90 | @Test
91 | public void testShuffleAllDecks() {
92 | DeckFactory factory = new DeckFactory();
93 | Deck deck1 = factory.createDeck();
94 | Deck deck2 = factory.createDeck();
95 | List decks = factory.getDecks();
96 |
97 | factory.shuffleAllDecks();
98 | assertNotEquals(deck1.getCards(), deck2.getCards());
99 | factory.shuffleAllDecks();
100 | assertNotEquals(deck1.getCards(), deck2.getCards());
101 | }
102 |
103 | /**
104 | * Test case for the {@link DeckFactory#shuffleDeck(int)} method.
105 | * This test ensures that the shuffleDeck method correctly shuffles the specified Deck object in the list of Decks in DeckFactory.
106 | */
107 | @Test
108 | public void testShuffleDeck() {
109 | DeckFactory factory = new DeckFactory();
110 | Deck deck1 = factory.createDeck();
111 | Deck deck2 = factory.createDeck();
112 | List decks = factory.getDecks();
113 |
114 | for (Deck deck : decks) {
115 | deck.shuffle();
116 | }
117 |
118 | assertNotEquals(deck1.getCards(), deck2.getCards());
119 | factory.shuffleDeck(1);
120 | assertNotEquals(deck1.getCards(), deck2.getCards());
121 | }
122 |
123 |
124 | }
125 |
--------------------------------------------------------------------------------
/docs/io/lyuda/jcards/class-use/Hand.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Uses of Class io.lyuda.jcards.Hand (jcards 0.0.6 API)
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
21 |
24 |
89 |
90 |
91 |
--------------------------------------------------------------------------------
/src/main/java/io/lyuda/jcards/DeckFactory.java:
--------------------------------------------------------------------------------
1 | package io.lyuda.jcards;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | /**
7 | * The {@code DeckFactory} class is used to create and manage multiple decks of cards.
8 | *
9 | * This class provides functionality for creating and keeping track of multiple {@link Deck} objects,
10 | * each of which can be shuffled, dealt from, and managed independently. The factory allows you to
11 | * easily create, shuffle, deal, and manage a collection of decks.
12 | *
13 | * @author lyudaio
14 | * @since 0.0.3
15 | */
16 | public class DeckFactory {
17 |
18 | /**
19 | * The `decks` member variable represents a collection of all the decks created by the factory.
20 | * It is used to store and manage all the decks created by the factory.
21 | * This variable is private and is only accessible within the class.
22 | */
23 | private final List decks;
24 |
25 |
26 | /**
27 | * Constructor for the DeckFactory class that creates a specified number of {@link Deck} objects.
28 | *
29 | * @param amount The number of decks to create.
30 | */
31 | public DeckFactory(int amount) {
32 | this.decks = new ArrayList<>();
33 | for (int i = 0; i < amount; i++) {
34 | createDeck();
35 | }
36 | }
37 |
38 | /**
39 | * Default constructor for the DeckFactory class.
40 | * This creates an empty list of {@link Deck} objects.
41 | */
42 | public DeckFactory() {
43 | this.decks = new ArrayList<>();
44 | }
45 |
46 |
47 | /**
48 | * Creates a new deck of cards and adds it to the list of decks managed by the factory.
49 | *
50 | * @return the created deck of cards
51 | */
52 | public Deck createDeck() {
53 | Deck deck = new Deck();
54 | decks.add(deck);
55 | return deck;
56 | }
57 |
58 | /**
59 | * Gets a list of all the decks created by the factory.
60 | *
61 | * @return a list of all the decks created by the factory
62 | */
63 | public List getDecks() {
64 | return decks;
65 | }
66 |
67 | /**
68 | * Gets a specific deck by its index in the list of decks managed by the factory.
69 | *
70 | * @param index the index of the deck to get
71 | * @return the deck at the specified index, or {@code null} if the index is invalid
72 | */
73 | public Deck getDeck(int index) {
74 | if (index < 0 || index >= decks.size()) {
75 | return null;
76 | }
77 | return decks.get(index);
78 | }
79 |
80 | /**
81 | * Removes a deck from the list of decks managed by the factory.
82 | *
83 | * @param index the index of the deck to remove
84 | * @return {@code true} if the deck was successfully removed, {@code false} otherwise
85 | */
86 | public boolean removeDeck(int index) {
87 | if (index < 0 || index >= decks.size()) {
88 | return false;
89 | }
90 | decks.remove(index);
91 | return true;
92 | }
93 |
94 | /**
95 | * Shuffles all the decks in the list of decks managed by the factory.
96 | *
97 | * @see Deck#shuffle()
98 | */
99 | public void shuffleAllDecks() {
100 | for (Deck deck : decks) {
101 | deck.shuffle();
102 | }
103 | }
104 |
105 | /**
106 | * Shuffles a specific deck by its index in the list of decks managed by the factory.
107 | *
108 | * @param index the index of the deck to shuffle
109 | * @return {@code true} if the deck was successfully shuffled, {@code false} otherwise
110 | */
111 | public boolean shuffleDeck(int index) {
112 | if (index < 0 || index >= decks.size()) {
113 | return false;
114 | }
115 | Deck deck = decks.get(index);
116 | deck.shuffle();
117 | return true;
118 | }
119 |
120 | /**
121 | * Removes all the decks from the list of decks managed by the factory.
122 | */
123 | public void clearDecks() {
124 | decks.clear();
125 | }
126 |
127 | /**
128 | * Sorts all the decks in the list of decks managed by the factory.
129 | */
130 | public void sortAllDecks() {
131 | for (Deck deck : decks) {
132 | deck.sort();
133 | }
134 | }
135 |
136 | /**
137 | * Adds a deck to the list of decks managed by the factory.
138 | *
139 | * @param deck the deck to add to the list of decks
140 | */
141 | public void addDeck(Deck deck) {
142 | decks.add(deck);
143 | }
144 |
145 | /**
146 | * Returns the number of decks in the list of decks.
147 | *
148 | * @return the number of decks in the list of decks
149 | */
150 | public int getDeckCount() {
151 | return decks.size();
152 | }
153 | }
154 |
--------------------------------------------------------------------------------
/docs/io/lyuda/jcards/game/package-summary.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | io.lyuda.jcards.game (jcards 0.0.6 API)
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
21 |
24 |
121 |
122 |
123 |
--------------------------------------------------------------------------------
/src/main/java/io/lyuda/jcards/Deck.java:
--------------------------------------------------------------------------------
1 | package io.lyuda.jcards;
2 |
3 | import java.security.SecureRandom;
4 | import java.util.ArrayList;
5 | import java.util.Collections;
6 | import java.util.Iterator;
7 | import java.util.List;
8 | import java.util.Random;
9 |
10 | /**
11 | * The {@code Deck} class represents a deck of cards. It is used to create and manage a collection of cards.
12 | *
13 | *
A deck of cards is created with all the possible combinations of suits and ranks, and can
14 | * be shuffled and dealt from. The deck keeps track of the number of cards remaining and throws
15 | * an exception if an attempt is made to deal from an empty deck.
16 | *
17 | * @author lyudaio
18 | * @since 0.0.1
19 | */
20 | public class Deck implements Comparable {
21 |
22 | /**
23 | * The `cards` member variable represents a collection of cards in a deck.
24 | * It is used to store and manage all the cards in the deck.
25 | * This variable is private and is only accessible within the class.
26 | */
27 | private final List cards;
28 |
29 | private final SecureRandom secureRandom;
30 | /**
31 | * Creates a new deck of cards with all the possible combinations of suits and ranks.
32 | */
33 | public Deck() {
34 | cards = new ArrayList<>();
35 | for (Suit suit : Suit.values()) {
36 | for (Rank rank : Rank.values()) {
37 | cards.add(new Card(rank, suit));
38 | }
39 | }
40 | this.secureRandom = new SecureRandom();
41 | }
42 |
43 | /**
44 | * Shuffles the deck of cards using a cryptographically secure random number generator.
45 | *
46 | * @see java.util.Collections#shuffle(List, Random)
47 | * @see java.security.SecureRandom
48 | */
49 | public void shuffle() {
50 | long seed = secureRandom.nextLong();
51 | Collections.shuffle(cards, new Random(seed));
52 | }
53 |
54 | /**
55 | * Deals a specified amount of cards from the deck.
56 | *
57 | * @param amount the number of cards to be dealt
58 | * @return a list of dealt cards
59 | * @throws IllegalStateException if the deck is empty or if the amount of cards to be dealt is greater than the number of cards in the deck
60 | */
61 | public List deal(int amount) {
62 | if (cards.isEmpty()) {
63 | throw new IllegalStateException("No more cards in the deck");
64 | }
65 | if (amount > cards.size()) {
66 | throw new IllegalStateException("Cannot deal more cards than are in the deck");
67 | }
68 | List dealtCards = new ArrayList<>();
69 | for (int i = 0; i < amount; i++) {
70 | dealtCards.add(cards.remove(0));
71 | }
72 | return dealtCards;
73 | }
74 |
75 | /**
76 | * Deals the next card from the deck.
77 | *
78 | * @return the next card in the deck
79 | * @throws IllegalStateException if the deck is empty
80 | */
81 | public Card deal() {
82 | if (cards.isEmpty()) {
83 | throw new IllegalStateException("No more cards in the deck");
84 | }
85 | return cards.remove(0);
86 | }
87 |
88 | /**
89 | * Splits the deck into a specified number of hands. If the cards cannot be split evenly,
90 | * the hands will have different numbers of cards.
91 | *
92 | * @param players the number of hands to split the deck into
93 | * @return a list of hands
94 | */
95 | public List split(int players) {
96 | List hands = new ArrayList<>();
97 | for (int i = 0; i < players; i++) {
98 | hands.add(new Hand());
99 | }
100 | Iterator cardIterator = cards.iterator();
101 | int playerDeal = 0;
102 | while(cardIterator.hasNext()) {
103 | if(playerDeal == players) {
104 | playerDeal = 0;
105 | }
106 | Card c = cardIterator.next();
107 | hands.get(playerDeal).addCard(c);
108 | cardIterator.remove();
109 | playerDeal++;
110 | }
111 | return hands;
112 | }
113 |
114 | /**
115 | * Searches the deck for a specific card based on its rank and suit.
116 | *
117 | * @param rank the rank of the card to search for
118 | * @param suit the suit of the card to search for
119 | * @return the index of the card in the deck, 0 if empty, or -1 if the card is not found
120 | */
121 | public int findCard(Rank rank, Suit suit) {
122 | if (cards.isEmpty()) {
123 | return 0;
124 | }
125 |
126 | for (int i = 0; i < cards.size(); i++) {
127 | Card card = cards.get(i);
128 | if (card.getRank() == rank && card.getSuit() == suit) {
129 | return i;
130 | }
131 | }
132 | return -1;
133 | }
134 |
135 | /**
136 | * Searches the deck for a specific rank.
137 | *
138 | * @param rank the rank to search for
139 | * @return a list of the indices of the cards in the deck with the specified rank
140 | */
141 | public List findCardsByRank(Rank rank) {
142 | List indices = new ArrayList<>();
143 | for (int i = 0; i < cards.size(); i++) {
144 | if (cards.get(i).getRank() == rank) {
145 | indices.add(i);
146 | }
147 | }
148 | return indices;
149 | }
150 |
151 | /**
152 | * Searches the deck for a specific suit.
153 | *
154 | * @param suit the suit to search for
155 | * @return a list of the indices of the cards in the deck with the specified suit
156 | */
157 | public List findCardsBySuit(Suit suit) {
158 | List indices = new ArrayList<>();
159 | for (int i = 0; i < cards.size(); i++) {
160 | if (cards.get(i).getSuit() == suit) {
161 | indices.add(i);
162 | }
163 | }
164 | return indices;
165 | }
166 |
167 | /**
168 | * Gets the number of cards in the deck.
169 | *
170 | * @return the number of cards in the deck
171 | */
172 | public int getSize() {
173 | return cards.size();
174 | }
175 |
176 | /**
177 | * Gets the card at the specified index in the deck.
178 | *
179 | * @param index the index of the desired card
180 | * @return the card at the specified index
181 | * @throws IndexOutOfBoundsException if the index is out of range (index (less than) 0 || index >= deck size)
182 | */
183 | public Card getCardAtIndex(int index) throws IndexOutOfBoundsException {
184 | if (index < 0 || index >= getSize()) {
185 | throw new IndexOutOfBoundsException("The specified index is out of range: " + index);
186 | }
187 | return cards.get(index);
188 | }
189 |
190 | /**
191 | * Compares two deck of cards based on their size.
192 | *
193 | * @param o the deck to compare to
194 | * @return a negative integer if this deck has less cards than `o`,
195 | * zero if this deck has the same number of cards as `o`,
196 | * or a positive integer if this deck has more cards than `o`.
197 | */
198 | @Override
199 | public int compareTo(Deck o) {
200 | return Integer.compare(this.cards.size(), o.cards.size());
201 | }
202 |
203 |
204 | /**
205 | * Retrieves the most up-to-date list of cards stored in the object.
206 | *
207 | * @return A list of {@link Card} objects stored in the object, which reflects any changes made to the underlying list.
208 | *
209 | *
The returned list may be empty, but it will never be
210 | * {@code null}.
211 | * @author lyudaio
212 | * @see Card
213 | * @since 0.0.3
214 | */
215 | public List getCards() {
216 | return cards;
217 | }
218 |
219 | /**
220 | * Sorts the deck of cards using the {@link Card#compareTo(Card)} method.
221 | *
222 | * @see java.util.Collections#sort(List)
223 | */
224 | public void sort() {
225 | Collections.sort(cards);
226 | }
227 |
228 | /**
229 | * Clears all the cards from the deck.
230 | */
231 | public void clear() {
232 | cards.clear();
233 | }
234 |
235 | public SecureRandom getSecureRandom() {
236 | return secureRandom;
237 | }
238 | }
239 |
--------------------------------------------------------------------------------
/docs/io/lyuda/jcards/package-summary.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | io.lyuda.jcards (jcards 0.0.6 API)
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
21 |
24 |
The purpose of this class is to thoroughly test the functionality of the {@link Deck} class.
20 | * All methods are tested to ensure that they behave as expected, and edge cases are also considered.
21 | *
22 | * @author lyudaio
23 | * @since 0.0.1
24 | */
25 | public class DeckTest {
26 | private Deck deck;
27 |
28 | /**
29 | * Initializes a new deck of cards for each test.
30 | */
31 | @BeforeEach
32 | public void setUp() {
33 | deck = new Deck();
34 | }
35 |
36 | /**
37 | * Test the creation of a new Deck of cards.
38 | * This test verifies that the deck is correctly initialized with 52 cards, each with a unique rank and suit.
39 | *
40 | * @author lyudaio
41 | * @since 0.0.1
42 | */
43 | @Test
44 | public void testCreation() {
45 | Deck deck = new Deck();
46 | assertEquals(52, deck.getSize(), "The deck should have 52 cards after creation");
47 |
48 | Set uniqueCards = new HashSet<>();
49 | for (int i = 0; i < 52; i++) {
50 | Card card = deck.getCardAtIndex(i);
51 | assertFalse(uniqueCards.contains(card), "Each card in the deck should be unique");
52 | uniqueCards.add(card);
53 | }
54 | }
55 |
56 | /**
57 | * Tests shuffling the deck of cards.
58 | *
59 | *
This test verifies that shuffling the deck of cards changes the order of the cards, and
60 | * that the order is different each time the deck is shuffled.
61 | */
62 | @Test
63 | public void testShuffle() {
64 | Deck deck = new Deck();
65 | List originalCards = new ArrayList<>(deck.getCards());
66 | deck.shuffle();
67 | int matchCount = 0;
68 | for (int i = 0; i < originalCards.size(); i++) {
69 | if (originalCards.get(i).equals(deck.getCards().get(i))) {
70 | matchCount++;
71 | }
72 | }
73 | assertNotEquals(matchCount, originalCards.size(), "The deck was randomly shuffled into the same order");
74 | }
75 |
76 | /**
77 | * Tests dealing a card from the deck.
78 | *
79 | *
This test verifies that dealing a card from the deck removes the card from the deck and
80 | * reduces the number of cards remaining. An exception is also thrown if an attempt is made to
81 | * deal from an empty deck.
82 | */
83 | @Test
84 | public void testDeal() {
85 | int size1 = deck.getSize();
86 | deck.deal();
87 | int size2 = deck.getSize();
88 |
89 | assertEquals(size1 - 1, size2);
90 |
91 | deck = new Deck();
92 | for (int i = 0; i < 52; i++) {
93 | deck.deal();
94 | }
95 | try {
96 | deck.deal();
97 | fail();
98 | } catch (IllegalStateException e) {
99 | assertEquals("No more cards in the deck", e.getMessage());
100 | }
101 | }
102 |
103 | /**
104 | * Tests splitting the deck into two hands.
105 | *
106 | *
This test verifies that the deck is split into two hands, and that the cards are distributed
107 | * evenly between the two hands.
108 | */
109 | @Test
110 | public void testSplit() {
111 | List hands = deck.split(2);
112 | assertEquals(hands.size(), 2);
113 | assertTrue(hands.get(0).getCards().contains(new Card(Rank.ACE, Suit.HEARTS)));
114 | assertFalse(hands.get(1).getCards().contains(new Card(Rank.ACE, Suit.HEARTS)));
115 | assertTrue(hands.get(1).getCards().contains(new Card(Rank.TWO, Suit.HEARTS)));
116 | assertFalse(hands.get(0).getCards().contains(new Card(Rank.TWO, Suit.HEARTS)));
117 | assertEquals(hands.get(0).size() + hands.get(1).size(), 52);
118 | }
119 |
120 | /**
121 | * Test method to check the `findCard` method of the `Deck` class. This test verifies that the `findCard` method is
122 | * able to locate a card with rank SEVEN and suit SPADES in the shuffled deck.
123 | */
124 | @Test
125 | public void testFindCard() {
126 | Deck deck = new Deck();
127 | deck.shuffle();
128 | int indexOfSevenOfSpades = deck.findCard(Rank.SEVEN, Suit.SPADES);
129 | Card foundCard = deck.getCards().get(indexOfSevenOfSpades);
130 | assertEquals(Rank.SEVEN, foundCard.getRank(), "The found card's rank should be SEVEN");
131 | assertEquals(Suit.SPADES, foundCard.getSuit(), "The found card's suit should be SPADES");
132 | }
133 |
134 |
135 | /**
136 | * Test the findCardsByRank() method to verify that it correctly returns a list of the indices
137 | * of the cards in the deck with the specified rank.
138 | */
139 | @Test
140 | public void testFindCardsByRank() {
141 | Deck deck = new Deck();
142 |
143 | List indices = deck.findCardsByRank(Rank.ACE);
144 | assertEquals(4, indices.size());
145 |
146 | indices = deck.findCardsByRank(Rank.FIVE);
147 | assertEquals(4, indices.size());
148 |
149 | indices = deck.findCardsByRank(Rank.KING);
150 | assertEquals(4, indices.size());
151 |
152 | indices = deck.findCardsByRank(Rank.JACK);
153 | assertEquals(4, indices.size());
154 |
155 | indices = deck.findCardsByRank(Rank.QUEEN);
156 | assertEquals(4, indices.size());
157 |
158 | indices = deck.findCardsByRank(Rank.TEN);
159 | assertEquals(4, indices.size());
160 |
161 | indices = deck.findCardsByRank(Rank.NINE);
162 | assertEquals(4, indices.size());
163 |
164 | indices = deck.findCardsByRank(Rank.EIGHT);
165 | assertEquals(4, indices.size());
166 |
167 | indices = deck.findCardsByRank(Rank.SEVEN);
168 | assertEquals(4, indices.size());
169 |
170 | indices = deck.findCardsByRank(Rank.SIX);
171 | assertEquals(4, indices.size());
172 |
173 | indices = deck.findCardsByRank(Rank.FOUR);
174 | assertEquals(4, indices.size());
175 |
176 | indices = deck.findCardsByRank(Rank.THREE);
177 | assertEquals(4, indices.size());
178 |
179 | indices = deck.findCardsByRank(Rank.TWO);
180 | assertEquals(4, indices.size());
181 | }
182 |
183 | /**
184 | * Test the findCardsBySuit() method to verify that it correctly returns a list of the indices
185 | * of the cards in the deck with the specified suit.
186 | */
187 | @Test
188 | public void testFindCardsBySuit() {
189 | Deck deck = new Deck();
190 |
191 | List indices = deck.findCardsBySuit(Suit.HEARTS);
192 | assertEquals(13, indices.size());
193 |
194 | indices = deck.findCardsBySuit(Suit.DIAMONDS);
195 | assertEquals(13, indices.size());
196 |
197 | indices = deck.findCardsBySuit(Suit.CLUBS);
198 | assertEquals(13, indices.size());
199 |
200 | indices = deck.findCardsBySuit(Suit.SPADES);
201 | assertEquals(13, indices.size());
202 | }
203 |
204 |
205 | /**
206 | * Tests the {@link Deck#deal(int)} method when the deck is empty.
207 | */
208 | @Test
209 | public void testDealFromEmptyDeck() {
210 | Deck deck = new Deck();
211 | deck.shuffle();
212 | deck.clear();
213 | IllegalStateException exception = assertThrows(IllegalStateException.class, () -> {
214 | deck.deal(1);
215 | });
216 | assertEquals("No more cards in the deck", exception.getMessage());
217 | }
218 |
219 | /**
220 | * Tests the {@link Deck#deal(int)} method when the amount of cards to be dealt is greater than the number of cards in the deck.
221 | */
222 | @Test
223 | public void testDealMoreCardsThanInDeck() {
224 | Deck deck = new Deck();
225 | deck.shuffle();
226 | IllegalStateException exception = assertThrows(IllegalStateException.class, () -> {
227 | deck.deal(53);
228 | });
229 | assertEquals("Cannot deal more cards than are in the deck", exception.getMessage());
230 | }
231 |
232 | /**
233 | * Tests the {@link Deck#deal(int)} method when the deck is not empty and the amount of cards to be dealt is less than or equal to the number of cards in the deck.
234 | */
235 | @Test
236 | public void testDealFromNonEmptyDeck() {
237 | Deck deck = new Deck();
238 | deck.shuffle();
239 | List dealtCards = deck.deal(5);
240 | assertEquals(5, dealtCards.size());
241 | assertEquals(47, deck.getCards().size());
242 | }
243 |
244 | @Test
245 | public void testClear() {
246 | Deck deck = new Deck();
247 | deck.clear();
248 | assertEquals(0, deck.getSize());
249 | }
250 |
251 | }
--------------------------------------------------------------------------------
/src/main/java/io/lyuda/jcards/Rank.java:
--------------------------------------------------------------------------------
1 | package io.lyuda.jcards;
2 |
3 | /**
4 | * The {@code Rank} enum represents the rank of a playing card.
5 | * Each rank is associated with a numerical value and a unicode symbol.
6 | *
7 | * @author lyudaio, Narkoleptika (Creator of the Paths)
8 | * @since 0.0.4
9 | */
10 | public enum Rank {
11 |
12 | /**
13 | * Represents the Ace card with a value of 1 and symbol of "\uD83C\uDCA1".
14 | */
15 | ACE(1, "\uD83C\uDCA1", "M22.864 24h-3.401l-2.353-6.69H6.73L4.377 24h-3.24L9.873 0h4.255zm-6.737-9.43L11.92 2.79 7.697 14.57z"),
16 |
17 | /**
18 | * Represents the Two card with a value of 2 and symbol of "\uD83C\uDCA2".
19 | */
20 | TWO(2, "\uD83C\uDCA2", "M19.958 24H4.042v-3.3l3.316-2.842q1.673-1.421 3.11-2.826 3.032-2.937 4.153-4.658 1.121-1.737 1.121-3.742 0-1.832-1.216-2.858-1.2-1.042-3.363-1.042-1.437 0-3.11.505-1.674.505-3.269 1.547h-.158V1.468Q5.747.916 7.61.458 9.49 0 11.242 0q3.616 0 5.668 1.753 2.053 1.736 2.053 4.72 0 1.343-.347 2.511-.332 1.153-.995 2.195-.616.979-1.453 1.926-.82.948-2.005 2.1-1.69 1.658-3.49 3.221-1.8 1.548-3.363 2.874h12.647z"),
21 |
22 | /**
23 | * Represents the Three card with a value of 3 and symbol of "\uD83C\uDCA3".
24 | */
25 | THREE(3, "\uD83C\uDCA3", "M17.888 12.426q.743.665 1.222 1.671.48 1.006.48 2.6 0 1.578-.572 2.893-.573 1.316-1.61 2.29-1.16 1.084-2.739 1.61-1.563.51-3.435.51-1.919 0-3.775-.464-1.857-.449-3.049-.99v-3.234h.232q1.316.866 3.095 1.439 1.78.573 3.435.573.975 0 2.074-.325t1.78-.96q.711-.68 1.051-1.5.356-.82.356-2.074 0-1.238-.402-2.043-.387-.82-1.083-1.284-.696-.48-1.687-.65-.99-.186-2.135-.186H9.733V9.733h1.083q2.352 0 3.745-.974 1.408-.99 1.408-2.878 0-.836-.356-1.455-.356-.634-.99-1.037-.665-.402-1.424-.557-.758-.155-1.717-.155-1.47 0-3.126.527Q6.7 3.73 5.23 4.689h-.154V1.455Q6.174.913 8 .465 9.842 0 11.56 0q1.686 0 2.97.31 1.284.31 2.321.99 1.114.743 1.687 1.795.573 1.053.573 2.46 0 1.92-1.362 3.358-1.346 1.424-3.188 1.795v.217q.743.124 1.702.526.96.387 1.625.975z"),
26 |
27 | /**
28 | * Represents the Four card with a value of 4 and symbol of "\uD83C\uDCA4".
29 | */
30 | FOUR(4, "\uD83C\uDCA4", "M21.075 17.246h-3.562v6.753h-3.095v-6.753H2.926v-3.707L14.547-.001h2.966v14.669h3.562zm-6.657-2.579V3.837l-9.3 10.83z"),
31 |
32 | /**
33 | * Represents the Five card with a value of 5 and symbol of "\uD83C\uDCA5".
34 | */
35 | FIVE(5, "\uD83C\uDCA5", "M19.658 16.042q0 1.642-.6 3.142t-1.642 2.526q-1.137 1.106-2.716 1.706Q13.137 24 11.07 24q-1.927 0-3.711-.41-1.784-.395-3.016-.964v-3.331h.221q1.295.82 3.032 1.405 1.737.568 3.41.568 1.121 0 2.164-.315 1.057-.316 1.879-1.106.694-.679 1.042-1.626.363-.947.363-2.195 0-1.216-.427-2.052-.41-.837-1.152-1.342-.821-.6-2.005-.837-1.169-.253-2.622-.253-1.389 0-2.684.19-1.279.189-2.21.379V-.002H19.5v2.764H8.4v6.252q.679-.063 1.39-.094.71-.032 1.23-.032 1.911 0 3.348.332 1.437.315 2.637 1.136 1.263.869 1.958 2.242.695 1.374.695 3.443z"),
36 |
37 | /**
38 | * Represents the Six card with a value of 6 and symbol of "\uD83C\uDCA6".
39 | */
40 | SIX(6, "\uD83C\uDCA6", "M20.171 16.031q0 3.52-2.326 5.752Q15.535 24 12.171 24q-1.705 0-3.1-.527-1.396-.527-2.466-1.566-1.333-1.287-2.062-3.41-.713-2.125-.713-5.117 0-3.07.651-5.442.667-2.372 2.109-4.217Q7.954 1.969 10.109.992 12.264 0 15.132 0q.915 0 1.535.077.62.078 1.256.28v2.96h-.155q-.434-.232-1.318-.434-.868-.217-1.783-.217-3.333 0-5.318 2.093-1.984 2.078-2.31 5.628 1.303-.79 2.558-1.193 1.272-.42 2.93-.42 1.474 0 2.59.28 1.132.264 2.31 1.085 1.364.946 2.046 2.388.698 1.442.698 3.504zm-3.147.124q0-1.442-.434-2.388-.419-.945-1.396-1.643-.713-.496-1.581-.651-.868-.155-1.814-.155-1.318 0-2.45.31-1.132.31-2.325.961-.031.341-.047.667-.015.31-.015.79 0 2.45.496 3.877.511 1.41 1.395 2.232.713.682 1.535 1.008.837.31 1.814.31 2.248 0 3.535-1.364 1.287-1.38 1.287-3.954z"),
41 |
42 | /**
43 | * Represents the Seven card with a value of 7 and symbol of "\uD83C\uDCA7".
44 | */
45 | SEVEN(7, "\uD83C\uDCA7", "M20.212 3.594 9.348 24h-3.45L17.457 2.821H3.788V.001h16.424z"),
46 |
47 | /**
48 | * Represents the Eight card with a value of 8 and symbol of "\uD83C\uDCA8".
49 | */
50 | EIGHT(8, "\uD83C\uDCA8", "M20.154 17.079q0 2.975-2.328 4.948Q15.514 24 12 24q-3.73 0-5.95-1.927-2.204-1.927-2.204-4.932 0-1.912 1.11-3.453 1.11-1.557 3.129-2.467v-.092q-1.85-.986-2.744-2.158-.879-1.171-.879-2.929 0-2.59 2.127-4.316Q8.717 0 12 0q3.438 0 5.488 1.65 2.05 1.649 2.05 4.192 0 1.557-.97 3.067-.972 1.496-2.852 2.343v.093q2.158.925 3.298 2.281 1.141 1.357 1.141 3.453zM16.501 5.873q0-1.65-1.28-2.62-1.264-.987-3.237-.987-1.942 0-3.19.925-1.234.925-1.234 2.497 0 1.11.617 1.927.632.801 1.896 1.433.57.278 1.634.725 1.079.447 2.096.74Q15.33 9.495 15.915 8.4q.586-1.094.586-2.528zm.478 11.499q0-1.418-.632-2.266-.617-.863-2.436-1.726-.724-.34-1.588-.632-.863-.293-2.296-.817-1.388.755-2.235 2.05-.833 1.295-.833 2.929 0 2.08 1.434 3.437t3.638 1.356q2.25 0 3.591-1.156 1.357-1.156 1.357-3.175z"),
51 |
52 | /**
53 | * Represents the Nine card with a value of 9 and symbol of "\uD83C\uDCA9".
54 | */
55 | NINE(9, "\uD83C\uDCA9", "M20.171 10.62q0 3.023-.698 5.488-.682 2.465-2.077 4.202-1.411 1.767-3.535 2.729-2.124.96-4.992.96-.806 0-1.52-.092-.713-.078-1.271-.264v-2.96h.155q.45.232 1.271.449.822.201 1.83.201 3.426 0 5.364-2.046 1.954-2.062 2.264-5.675-1.442.869-2.713 1.24-1.272.373-2.776.373-1.426 0-2.589-.28-1.147-.278-2.31-1.085-1.364-.945-2.062-2.403Q3.83 10 3.83 7.97q0-3.535 2.326-5.752Q8.48 0 11.83 0q1.674 0 3.1.527 1.427.512 2.497 1.55 1.318 1.287 2.03 3.318.714 2.016.714 5.225zm-3.132-.667q0-2.403-.496-3.86T15.18 3.829q-.729-.697-1.566-.992-.837-.31-1.814-.31-2.233 0-3.535 1.395-1.287 1.396-1.287 3.923 0 1.473.419 2.403.418.93 1.41 1.628.698.48 1.536.65.837.156 1.86.156 1.21 0 2.45-.326 1.24-.325 2.325-.945l.031-.636q.031-.326.031-.822z"),
56 |
57 | /**
58 | * Represents the Ten card with a value of 10 and symbol of "\uD83C\uDCAA".
59 | */
60 | TEN(10, "\uD83C\uDCAA", "M24 11.985q0 6.209-1.698 9.12Q20.618 24 17.063 24q-3.61 0-5.282-2.941-1.657-2.942-1.657-9.043 0-6.147 1.684-9.074Q13.492.001 17.062.001q3.61 0 5.268 2.988Q24 5.962 24 11.985zm-3.543 7.03q.471-1.255.633-2.942.175-1.703.175-4.088 0-2.353-.175-4.088-.162-1.734-.647-2.942-.471-1.192-1.293-1.796-.809-.603-2.088-.603-1.267 0-2.102.603-.822.604-1.307 1.827-.458 1.146-.633 2.989-.162 1.842-.162 4.041 0 2.416.148 4.041.149 1.626.634 2.911.444 1.208 1.253 1.843.821.635 2.169.635 1.266 0 2.101-.604.836-.604 1.294-1.827zm-9.598 4.505H-.001v-2.353h4.177V5.714H0V3.608q.849 0 1.819-.155.97-.17 1.468-.48.62-.387.97-.975.364-.604.418-1.61h2.088v20.779h4.096z"),
61 |
62 | /**
63 | * Represents the Jack card with a value of 11 and symbol of "\uD83C\uDCAB".
64 | */
65 | JACK(10, "\uD83C\uDCAB", "M17.579 17.515q0 3.115-1.907 4.8Q13.78 24 10.586 24q-.763 0-2.035-.143-1.271-.127-2.13-.318v-2.94h.175q.652.222 1.606.46.953.239 1.955.239 1.462 0 2.32-.334.874-.334 1.287-.954.43-.635.54-1.557.128-.922.128-2.13V2.511H9.425V0h8.154z"),
66 |
67 | /**
68 | * Represents the Queen card with a value of 12 and symbol of "\uD83C\uDCAD".
69 | */
70 | QUEEN(10, "\uD83C\uDCAD", "M20.864 23.649q-.753.188-1.493.263-.728.088-1.493.088-2.183 0-3.513-1.204-1.317-1.192-1.43-3.425-.301.05-.59.063-.276.025-.539.025-1.97 0-3.588-.653-1.606-.652-2.735-1.894-1.13-1.242-1.744-3.049-.602-1.806-.602-4.127 0-2.283.602-4.09.602-1.82 1.756-3.111Q6.6 1.305 8.23.653 9.874 0 11.806 0q2.007 0 3.6.665 1.606.653 2.723 1.87 1.142 1.254 1.744 3.073.614 1.82.614 4.128 0 3.425-1.405 5.77-1.392 2.347-3.75 3.288.05 1.43.677 2.22.627.79 2.283.79.514 0 1.217-.162.715-.15 1.016-.276h.339zm-2.96-13.913q0-3.638-1.632-5.608-1.63-1.982-4.453-1.982-2.848 0-4.48 1.982-1.618 1.97-1.618 5.608 0 3.676 1.656 5.633 1.656 1.945 4.442 1.945t4.428-1.945q1.656-1.957 1.656-5.633z"),
71 |
72 | /**
73 | * Represents the King card with a value of 13 and symbol of "\uD83C\uDCAE".
74 | */
75 | KING(10, "\uD83C\uDCAE", "M21.606 24h-4.142L7.97 13.314l-2.386 2.547V24h-3.19V0h3.19v12.523L17.237 0h3.869l-10.72 11.283z");
76 |
77 | /**
78 | * The numerical value of the rank.
79 | */
80 | private final int value;
81 |
82 | /**
83 | * The unicode symbol representing the rank.
84 | */
85 | private final String symbol;
86 |
87 | /**
88 | * The path for the rank.
89 | */
90 | private final String path;
91 |
92 | /**
93 | * Constructs a {@code Rank} with a given value and symbol.
94 | *
95 | * @param value the numerical value of the rank.
96 | * @param symbol the unicode symbol of the rank.
97 | */
98 | Rank(int value, String symbol, String path) {
99 | this.value = value;
100 | this.symbol = symbol;
101 | this.path = path;
102 | }
103 |
104 | /**
105 | * Returns the numerical value of the rank.
106 | *
107 | * @return the numerical value of the rank.
108 | */
109 | public int getValue() {
110 | return value;
111 | }
112 |
113 | /**
114 | * Returns the unicode symbol of the rank.
115 | *
116 | * @return the unicode symbol of the rank.
117 | */
118 | public String getSymbol() {
119 | return symbol;
120 | }
121 |
122 | /**
123 | * Returns the path of the rank.
124 | *
125 | * @return the path of the rank.
126 | */
127 | public String getPath() {
128 | return path;
129 | }
130 |
131 | }
132 |
133 |
--------------------------------------------------------------------------------
/docs/io/lyuda/jcards/class-use/Rank.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Uses of Enum Class io.lyuda.jcards.Rank (jcards 0.0.6 API)
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
21 |
24 |