Giter VIP home page Giter VIP logo

Comments (3)

lyudaio avatar lyudaio commented on May 18, 2024

Hey thanks for pointing this out. You're absolutely right the test would fail if both decks are randomly shuffled into the same order. To account for this, I have adjusted the testShuffle method to account for this. Let me know if there is something you'd improve upon it and I'll include it in the 0.0.3 release.

@Test
public void testShuffle() {
    Deck deck = new Deck();
    List<Card> originalCards = new ArrayList<>(deck.cards);
    deck.shuffle();
    int matchCount = 0;
    for (int i = 0; i < originalCards.size(); i++) {
        if (originalCards.get(i).equals(deck.cards.get(i))) {
            matchCount++;
        }
    }
    assertFalse("The deck was randomly shuffled into the same order", matchCount == originalCards.size());
}

For the testFindCard method, you're right we shouldn't be checking the index, rather making sure the card was found.

Here is a refactored method we could include in the 0.0.3 release.

    /**
     * Test method to check the `findCard` method of the `Deck` class. This test verifies that the `findCard` method is
     * able to locate a card with rank SEVEN and suit SPADES in the shuffled deck.
     */
    @Test
    public void testFindCard() {
        Deck deck = new Deck();
        deck.shuffle();
        int indexOfSevenOfSpades = deck.findCard(Card.Rank.SEVEN, Card.Suit.SPADES);
        Card foundCard = deck.getCards().get(indexOfSevenOfSpades);
        assertEquals(Card.Rank.SEVEN, foundCard.getRank(), "The found card's rank should be SEVEN");
        assertEquals(Card.Suit.SPADES, foundCard.getSuit(), "The found card's suit should be SPADES");
    }

Further, the shuffle() method itself will probably be refactored to use a different seed every time.

    /**
    * Shuffles the deck of cards using a cryptographically secure random number generator.
    *
    * @see java.util.Collections#shuffle(List, Random)
    * @see java.security.SecureRandom
    */
   public void shuffle() {
       SecureRandom secureRandom = new SecureRandom();
       long seed = secureRandom.nextLong();
       Collections.shuffle(cards, new Random(seed));
   }

Thanks again for taking the time out of your day to respond!

from jcards.

lyudaio avatar lyudaio commented on May 18, 2024

One thing missing from the Deck class as well would be a better way to get a list of all the cards, this will also be added in the 0.0.3 release.

    /**
     * Retrieves the most up-to-date list of cards stored in the object.
     *
     * @return A list of {@link Card} objects stored in the object, which reflects any changes made to the underlying list.
     *
     * <p> The returned list may be empty, but it will never be
     * {@code null}.
     *
     * @see Card
     * @author lyudaio
     * @since 0.0.3
     */
    public List<Card> getCards() {
        return cards;
    }

from jcards.

lyudaio avatar lyudaio commented on May 18, 2024

These issues have been addressed in #3 - Thanks again for your help!

from jcards.

Related Issues (7)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.