jdalbey

BlackjackConsole.java

Apr 1st, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.35 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Vector;
  3.  
  4. /* Simulation of console-I/O program Blackjack,
  5. using ConsoleApplet as a basis. See the file
  6. ConsoleApplet.java for more information.
  7.  
  8. */
  9. public class BlackjackConsole
  10. {
  11.  
  12. private static Scanner scanner = new Scanner(System.in);
  13. private int[] deck; // An array of 52 Cards, representing the deck.
  14. private int currentPosition; // Current position in the deck
  15. private Vector hand; // The cards in the hand.
  16.  
  17. public static void main(String[] args)
  18. {
  19. new BlackjackConsole().run();
  20. }
  21.  
  22. public void run()
  23. {
  24.  
  25. /*
  26. This program lets the user play Blackjack. The computer
  27. acts as the dealer. The user has a stake of $100, and
  28. makes a bet on each game. The user can leave at any time,
  29. or will be kicked out when he loses all the money.
  30. House rules: The dealer hits on a total of 16 or less
  31. and stands on a total of 17 or more. Dealer wins ties.
  32. A new deck of cards is used for each game.
  33. */
  34.  
  35. int money; // Amount of money the user has.
  36. int bet; // Amount user bets on a game.
  37. boolean userWins; // Did the user win the game?
  38.  
  39. System.out.println("Welcome to the game of blackjack.");
  40. System.out.println();
  41.  
  42. money = 100; // User starts with $100.
  43.  
  44. while (true)
  45. {
  46. System.out.println("You have " + money + " dollars.");
  47. do
  48. {
  49. System.out.println("How many dollars do you want to bet? (Enter 0 to end.)");
  50. System.out.print("? ");
  51. bet = scanner.nextInt();
  52. if (bet < 0 || bet > money)
  53. {
  54. System.out.println("Your answer must be between 0 and " + money + '.');
  55. }
  56. } while (bet < 0 || bet > money);
  57. if (bet == 0)
  58. {
  59. break;
  60. }
  61. userWins = playBlackjack();
  62. if (userWins)
  63. {
  64. money = money + bet;
  65. } else
  66. {
  67. money = money - bet;
  68. }
  69. System.out.println();
  70. if (money == 0)
  71. {
  72. System.out.println("Looks like you've are out of money!");
  73. break;
  74. }
  75. }
  76.  
  77. System.out.println();
  78. System.out.println("You leave with $" + money + '.');
  79.  
  80. } // end main()
  81.  
  82. private boolean playBlackjack()
  83. {
  84. // Let the user play one game of Blackjack.
  85. // Return true if the user wins, false if the user loses.
  86.  
  87. Vector dealerHand; // The dealer's hand.
  88. Vector userHand; // The user's hand.
  89.  
  90. // Create an unshuffled deck of cards.
  91. deck = new int[52];
  92. int cardCt = 0; // How many cards have been created so far.
  93. for (int suit = 0; suit <= 3; suit++)
  94. {
  95. for (int value = 1; value <= 13; value++)
  96. {
  97. deck[cardCt] = value;
  98. cardCt++;
  99. }
  100. }
  101. currentPosition = 0;
  102.  
  103. dealerHand = new Vector();
  104. userHand = new Vector();
  105.  
  106. /* Shuffle the deck, then deal two cards to each player. */
  107.  
  108. shuffle();
  109.  
  110. dealerHand.addElement(dealCard());
  111. dealerHand.addElement(dealCard());
  112. userHand.addElement(dealCard());
  113. userHand.addElement(dealCard());
  114.  
  115. System.out.println();
  116. System.out.println();
  117.  
  118. /* Check if one of the players has Blackjack (two cards totaling to 21).
  119. The player with Blackjack wins the game. Dealer wins ties.
  120. */
  121.  
  122. if (value(dealerHand) == 21)
  123. {
  124. System.out.println("Dealer has the " + showCard(getCard(dealerHand, 0)) + " and the " + showCard(getCard(dealerHand, 1)) + ".");
  125. System.out.println("User has the " + showCard(getCard(userHand, 0)) + " and the " + showCard(getCard(userHand, 1)) + ".");
  126. System.out.println();
  127. System.out.println("Dealer has Blackjack. Dealer wins.");
  128. return false;
  129. }
  130.  
  131. if (value(userHand) == 21)
  132. {
  133. System.out.println("Dealer has the " + showCard(getCard(dealerHand, 0)) + " and the " + showCard(getCard(dealerHand, 1)) + ".");
  134. System.out.println("User has the " + showCard(getCard(userHand, 0)) + " and the " + showCard(getCard(userHand, 1)) + ".");
  135. System.out.println();
  136. System.out.println("You have Blackjack. You win.");
  137. return true;
  138. }
  139.  
  140. /* If neither player has Blackjack, play the game. The user gets a chance
  141. to draw cards (i.e., to "Hit"). The while loop ends when the user
  142. chooses to "Stand" or when the user goes over 21.
  143. */
  144.  
  145. while (true)
  146. {
  147.  
  148. /* Display user's cards, and let user decide to Hit or Stand. */
  149.  
  150. System.out.println();
  151. System.out.println();
  152. System.out.println("Your cards are:");
  153. for (int i = 0; i < userHand.size(); i++)
  154. {
  155. System.out.println(" " + showCard(getCard(userHand, i)));
  156. }
  157. System.out.println("Your total is " + value(userHand));
  158. System.out.println();
  159. System.out.println("Dealer is showing the " + showCard(getCard(dealerHand, 0)));
  160. System.out.println();
  161. System.out.print("Hit (H) or Stand (S)? ");
  162. char userAction; // User's response, 'H' or 'S'.
  163. do
  164. {
  165. userAction = Character.toUpperCase(scanner.next().charAt(0));
  166. if (userAction != 'H' && userAction != 'S')
  167. {
  168. System.out.print("Please respond H or S: ");
  169. }
  170. } while (userAction != 'H' && userAction != 'S');
  171.  
  172. /* If the user Hits, the user gets a card. If the user Stands, the
  173. dealer gets a chance to draw and the game ends.
  174. */
  175.  
  176. if (userAction == 'S')
  177. {
  178. // Loop ends; user is done taking cards.
  179. break;
  180. } else
  181. { // userAction is 'H'.
  182. // Give the user a card. If the user goes over 21, the user loses.
  183. int newCard = dealCard();
  184. userHand.addElement(newCard);
  185. System.out.println();
  186. System.out.println("User hits.");
  187. System.out.println("Your card is the " + showCard(newCard));
  188. System.out.println("Your total is now " + value(userHand));
  189. if (value(userHand) > 21)
  190. {
  191. System.out.println();
  192. System.out.println("You busted by going over 21. You lose.");
  193. System.out.println("Dealer's other card was the " + showCard(getCard(dealerHand, 1)));
  194. return false;
  195. }
  196. }
  197.  
  198. } // end while loop
  199.  
  200. /* If we get to this point, the user has Stood with 21 or less. Now, it's
  201. the dealer's chance to draw. Dealer draws cards until the dealer's total is > 16.
  202. */
  203.  
  204. System.out.println();
  205. System.out.println("User stands.");
  206. System.out.println("Dealer's cards are");
  207. System.out.println(" " + showCard(getCard(dealerHand, 0)));
  208. System.out.println(" " + showCard(getCard(dealerHand, 1)));
  209. while (value(dealerHand) <= 16)
  210. {
  211. int newCard = dealCard();
  212. System.out.println("Dealer hits and gets the " + showCard(newCard));
  213. dealerHand.addElement(newCard);
  214. }
  215. System.out.println("Dealer's total is " + value(dealerHand));
  216.  
  217. /* Now, the winner can be declared. */
  218.  
  219. System.out.println();
  220. if (value(dealerHand) > 21)
  221. {
  222. System.out.println("Dealer busted by going over 21. You win.");
  223. return true;
  224. } else
  225. {
  226. if (value(dealerHand) == value(userHand))
  227. {
  228. System.out.println("Dealer wins on a tie. You lose.");
  229. return false;
  230. } else
  231. {
  232. if (value(dealerHand) > value(userHand))
  233. {
  234. System.out.println("Dealer wins, " + value(dealerHand) + " points to " + value(userHand) + ".");
  235. return false;
  236. } else
  237. {
  238. System.out.println("You win, " + value(userHand) + " points to " + value(dealerHand) + ".");
  239. return true;
  240. }
  241. }
  242. }
  243.  
  244. } // end playBlackjack()
  245.  
  246. public int dealCard()
  247. {
  248. // Deals one card from the deck and returns it.
  249. if (currentPosition == 52)
  250. {
  251. shuffle();
  252. }
  253. currentPosition++;
  254. return deck[currentPosition - 1];
  255. }
  256.  
  257. public void shuffle()
  258. {
  259. // Put all the used cards back into the deck, and shuffle it into
  260. // a random order.
  261. for (int i = 51; i > 0; i--)
  262. {
  263. int rand = (int) (Math.random() * (i + 1));
  264. int temp = deck[i];
  265. deck[i] = deck[rand];
  266. deck[rand] = temp;
  267. }
  268. currentPosition = 0;
  269. }
  270.  
  271. public int getCard(Vector hand, int position)
  272. {
  273. // Get the card from the hand in given position, where positions
  274. // are numbered starting from 0. If the specified position is
  275. // not the position number of a card in the hand, then null
  276. // is returned.
  277. if (position >= 0 && position < hand.size())
  278. {
  279. return ((Integer)hand.elementAt(position)).intValue();
  280. } else
  281. {
  282. return 0;
  283. }
  284. }
  285.  
  286. public int value(Vector hand)
  287. {
  288. // Returns the value of this hand for the
  289. // game of Blackjack.
  290.  
  291. int val; // The value computed for the hand.
  292. boolean ace; // This will be set to true if the
  293. // hand contains an ace.
  294. int cards; // Number of cards in the hand.
  295.  
  296. val = 0;
  297. ace = false;
  298. cards = hand.size();
  299.  
  300. for (int i = 0; i < cards; i++)
  301. {
  302. // Add the value of the i-th card in the hand.
  303. int card; // The i-th card;
  304. int cardVal; // The blackjack value of the i-th card.
  305. card = getCard(hand, i);
  306. cardVal = getCardValue(card); // The normal value, 1 to 13.
  307. if (cardVal > 10)
  308. {
  309. cardVal = 10; // For a Jack, Queen, or King.
  310. }
  311. if (cardVal == 1)
  312. {
  313. ace = true; // There is at least one ace.
  314. }
  315. val = val + cardVal;
  316. }
  317.  
  318. // Now, val is the value of the hand, counting any ace as 1.
  319. // If there is an ace, and if changing its value from 1 to
  320. // 11 would leave the score less than or equal to 21,
  321. // then do so by adding the extra 10 points to val.
  322.  
  323. if (ace == true && val + 10 <= 21)
  324. {
  325. val = val + 10;
  326. }
  327.  
  328. return val;
  329.  
  330. }
  331. public int getCardValue(int card)
  332. {
  333. int result = card;
  334. switch (card)
  335. {
  336. case 11:
  337. case 12:
  338. case 13:
  339. result = 10;
  340. }
  341. return result;
  342. }
  343. public String showCard(int card)
  344. {
  345. switch (card)
  346. {
  347. case 1:
  348. return "Ace";
  349. case 2:
  350. return "2";
  351. case 3:
  352. return "3";
  353. case 4:
  354. return "4";
  355. case 5:
  356. return "5";
  357. case 6:
  358. return "6";
  359. case 7:
  360. return "7";
  361. case 8:
  362. return "8";
  363. case 9:
  364. return "9";
  365. case 10:
  366. return "10";
  367. case 11:
  368. return "Jack";
  369. case 12:
  370. return "Queen";
  371. case 13:
  372. return "King";
  373. default:
  374. return "??";
  375. }
  376. }
  377. } // end class
Add Comment
Please, Sign In to add comment