Advertisement
Guest User

DeckOfCards

a guest
Feb 21st, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.41 KB | None | 0 0
  1. package com.card;
  2. import java.util.Random;
  3.  
  4.  
  5. public class DeckOfCards {
  6.     int FACE[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
  7.     String SUIT[] = { "Diamonds", "Clubs", "Hearts", "Spades" };
  8.     Card deck[] = new Card[52];
  9.     Random randomGenerator = new Random();
  10.  
  11.     void createDeck() {
  12.         int count = 0;
  13.         int count2 = 0;
  14.         int temp = 0;
  15.         for (int i = 0; i < 4; i++) {
  16.             for (int j = 0+temp; j < 13+temp; j++) {
  17.                 deck[j] = new Card(FACE[count2], SUIT[count]);
  18.                 count2++;
  19.             }
  20.             count2=0;
  21.             count++;
  22.             temp += 13;
  23.         }
  24.     }
  25.     void shuffleDeck() {
  26.         for(int i = 0; i < 52; i++) {
  27.             int rand = randomGenerator.nextInt(51);
  28.             Card temp = new Card();
  29.             temp = deck[rand];
  30.             deck[rand] = deck[i];
  31.             deck[i] = temp;
  32.         }
  33.     }
  34.     Card dealCard() {
  35.         Card deal = new Card();
  36.         for(int i = 0; i < 52; i++) {
  37.             if(deck[i] != null) {
  38.                 deal = deck[i];
  39.                 deck[i] = null;
  40.                 return deal;
  41.             }
  42.             if(deck[51] == null) {
  43.                 return null;
  44.             }
  45.         }
  46.         deck[0] = null;
  47.         return deal;
  48.     }
  49.    
  50.     int cardsLeft() {
  51.         int count=0;
  52.         for(int i = 0; i < deck.length ; i++) {
  53.             if(deck[i] != null) {
  54.                 count++;
  55.             }
  56.         }
  57.         return count;
  58.     }
  59.    
  60. }
  61.  
  62. class Card {
  63.     int face;
  64.     String suit;
  65.  
  66.     public Card(int f, String s) {
  67.         face = f;
  68.         suit = s;
  69.     }
  70.    
  71.     public Card() {
  72.         face = 0;
  73.         suit = null;
  74.     }
  75.     public int getFace() {
  76.         return face;
  77.     }
  78.     public String getSuit() {
  79.         return suit;
  80.     }
  81.  
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement