Posted By

mille9393 on 10/06/18


Tagged

as


Versions (?)

asdadadad


 / Published in: Java
 

asd

  1. import java.lang.management.PlatformLoggingMXBean;
  2. import java.util.Scanner;
  3. public class BlackJackGame {
  4.  
  5. public static void main(String[] args) {
  6.  
  7. Scanner scanner = new Scanner(System.in);
  8. boolean stay = false;
  9.  
  10. System.out.println("What nickName would you like to have?");
  11. String pAnswer = scanner.nextLine();
  12. Player me = new Player(pAnswer);
  13. Player dealer = new Player("Dealer");
  14.  
  15.  
  16. System.out.println("Would you like to start a new game? 'Yes/No' :");
  17. pAnswer = scanner.nextLine();
  18.  
  19. if (pAnswer.equalsIgnoreCase("Yes")) {
  20.  
  21. DeckOfCards deck1 = new DeckOfCards();
  22. Card card1 = new Card(Face.ACE, Suit.CLUBS);
  23. deck1.shuffleDeck();
  24.  
  25. int totalPlayerSum = me.getHandSum();
  26. int totalDealerSum = dealer.getHandSum();
  27.  
  28. me.addCard(deck1.dealNextCard());
  29. me.addCard(deck1.dealNextCard());
  30. me.getPlayerHand(true);
  31. System.out.println(" ");
  32. dealer.addCard(deck1.dealNextCard());
  33. dealer.addCard(deck1.dealNextCard());
  34. dealer.getPlayerHand(false);
  35.  
  36. //PLAYER
  37. do {
  38. System.out.println("Would " + me.getNickName() + " like to bust or stay? 'Bust/Stay'");
  39. pAnswer = scanner.nextLine();
  40. //BUST
  41. if (pAnswer.equalsIgnoreCase("Bust")) {
  42. me.addCard(deck1.dealNextCard());
  43. if (me.getHandSum() > 21) {
  44. System.out.println("You busted and got a total of " + me.getHandSum() + ". Dealer wins this time! ");
  45. System.exit(0);
  46. }
  47. }
  48. //STAY
  49. if (pAnswer.equalsIgnoreCase("stay")) {
  50. System.out.println("You have chosen to stay. Your hand: " + me.getHandSum());
  51. }
  52.  
  53. } while (pAnswer.equalsIgnoreCase("Bust"));
  54.  
  55.  
  56. //DEALER
  57. do {
  58.  
  59. System.out.println("");
  60. System.out.println("Dealers turn!");
  61. //DRAW CARD
  62. if (totalDealerSum < 16) {
  63. dealer.addCard(deck1.dealNextCard());
  64. if (dealer.getHandSum() > 21) {
  65. System.out.println("Dealer busted and got a total of " + dealer.getHandSum() + ". " + me.getNickName() + " wins this time!");
  66. System.exit(0);
  67. }
  68. } else {
  69. System.out.println("Dealer has chosen to stay " + dealer.getHandSum());
  70. stay = false;
  71. }
  72.  
  73. } while (stay);
  74.  
  75. }
  76. }
  77. }
  78.  
  79.  
  80. //http://www.mathcs.emory.edu/~cheung/Courses/170/Syllabus/10/deck-of-cards.html
  81.  
  82.  
  83.  
  84. import java.util.ArrayList;
  85. public class Player {
  86.  
  87.  
  88. private String nickName;
  89. private int playerNumOfCards;
  90. ArrayList<Card> playerHand;
  91.  
  92. public Player (String name){
  93. this.nickName = name;
  94. playerHand = new ArrayList<Card>(); //creates an empty playerHand
  95.  
  96. }
  97.  
  98. public String getNickName() {
  99. return nickName;
  100. }
  101.  
  102. public void addCard(Card aCard){
  103. playerHand.add(aCard);
  104. //System.out.println("The drawn card is: " + aCard);
  105. this.playerNumOfCards++;
  106. }
  107.  
  108. public int getHandSum(){
  109.  
  110. int totalSum = 0;
  111. for(Card countSum: playerHand){
  112. totalSum = totalSum + countSum.getFace();
  113. }
  114. //System.out.print(this.nickName);
  115. //System.out.println(", total sum of your hand is: " + totalSum);
  116. return totalSum;
  117. }
  118.  
  119. public void getPlayerHand(boolean hideCard) {
  120. System.out.println(this.nickName + "s current hand.");
  121. for ( int c = 0; c < playerNumOfCards; c++){
  122. if(c == 0 && !hideCard){
  123. System.out.println("[Hidden card]");
  124. } else {
  125. System.out.println(playerHand.get(c).toString());
  126. }
  127. }
  128. }
  129.  
  130.  
  131. }
  132.  
  133.  
  134.  
  135. public class Card {
  136.  
  137. private Face face; //Face of card, i.e "King" & "Queen"
  138. private Suit suit; //Suit of card, i.e "Hearts" & "diamonds"
  139. int total = 0;
  140.  
  141. public Card (Face cardFace, Suit cardSuit){ //Constructor which initializes card's face and suit
  142. this.face = cardFace;
  143. this.suit = cardSuit;
  144. }
  145.  
  146. public int getFace(){
  147. return face.getFaceValue();
  148. }
  149.  
  150. public String getSuit(){
  151. return suit.PrintSuitText();
  152. }
  153.  
  154. public String toString(){ //return String representation of Card
  155. return face + " of " + suit;
  156. }
  157. }
  158.  
  159.  
  160. import javax.naming.NoInitialContextException;
  161. import java.awt.font.FontRenderContext;
  162. import java.util.ArrayList;
  163. import java.util.Random;
  164. import java.util.Collections;
  165.  
  166.  
  167. public class DeckOfCards {
  168.  
  169. private Card[] deck;
  170. private static final Random random = new Random();
  171.  
  172. private int currentCard; //index of next Card to be deal (0-51)
  173. private static int NUMBER_OF_CARDS = 52; //Constant number of cards
  174.  
  175. public DeckOfCards(){
  176.  
  177. Face [] faces = {Face.ACE, Face.DEUCE, Face.THREE, Face.FOUR, Face.FIVE, Face.SIX,
  178. Face.SEVEN, Face.EIGHT, Face.NINE, Face.TEN, Face.JACK, Face.QUEEN,
  179. Face.KING};
  180. Suit[] suits = {Suit.HEARTS, Suit.SPADES, Suit.DIAMONDS, Suit.CLUBS};
  181.  
  182. deck = new Card [NUMBER_OF_CARDS]; // create array with Cards (52)
  183. currentCard = 0;
  184.  
  185. //Populate deck with Cards
  186. for(int count = 0; count < deck.length; count++)
  187. deck [count] = new Card(faces [count % 13], suits [count / 13]);
  188. }
  189.  
  190. public void shuffleDeck(){
  191. currentCard = 0;
  192.  
  193. for (int first = 0; first < deck.length; first++){
  194.  
  195. int second = random.nextInt(NUMBER_OF_CARDS); //Select a random card from number 0-51 (Number_of_cards)
  196.  
  197. //Loops through all the cards and swaps it with the "Second" card which is randomly chosen card from hte same list.
  198. Card temp = deck[first];
  199. deck [first] = deck [second];
  200. deck [second] = temp;
  201. }
  202. }
  203.  
  204. public void getCardDeck(){
  205. int start = 1;
  206. for(Card k : deck) {
  207. System.out.println("" + start + "/52 " + k);
  208. start++;
  209. }
  210. }
  211.  
  212. public Card dealNextCard(){
  213.  
  214. //Get the top card
  215. Card topCard = this.deck[0];
  216.  
  217. //shift all the subsequent cards to the left by one index
  218. for(int currentCard = 1; currentCard < NUMBER_OF_CARDS; currentCard ++){
  219. this.deck[currentCard-1] = this.deck[currentCard];
  220. }
  221. this.deck[NUMBER_OF_CARDS-1] = null;
  222.  
  223. //decrement the number of cards in our deck
  224. this.NUMBER_OF_CARDS--;
  225.  
  226. return topCard;
  227. }
  228.  
  229. }
  230.  
  231.  
  232. public enum Suit {
  233.  
  234. HEARTS(" Hearts"),
  235. SPADES(" Spades"),
  236. DIAMONDS(" Diamonds"),
  237. CLUBS(" Clubs");
  238.  
  239. private final String suitText;
  240.  
  241. private Suit(String suitText){
  242. this.suitText = suitText;
  243. }
  244.  
  245. public String PrintSuitText(){
  246. return suitText;
  247. }
  248.  
  249. }
  250.  
  251.  
  252.  
  253. public enum Face {
  254.  
  255. ACE(1), DEUCE (2), THREE (3),
  256. FOUR(4), FIVE(5), SIX(6),
  257. SEVEN(7), EIGHT(8), NINE(9),
  258. TEN(10), JACK(10), QUEEN(10),
  259. KING(10);
  260.  
  261.  
  262. private final int faceValue;
  263.  
  264. private Face(int faceValue){
  265. this.faceValue = faceValue;
  266. }
  267.  
  268. public int getFaceValue(){
  269. return faceValue;
  270. }
  271.  
  272. }

Report this snippet  

You need to login to post a comment.