Git Product home page Git Product logo

carddeck's Introduction

CardDeck

CardDeck is an application that uses a the modified Fisher-Yates algorithm to shuffle a deck of cards stored in an integer array, and then it displays the deck of 52 playing cards on the screen.

Shuffling Algorithm

At first glance, you'd think using Random or SecureRandom classes would work well to randomly shuffle an array. In reality, they don't give you the good "randomness" you expect when shuffling cards. These classes actually generate a stream of pseudorandom numbers, whoich are not truly random. Frequently, you'd end up over-shuffling the same positions or missing some entirely. Fisher-Yates solves this problem by making sure all the cards participate in the shuffling.

I've developed a reusable Randomizer class based on the the Fisher-Yates algorithm with several methods that can be used if you need to perform a good shuffle. This code uses the the modified Fisher-Yates algorithm to shuffle the deck of cards, and then it displays the deck of 52 playing cards.

The code is written in Java and the application was developed using IntelliJ Idea Community Edition.

Randomizer Class

A class that creates a random sized array of integers of size passed. This is a variant of the Fisher-Yates shuffle for excellent randomizing. It avoids the negative effects of over-shuffling.

To Instantiate

Randomizer n = new Randomizer(sizePassed);

sizePassed = Length of array to randomize (integer)

Integer range starts at 0 and ends at sizePassed-1

For example, if {sizePassed} is 10, the integer range is from 0-9.

Methods

int nextInt() - Return the integer value in the current position from the ArrayList and increment the position; when the end of the array is reached, it automatically reshuffles the array and returns the first element of the reshuffled array.

Output

Card Deck In Order
Ace of Clubs
2 of Clubs
3 of Clubs
4 of Clubs
5 of Clubs
{...}
9 of Diamonds
10 of Diamonds
Jack of Diamonds
Queen of Diamonds
King of Diamonds

Random Card Deck Order
7 of Hearts
5 of Clubs
2 of Clubs
8 of Hearts
5 of Diamonds
King of Diamonds
{...}
2 of Diamonds
Ace of Hearts
8 of Clubs
9 of Hearts
7 of Spades
4 of Diamonds
Ace of Spades

Process finished with exit code 0

For More Information

Here is an excellent YouTube video demo by Adam Khoury showing the algorithm in action: Fisher-Yates Shuffle Modern Algorithm JavaScript Programming Tutorial

This is the Wikipedia explanation: Fisher-Yates Shuffle

Happy Coding!

Randy Riegsecker

carddeck's People

Contributors

randy-riegsecker avatar

Watchers

 avatar

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.