Posted By

mille9393 on 09/20/18


Tagged

java


Versions (?)

asd


 / Published in: Java
 

asd

  1. import java.util.ArrayList;
  2. import java.util.Random;
  3. import java.util.Collections;
  4.  
  5.  
  6. public class DeckOfCards {
  7.  
  8. private Card[] deck;
  9. private static final Random random = new Random();
  10.  
  11. private int currentCard; //index of next Card to be deal (0-51)
  12. private static final int NUMBER_OF_CARDS = 52; //Constant number of cards
  13.  
  14. public DeckOfCards(){
  15.  
  16. String [] faces = {"Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack","Queen", "King"};
  17. String [] suits = {"Hearts", "Diamonds", "Clubs", "Spades"};
  18.  
  19. deck = new Card [NUMBER_OF_CARDS]; // create array with Cards (52)
  20. currentCard = 0;
  21.  
  22. //Populate deck with Cards
  23. for(int count = 0; count < deck.length; count++)
  24. deck [count] =
  25. new Card(faces [count % 13], suits [count / 13]);
  26. }
  27.  
  28. public void shuffleDeck(){
  29. currentCard = 0;
  30.  
  31. for (int first = 0; first < deck.length; first++){
  32.  
  33. int second = random.nextInt(NUMBER_OF_CARDS); //Select a random card from number 0-51 (Number_of_cards)
  34.  
  35. //Loops through all the cards and swaps it with the "Second" card which is randomly chosen card from hte same list.
  36. Card temp = deck[first];
  37. deck [first] = deck [second];
  38. deck [second] = temp;
  39. }
  40. }
  41.  
  42. public void getKortlek(){
  43. for(Card k : deck){
  44. System.out.println(k);
  45. }
  46. }
  47.  
  48. public Card dealCard(){
  49. if (currentCard < deck.length){
  50.  
  51. return deck[currentCard++];
  52. } else {
  53. return null;
  54. }
  55. }
  56.  
  57. }
  58.  
  59.  
  60. public class BlackJackGame {
  61.  
  62. public static void main(String[] args) {
  63.  
  64. DeckOfCards deck1 = new DeckOfCards();
  65. deck1.shuffleDeck();
  66.  
  67. for( int i = 1; i <= 52; i++){
  68. System.out.printf( "%-19s", deck1.dealCard() );
  69.  
  70. if( i % 4 == 0);
  71. System.out.println();
  72.  
  73. }
  74. }
  75. }
  76.  
  77.  
  78. public class Card {
  79.  
  80. private String face; //Face of card, i.e "King" & "Queen"
  81. private String suit; //Suit of card, i.e "Hearts" & "diamonds"
  82.  
  83. public Card (String cardFace, String cardSuit){ //Constructor which initializes card's face and suit
  84. this.face = cardFace;
  85. this.suit = cardSuit;
  86. }
  87.  
  88. public String toString(){ //return String representation of Card
  89. return face + " of " + suit;
  90. }
  91.  
  92. }

Report this snippet  

You need to login to post a comment.