Posted By

mille9393 on 09/26/18


Tagged


Versions (?)

asdasdasdsad


 / Published in: Java
 

asdasd

  1. public class Card {
  2.  
  3. private Face face; //Face of card, i.e "King" & "Queen"
  4. private Suit suit; //Suit of card, i.e "Hearts" & "diamonds"
  5.  
  6. public Card (Face cardFace, Suit cardSuit){ //Constructor which initializes card's face and suit
  7. this.face = cardFace;
  8. this.suit = cardSuit;
  9. }
  10.  
  11. public int getRank(){
  12. return getRank();
  13. }
  14.  
  15. public String getSuit(){
  16. return getSuit();
  17. }
  18.  
  19. public String toString(){ //return String representation of Card
  20. return face.getFaceValue() + " of " + suit.getSuitText();
  21. }
  22. }
  23.  
  24.  
  25. import java.util.ArrayList;
  26. public class Player {
  27.  
  28.  
  29. private String nickName;
  30. private int playerNumOfCards;
  31. ArrayList<Card> playerHand;
  32.  
  33.  
  34. public Player (String name){
  35. this.nickName = name;
  36. playerHand = new ArrayList<Card>(); //creates an empty playerHand
  37.  
  38. }
  39.  
  40. public String getNickName() {
  41. return nickName;
  42. }
  43.  
  44. public void addCard(Card aCard){
  45. playerHand.add(aCard);
  46. this.playerNumOfCards++;
  47.  
  48. }
  49.  
  50. public void getHandSum(){
  51.  
  52. }
  53.  
  54. public void getPlayerHand(){
  55. for(Card cards: playerHand){
  56. System.out.println(cards.toString());
  57. }
  58. }
  59.  
  60.  
  61. }
  62.  
  63. import java.lang.management.PlatformLoggingMXBean;
  64.  
  65. public class BlackJackGame {
  66.  
  67. public static void main(String[] args) {
  68.  
  69. DeckOfCards deck1 = new DeckOfCards();
  70. Player player1 = new Player("mille");
  71. deck1.shuffleDeck();
  72.  
  73. }
  74. }
  75.  
  76.  
  77. import java.util.ArrayList;
  78. import java.util.Random;
  79. import java.util.Collections;
  80.  
  81.  
  82. public class DeckOfCards {
  83.  
  84. private Card[] deck;
  85. private static final Random random = new Random();
  86.  
  87. private int currentCard; //index of next Card to be deal (0-51)
  88. private static int NUMBER_OF_CARDS = 52; //Constant number of cards
  89.  
  90. public DeckOfCards(){
  91.  
  92. String [] faces = {"Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack","Queen", "King"};
  93. String [] suits = {"Hearts", "Diamonds", "Clubs", "Spades"};
  94.  
  95. deck = new Card [NUMBER_OF_CARDS]; // create array with Cards (52)
  96. currentCard = 0;
  97.  
  98. //Populate deck with Cards
  99. for(int count = 0; count < deck.length; count++)
  100. deck [count] = new Card(faces [count % 13], suits [count / 13]);
  101. }
  102.  
  103. public void shuffleDeck(){
  104. currentCard = 0;
  105.  
  106. for (int first = 0; first < deck.length; first++){
  107.  
  108. int second = random.nextInt(NUMBER_OF_CARDS); //Select a random card from number 0-51 (Number_of_cards)
  109.  
  110. //Loops through all the cards and swaps it with the "Second" card which is randomly chosen card from hte same list.
  111. Card temp = deck[first];
  112. deck [first] = deck [second];
  113. deck [second] = temp;
  114. }
  115. }
  116.  
  117. public void getCardDeck(){
  118. int start = 1;
  119. for(Card k : deck) {
  120. System.out.println("" + start + "/52 " + k);
  121. start++;
  122. }
  123. }
  124.  
  125. public Card dealNextCard(){
  126.  
  127. //Get the top card
  128. Card topCard = this.deck[0];
  129.  
  130. //shift all the subsequent cards to the left by one index
  131. for(int currentCard = 1; currentCard < NUMBER_OF_CARDS; currentCard ++){
  132. this.deck[currentCard-1] = this.deck[currentCard];
  133. }
  134. this.deck[NUMBER_OF_CARDS-1] = null;
  135.  
  136. //decrement the number of cards in our deck
  137. this.NUMBER_OF_CARDS--;
  138.  
  139. return topCard;
  140. }
  141.  
  142. }
  143.  
  144.  
  145.  
  146. public enum Suit {
  147.  
  148. HEARTS("hearts"),
  149. SPADES("Spades"),
  150. DIAMONDS("Diamonds"),
  151. CLUBS("clubs");
  152.  
  153. private final String suitText;
  154.  
  155. private Suit(String suitText){
  156. this.suitText = suitText;
  157. }
  158.  
  159. public String getSuitText(){
  160. return suitText;
  161. }
  162.  
  163. }
  164.  
  165.  
  166. public enum Face {
  167.  
  168. ACE(1), DEUCE (2), THREE (3),
  169. FOUR(4), FIVE(5), SIX(6),
  170. SEVEN(7), EIGHT(8), NINE(9),
  171. TEN(10), JACK(10), QUEEN(10),
  172. KING(10);
  173.  
  174.  
  175. private final int faceValue;
  176.  
  177. private Face(int faceValue){
  178. this.faceValue = faceValue;
  179. }
  180.  
  181. public int getFaceValue(){
  182. return faceValue;
  183. }
  184.  
  185.  
  186. }

Report this snippet  

You need to login to post a comment.