Advertisement
MnMWizard

Black Jack Project

Dec 6th, 2017
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.98 KB | None | 0 0
  1. import static java.lang.System.*;
  2. import java.util.*;
  3. import javax.swing.*;
  4.  
  5. /**
  6. *
  7. * @author jmellen and Mason Marnell
  8. */
  9. public class BlackJack {
  10.  
  11. static int deck[];
  12. static int deckI;
  13. static ArrayList<Integer> player;
  14. static ArrayList<Integer> dealer;
  15.  
  16. static int bet;
  17. static int money = 500;
  18.  
  19. /**
  20. * Builds a deck of cards this will be done day 1
  21. */
  22. public static void build() {
  23. // make a new deck of 52 cards
  24. // set every spot in the array to a card value.
  25. deck = new int[52];
  26. int i = 0;
  27. int p = 0;
  28. for(;p<4;p++){
  29. for(;i<52;){
  30. deck[i] = 2;
  31. i++;
  32. deck[i] = 3;
  33. i++;
  34. deck[i] = 4;
  35. i++;
  36. deck[i] = 5;
  37. i++;
  38. deck[i] = 6;
  39. i++;
  40. deck[i] = 7;
  41. i++;
  42. deck[i] = 8;
  43. i++;
  44. deck[i] = 9;
  45. i++;
  46. deck[i] = 10;
  47. i++;
  48. deck[i] = 10;
  49. i++;
  50. deck[i] = 10;
  51. i++;
  52. deck[i] = 10;
  53. i++;
  54. deck[i] = 11;
  55. i++;
  56. }
  57. }
  58. }
  59.  
  60. /**
  61. * This method is done for you, and is only used to see the deck of cards,
  62. * it will not be used for the final Black Jack Program
  63. */
  64. public static void displayDeck() {
  65. for (int i = 0; i < deck.length; i++) {
  66. System.out.println(i + " " + deck[i]);
  67. }
  68. }
  69.  
  70. /**
  71. * DAY 2
  72. * Deal the cards, this method is for day 2. You will deal 3 cards
  73. * 2 to the player and 1 to the Dealer, you will then display all the cards
  74. */
  75. public static void deal() {
  76. player = new ArrayList();
  77. // create a new ArrayList() for the dealer
  78. dealer = new ArrayList();
  79.  
  80. deckI = 0; // this sets the location of the deck to the first card in the deck
  81.  
  82. int aCard = deck[deckI]; // grabs the next card from the deck
  83. deckI++; // moves the pointer to the next card in the deck
  84. player.add(aCard); // deals 1 card to the player
  85. aCard = deck[deckI];
  86. deckI++;
  87. dealer.add(aCard);// deal 1 card to the dealer
  88. aCard = deck[deckI];
  89. deckI++;
  90. player.add(aCard);// deal 1 more card to the player
  91. aCard = deck[deckI];
  92. CompleteDisplay(); // display the cards
  93. }
  94.  
  95. /**
  96. * DAY 2
  97. * This method will return a string of all the cards in a hand of cards
  98. * @param cards an ArrayList of cards
  99. * @return all of the cards in your hand
  100. */
  101. public static String display(ArrayList<Integer> cards) {
  102. String YourCards = ""; // set YourCards to an initial value of nothing
  103. for (int o = 0; o < cards.size();o++) {
  104. YourCards = YourCards + cards.get(o) + " ";
  105. }
  106. // go through all of your cards and add the value to YourCards
  107.  
  108. return "Total value of cards is " + sumOfCards(cards) + "\n The cards are " + YourCards;
  109. }
  110. /**
  111. * COMPLETED METHOD USED DAY 2
  112. * This will display all of the cards in the dealer and players hand
  113. */
  114. public static void CompleteDisplay() {
  115. String dealerCards = display(dealer);
  116. String playerCards = display(player);
  117. JOptionPane.showMessageDialog(null, "HERE ARE THE PLAYERS CARDS \n" + playerCards
  118. + "\n Here are the Dealer Cards \n" + dealerCards);
  119. }
  120.  
  121. /**
  122. * DAY 3
  123. * Add up the value of your cards
  124. * This will return the sum of all cards in a hand of cards
  125. * @param cards an Integer of arrayList of cards
  126. * @return the sum of the cards
  127. */
  128. public static int sumOfCards(ArrayList<Integer> cards) {
  129. int sum = 0;
  130. // add up the value of all the cards in the ArrayList of cards
  131. for(int u=0;u<cards.size();u++){
  132. sum+= cards.get(u);
  133. }
  134. return sum;
  135. }
  136. /**
  137. * Day 4
  138. * This method allows the player to decide to hit of Stay in Black Jack
  139. */
  140. public static void hitOrStay() {
  141.  
  142. String ans = JOptionPane.showInputDialog(null, "Do you want to HIT or STAY???");
  143.  
  144. while ((ans.contains("H") || ans.contains("h")) && sumOfCards(player) < 21) {
  145. // deal a card see method deal
  146. int aCard = deck[deckI++];
  147. player.add(aCard);
  148. CompleteDisplay();
  149. if(sumOfCards(player) < 21){
  150. ans = JOptionPane.showInputDialog(null, "Do you want to HIT or STAY???");
  151. }
  152. if(sumOfCards(player) >21){
  153. CheckAce(player);
  154. }
  155. // check to see if the value of the cards is less than 21
  156. // if the value is less than 21 allow player to choose another card.
  157. }
  158. }
  159. /**
  160. * DAY 4
  161. * This method allows the dealer to decide if they need to hit of Stay
  162. */
  163. public static void DealersTurn() {
  164. while(sumOfCards(dealer)<16){
  165. int aCard = deck[deckI++];
  166. dealer.add(aCard);
  167.  
  168. }
  169. if(sumOfCards(dealer)>21){
  170. CheckAce(dealer);
  171. }
  172. CompleteDisplay();
  173. // while the sum of the dealer cards are less that 16
  174. // add a card to the dealer
  175. // if the sum of the dealer cards are over 21
  176. // check to see if the dealer has an Ace
  177. // call CompleteDisplay();
  178. }
  179.  
  180. /**
  181. * DAY 4 Check to see if there is an Ace in the hand
  182. * This method checks to see if an Ace (11) is in you hand
  183. * If there is an ace, it will change it to a 1
  184. * @param hand all the cards in a hand
  185. */
  186. public static void CheckAce(ArrayList<Integer> hand) {
  187. // loop through the hand
  188. // if the spot is an 11
  189. // set the spot to 1
  190. // return;
  191.  
  192. for(int r = 0;r<hand.size();r++){
  193. if(hand.get(r) == 11){
  194. hand.set(r, 1);
  195. }
  196.  
  197. }
  198. return;
  199.  
  200. }
  201. /**
  202. * Day 5
  203. * This method checks to see who wins the hand of black jack
  204. */
  205. public static void WhoWins() {
  206. CompleteDisplay();
  207. if (sumOfCards(player) > 21)
  208. {
  209. money = money - bet;
  210. JOptionPane.showMessageDialog(null, "House wins the bet \n you have " + money);
  211. } else if (sumOfCards(dealer) > 21)
  212. {
  213. money = money + bet;
  214. JOptionPane.showMessageDialog(null, "Player wins the bet\n you have " + money);
  215. }
  216. else if (sumOfCards(dealer) > sumOfCards(player))
  217. {
  218. money = money - bet;
  219. JOptionPane.showMessageDialog(null, "Player loses the bet\n you have " + money);
  220. }
  221. else if (sumOfCards(player) > sumOfCards(dealer))
  222. {
  223. money = money - bet;
  224. JOptionPane.showMessageDialog(null, "Player wins the bet\n you have " + money);
  225. }
  226. // use 3 more else if to see who won
  227. }
  228. /**
  229. * Day 5
  230. * Swaps every card with another card in the Deck
  231. * Shuffles the deck of cards
  232. */
  233. public static void shuffle() {
  234. for (int e = 0; e < deck.length; e++) {
  235. int rand = (int) (Math.random() * deck.length);
  236. int spare = deck[e]; // make a sparecard with card at spot i
  237. deck[e] = deck[rand]; // set the value of spot i to the value of rand
  238. deck[rand] = spare; // set the value of rand to sparecard
  239. }
  240. }
  241.  
  242. public static void main(String[] args) {
  243.  
  244. // //DAY 1 Build deck and display the deck
  245. // build();
  246. // displayDeck();
  247. // //Day 2
  248. // deal();
  249. while (true) {
  250. build();
  251. shuffle();
  252. String Tbet = JOptionPane.showInputDialog("Enter your bet");
  253. try {
  254. bet = Integer.parseInt(Tbet);
  255. } catch (Exception E) {
  256. bet = 50;
  257. }
  258. deal();
  259. hitOrStay();
  260. DealersTurn();
  261. WhoWins();
  262. }
  263. }
  264. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement