Advertisement
Guest User

Untitled

a guest
Sep 29th, 2015
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.61 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.util.List;
  5. import java.util.ArrayList;
  6. import java.util.Random;
  7.  
  8. public class BJ extends JFrame {
  9.  
  10. private GridBagLayout layout = new GridBagLayout();
  11. private GridBagConstraints gbc = new GridBagConstraints();
  12. private Random random = new Random();
  13.  
  14. private JLabel cards = new JLabel();
  15.  
  16. private JButton hit;
  17. private JButton stay;
  18. private JButton dealCards;
  19. private JButton increaseBet;
  20. private JButton decreaseBet;
  21.  
  22. private JPanel topPanel = new JPanel();
  23. private JPanel playerCards = new JPanel();
  24. private JPanel dealerCards = new JPanel();
  25. private JPanel allCards = new JPanel();
  26. private JPanel leftPanel = new JPanel();
  27. private JPanel buttons;
  28.  
  29. private JTextField playerTotal;
  30. private JTextField dealerTotal;
  31. private JTextField result;
  32. private JTextField bet;
  33.  
  34. private String deck[] = new String[52];
  35. private String faces[] = {"two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "jack", "queen", "king", "ace"};
  36. private String suits[] = {"spades", "hearts", "diamonds", "clubs"};
  37. private int currentCard = 0;
  38.  
  39. private boolean gameInProgress = false;
  40.  
  41. private int xPosDealer = 0;
  42. private int xPosPlayer = 0;
  43. private int currentPlayerTotal = 0;
  44. private int currentDealerTotal = 0;
  45. private int betAmount = 0;
  46.  
  47. private String playerCardsInHand;
  48. private String dealerCardsInHand;
  49.  
  50. enum Status {WON, LOST, CONTINUE};
  51. private Status gameStatus;
  52.  
  53. public BJ() {
  54.  
  55. super("BlackJack");
  56. setLayout(new BorderLayout());
  57. hit = new JButton("HIT");
  58.  
  59. hit.addActionListener ( new ActionListener() {
  60.  
  61. public void actionPerformed (ActionEvent e) {
  62.  
  63. if (e.getSource() == hit && gameInProgress == true) {
  64.  
  65. displayPlayerCard();
  66.  
  67. }
  68. }
  69. }
  70. );
  71.  
  72. stay = new JButton("STAY");
  73.  
  74. stay.addActionListener(
  75.  
  76. new ActionListener() {
  77.  
  78. public void actionPerformed (ActionEvent e) {
  79.  
  80. if (e.getSource() == stay && gameStatus != Status.LOST) {
  81. while (currentDealerTotal < 17)
  82. displayDealerCard();
  83. }
  84. gameResult();
  85.  
  86. }
  87. }
  88. );
  89.  
  90. dealCards = new JButton("DEAL CARDS");
  91. dealCards.addActionListener (new ActionListener() {
  92. public void actionPerformed (ActionEvent e) {
  93.  
  94. if (e.getSource() == dealCards && gameInProgress == false) {
  95. clearTable();
  96. startGame();
  97. checkStatus();
  98. }
  99.  
  100. }
  101.  
  102. });
  103.  
  104. increaseBet = new JButton("INCREASE BET");
  105. decreaseBet = new JButton("DECREASE BET");
  106.  
  107. increaseBet.addActionListener (new ActionListener() {
  108.  
  109. public void actionPerformed (ActionEvent e) {
  110.  
  111. if (e.getSource() == increaseBet && gameInProgress == false) {
  112.  
  113. betAmount += 5;
  114. bet.setText(Integer.toString(betAmount));
  115.  
  116. }
  117.  
  118. }
  119.  
  120. });
  121.  
  122. playerTotal = new JTextField(10);
  123. dealerTotal = new JTextField(10);
  124. result = new JTextField(10);
  125.  
  126. playerTotal.setEditable(false);
  127. dealerTotal.setEditable(false);
  128. result.setEditable(false);
  129.  
  130.  
  131. buttons = new JPanel();
  132. buttons.add(hit);
  133. buttons.add(stay);
  134. add(buttons, BorderLayout.SOUTH);
  135.  
  136. topPanel.add(dealCards);
  137. topPanel.add(playerTotal);
  138. topPanel.add(dealerTotal);
  139. topPanel.add(result);
  140. add(topPanel, BorderLayout.NORTH);
  141.  
  142. playerCards.setLayout(new GridLayout());
  143. dealerCards.setLayout(new GridLayout());
  144.  
  145. fillDeck(deck);
  146. shuffleCards();
  147.  
  148. }
  149.  
  150. public void dealerTotal (String cards) {
  151.  
  152. for (int i=0; i<faces.length; i++) {
  153.  
  154. if (cards.equals(faces[i]) && i <=8)
  155. currentDealerTotal += i + 2;
  156.  
  157. else if (cards.equals(faces[i]) && i == 12) {
  158.  
  159. if ( (currentPlayerTotal += 11) > 21)
  160. currentPlayerTotal += 1;
  161.  
  162. if ( (currentPlayerTotal += 11) == 21)
  163. currentPlayerTotal += 11;
  164.  
  165. }
  166.  
  167. else
  168. currentDealerTotal += 10;
  169.  
  170. }
  171.  
  172. dealerTotal.setText(Integer.toString(currentDealerTotal));
  173. dealerCardsInHand = "";
  174.  
  175. if (currentDealerTotal > 21) {
  176.  
  177. gameStatus = Status.WON;
  178. gameResult();
  179.  
  180. }
  181.  
  182. }
  183.  
  184. public void playerTotal (String cards) {
  185.  
  186. for (int i=0; i<faces.length; i++) {
  187.  
  188. if (cards.equals(faces[i]) && i <=8)
  189. currentPlayerTotal += i + 2;
  190.  
  191. else if (cards.equals(faces[i]) && i == 12) {
  192.  
  193. if ( (currentPlayerTotal += 11) > 21)
  194. currentPlayerTotal += 1;
  195.  
  196. else if ( (currentPlayerTotal += 11) == 21)
  197. currentPlayerTotal += 11;
  198. }
  199.  
  200. else
  201. currentPlayerTotal += 10;
  202.  
  203. }
  204.  
  205. playerTotal.setText(Integer.toString(currentPlayerTotal));
  206. playerCardsInHand = "";
  207.  
  208. checkStatus();
  209.  
  210. }
  211.  
  212. public void checkStatus () {
  213.  
  214. if (currentPlayerTotal > 21) {
  215.  
  216. gameStatus = Status.LOST;
  217. gameResult();
  218.  
  219. }
  220.  
  221. else if (currentPlayerTotal == 21) {
  222.  
  223. while (currentDealerTotal < 17)
  224. displayDealerCard();
  225.  
  226. gameResult();
  227.  
  228. }
  229.  
  230. else if (currentDealerTotal >= 17 && currentPlayerTotal > currentDealerTotal) {
  231.  
  232. gameStatus = Status.WON;
  233. gameResult();
  234.  
  235. }
  236.  
  237. }
  238.  
  239. public void startGame() {
  240.  
  241. currentPlayerTotal = 0;
  242. currentDealerTotal = 0;
  243. displayPlayerCard();
  244. displayPlayerCard();
  245. displayDealerCard();
  246. displayDealerCard();
  247. gameInProgress = true;
  248. gameStatus = Status.CONTINUE;
  249. xPosPlayer = 3;
  250. xPosDealer = 3;
  251.  
  252. if (currentCard > 40) {
  253. currentCard = 0;
  254. shuffleCards();
  255. }
  256.  
  257. }
  258.  
  259. public void gameResult() {
  260.  
  261. if (gameStatus == Status.WON)
  262. result.setText("You Win!");
  263.  
  264. else if (gameStatus == Status.LOST)
  265. result.setText("You Lost!");
  266.  
  267. else if (currentDealerTotal > currentPlayerTotal)
  268. result.setText("You Lost!");
  269.  
  270. else if (currentDealerTotal == currentPlayerTotal)
  271. result.setText("Push");
  272.  
  273. else
  274. result.setText("You Win!");
  275.  
  276. gameInProgress = false;
  277.  
  278. }
  279.  
  280. public void clearTable() {
  281.  
  282. playerCards.removeAll();
  283. playerCards.revalidate();
  284. playerCards.repaint();
  285. dealerCards.removeAll();
  286. dealerCards.revalidate();
  287. dealerCards.repaint();
  288. result.setText("");
  289. playerCardsInHand = "";
  290. dealerCardsInHand = "";
  291.  
  292. }
  293.  
  294. public void shuffleCards() {
  295.  
  296. for (int i=0; i<deck.length; i++) {
  297.  
  298. int num = random.nextInt(52);
  299. String temp = deck[i];
  300. deck[i] = deck[num];
  301. deck[num] = temp;
  302.  
  303. }
  304.  
  305. }
  306.  
  307. public void displayPlayerCard() {
  308.  
  309. String cardName[] = deck[currentCard].split(" ");
  310. playerCardsInHand = cardName[0];
  311.  
  312. String cardFile = cardName[0] + "of" + cardName[1] + ".jpg";
  313. Icon card = new ImageIcon(getClass().getResource(cardFile));
  314. playerCards.add(new JLabel(card));
  315. playerCards.revalidate();
  316. playerCards.repaint();
  317. add(playerCards, BorderLayout.WEST);
  318. currentCard++;
  319. playerTotal(playerCardsInHand);
  320.  
  321. }
  322.  
  323. public void displayDealerCard() {
  324.  
  325. String cardName[] = deck[currentCard].split(" ");
  326. dealerCardsInHand = cardName[0];
  327.  
  328. String cardFile = cardName[0] + "of" + cardName[1] + ".jpg";
  329. Icon card = new ImageIcon(getClass().getResource(cardFile));
  330. dealerCards.add(new JLabel(card));
  331. dealerCards.revalidate();
  332. dealerCards.repaint();
  333. add(dealerCards, BorderLayout.EAST);
  334. currentCard++;
  335. dealerTotal(dealerCardsInHand);
  336.  
  337. }
  338.  
  339. public void fillDeck (String[] array) {
  340.  
  341. for (int i= 0; i<array.length; i++) {
  342.  
  343. array[i] = faces[i % 13] + " " + suits[i/13];
  344.  
  345. }
  346.  
  347. }
  348.  
  349. public static void main (String[] args) {
  350.  
  351. BJ bj = new BJ();
  352. bj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  353. bj.setVisible(true);
  354. bj.setSize(750, 750);
  355.  
  356. }
  357.  
  358. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement