Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Deck Of Cards Code Using Methods
- * A.C. Silvestri
- * 11/17/2016
- */
- package chapter7;
- public class DeckOfCards {
- public static void main(String[] args) {
- int deck[] = initDeck();
- shuffleTheDeck(deck);
- int yourHand[] = dealAHand(deck, 5);
- displayYourHand(yourHand);
- }
- private static void displayYourHand(int[] yourHand) {
- String[] suits = { "Spades", "Hearts", "Clubs", "Diamonds" };
- String[] ranks = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10",
- "Jack", "Queen", "King" };
- // Display the first four cards
- for (int i = 0; i < yourHand.length; i++) {
- String suit = suits[yourHand[i] / 13];
- String rank = ranks[yourHand[i] % 13];
- System.out.println("Card number " + yourHand[i] + ": " + rank + " of " + suit);
- }
- }
- private static int[] dealAHand(int[] deck, int numberOfCards) {
- int hand[] = new int[numberOfCards];
- for (int i = 0; i < hand.length; i++)
- hand[i] = deck[i];
- return hand;
- }
- private static void shuffleTheDeck(int[] deck) {
- for (int i = 0; i < deck.length; i++) {
- // Generate an index randomly
- int index = (int) (Math.random() * deck.length);
- int temp = deck[i];
- deck[i] = deck[index];
- deck[index] = temp;
- }
- }
- private static int[] initDeck() {
- int deck[] = new int[52];
- for (int i = 0; i < deck.length; i++)
- deck[i] = i;
- return deck;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement