daily pastebin goal
70%
SHARE
TWEET

Deck Of Cards Code Using Methods

a guest Nov 17th, 2016 120 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. /*
  2.  * Deck Of Cards Code Using Methods
  3.  * A.C. Silvestri
  4.  * 11/17/2016
  5.  */
  6.  
  7. package chapter7;
  8.  
  9. public class DeckOfCards {
  10.     public static void main(String[] args) {
  11.         int deck[] = initDeck();
  12.         shuffleTheDeck(deck);
  13.         int yourHand[] = dealAHand(deck, 5);
  14.         displayYourHand(yourHand);
  15.     }
  16.  
  17.     private static void displayYourHand(int[] yourHand) {
  18.         String[] suits = { "Spades", "Hearts", "Clubs", "Diamonds" };
  19.         String[] ranks = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10",
  20.                 "Jack", "Queen", "King" };
  21.  
  22.         // Display the first four cards
  23.         for (int i = 0; i < yourHand.length; i++) {
  24.             String suit = suits[yourHand[i] / 13];
  25.             String rank = ranks[yourHand[i] % 13];
  26.             System.out.println("Card number " + yourHand[i] + ": " + rank + " of " + suit);
  27.         }
  28.     }
  29.  
  30.     private static int[] dealAHand(int[] deck, int numberOfCards) {
  31.         int hand[] = new int[numberOfCards];
  32.         for (int i = 0; i < hand.length; i++)
  33.             hand[i] = deck[i];
  34.         return hand;
  35.     }
  36.  
  37.     private static void shuffleTheDeck(int[] deck) {
  38.         for (int i = 0; i < deck.length; i++) {
  39.             // Generate an index randomly
  40.             int index = (int) (Math.random() * deck.length);
  41.             int temp = deck[i];
  42.             deck[i] = deck[index];
  43.             deck[index] = temp;
  44.         }
  45.     }
  46.  
  47.     private static int[] initDeck() {
  48.         int deck[] = new int[52];
  49.         for (int i = 0; i < deck.length; i++)
  50.             deck[i] = i;
  51.         return deck;
  52.     }
  53.  
  54. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top