Advertisement
Guest User

Untitled

a guest
Apr 30th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.45 KB | None | 0 0
  1. import java.util.Collections;
  2. import java.util.Scanner;
  3. import java.util.ArrayList;
  4.  
  5. public class Blackjack {
  6.  
  7. public static ArrayList<cards> deck = new ArrayList<cards>();
  8. public static ArrayList<Player> playerDatabase = new ArrayList<Player>();
  9.  
  10. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~MAIN CLASS~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
  11. public static void main(String args []){
  12.  
  13. playBJ();
  14.  
  15.  
  16. /*
  17. int i;
  18. int decks = 3; // where decks determines the amount of decks being played.
  19. for (int x = 0; x < decks; x++){
  20. for(i = deck.size() - 1; i > -1; i--){
  21. System.out.println(showHand(playerDatabase));
  22. //System.out.println(toString(deck.get(i)));
  23. deck.remove(i);
  24. }
  25. }
  26. */
  27.  
  28.  
  29. }//~~~~~~~~~~~~~~~~~~~~~~~~~~~~END OF MAIN CLASS~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
  30.  
  31.  
  32.  
  33. public static String toString(cards blah){
  34. // toString. This is used to examine the deck, and at the points where the numbers are
  35. // 11, 12, 13, 1, it changes them to the appropriate variable.
  36.  
  37. String s = "";
  38. switch( blah.getValue()){
  39. case 1:
  40. s = "A";
  41. break;
  42. case 11:
  43. s = "J";
  44. break;
  45. case 12:
  46. s = "Q";
  47. break;
  48. case 13:
  49. s = "K";
  50. break;
  51. default:
  52. s += blah.getValue(); // this is the same as s = s + blah.getValue();
  53. break;
  54. }// end of switch statement 1
  55.  
  56. switch( blah.getSuit()){
  57. case 1:
  58. return s + "♥";
  59. case 2:
  60. return s + "♦";
  61. case 3:
  62. return s + "♣";
  63. default:
  64. return s + "♠";
  65.  
  66. }// end of switch statement 2
  67. }// end of function
  68.  
  69. // SHUFFEL FUNCTION
  70.  
  71. public static void shuffle(){
  72. for(int i = 1; i < 5; i++){
  73. for (int j = 1; j < 14; j++){
  74. deck.add(new cards(i, j));
  75. }// end of loop 2
  76. }// end of loop 1
  77. Collections.shuffle(deck);
  78. }
  79.  
  80. // DRAW FUNCTION
  81. public static cards draw(ArrayList<cards> deal){
  82. cards tmp = new cards(0,0);
  83. tmp = deal.get(0);
  84. return tmp;
  85. }
  86.  
  87.  
  88. // DEAL FUNCTION
  89. public static void deal(){
  90. shuffle();
  91. for(int i = 0; i < 2; i++){
  92. for(int j = 1; j < playerDatabase.size() -1; j++){
  93. Player tmp = playerDatabase.get(j); // getting cards from deck
  94. cards tmpCard = draw(deck);
  95. deck.remove(0);
  96.  
  97. ArrayList<cards> tmpArray = new ArrayList<cards>();
  98. tmpArray.add(tmpCard); // adding card from deck to player's hand
  99. tmp.setCardsInHand(tmpArray);
  100. playerDatabase.set(j, tmp);
  101.  
  102. }
  103. }
  104. }
  105.  
  106. // CREATE PLAYER FUNCTION
  107. public static void createPlayer(){
  108.  
  109. String response = "yes";
  110. Scanner sc = new Scanner(System.in);
  111.  
  112. ArrayList<cards> dealer = new ArrayList<cards>();
  113. ArrayList<cards> cardsInHand = new ArrayList<cards>();
  114.  
  115. playerDatabase.add(new Player("Dealer", 0.0, 0, 0, false, dealer )); // adds the dealer to spot 1
  116.  
  117. while(response.equals("yes") || response.equals("Yes") || response.equals("Y")){
  118. System.out.println("What is your name?");
  119. String name = sc.nextLine();
  120. System.out.println("How much money do you have?");
  121. double money = sc.nextInt();
  122. playerDatabase.add(new Player(name, money, 0, 0, false, cardsInHand));
  123. System.out.println("Would you like to create another player?");
  124. response = sc.nextLine();
  125. response = sc.nextLine();
  126.  
  127.  
  128. // System.out.println(playerDatabase.size());
  129.  
  130. }// end of while loop
  131. // this sets the dealer's money to 25x the amount of money the player's have
  132. double dealerMoney = 0;
  133. for(int x = 1; x < playerDatabase.size() - 1; x++){
  134. Player tmp = playerDatabase.get(x);
  135. double playerMoney = tmp.getMoney();
  136. dealerMoney += playerMoney * 25;
  137. }
  138. playerDatabase.get(1).setMoney(dealerMoney);
  139. sc.close();
  140. }// end of CREATE PLAYER FUNCTION
  141.  
  142. // Get Value
  143. // gets the value of the player's hand by adding up the cards in it.
  144.  
  145. // SHOW HAND FUNCTION
  146. public static int showHand(ArrayList<Player> Player){
  147. int tmpV;
  148. for(int x = 1; x <= playerDatabase.size() - 1; x++){
  149. Player tmp = playerDatabase.get(x);
  150. tmpV = tmp.getHandValue();
  151.  
  152. ArrayList<cards> tmpArray = new ArrayList<cards>();
  153. tmpArray = tmp.getCardsInHand();
  154. for(int i = 0; i < tmpArray.size() - 1; i++){
  155. System.out.println(toString(tmpArray.get(i)));
  156. System.out.printf("\n Total Hand Value: ");
  157. }
  158. return tmpV;
  159. }
  160. return 0;
  161.  
  162. }
  163.  
  164.  
  165. // PlayBJ
  166. public static void playBJ(){
  167.  
  168.  
  169. createPlayer();
  170. double bet;
  171. double totalBetSum = 0;
  172.  
  173. while(totalBetSum == 0){
  174. shuffle(); // creates a shuffled deck
  175. Scanner sc = new Scanner(System.in);
  176. for(int j = 1; j <= playerDatabase.size() -1; j++){ // Handles the betting section
  177. System.out.println("I'm entering the betting loop");
  178. System.out.println("There are : " + playerDatabase.size() + " people in the database");
  179. Player tmp = playerDatabase.get(j);
  180. String tmpName = tmp.getName();
  181. System.out.println("How much does " + tmpName + " wish to bet?");
  182. String ii = sc.nextLine();
  183. bet = sc.nextDouble();
  184. tmp.setBet(bet);
  185. totalBetSum += tmp.getBet();
  186.  
  187. if(tmp.getBet() == 0){
  188. tmp.setTurn(true);
  189. }
  190. playerDatabase.set(j, tmp);
  191. } // end of betting section
  192.  
  193. System.out.print("Cards are being delt...");
  194.  
  195. for(int x = 1; x <= playerDatabase.size() - 1; x++){ // starts at one because it doesn't include the dealer
  196. System.out.println("I'm entering the dealing loop"); // TESTING
  197. Player tmp = playerDatabase.get(x);
  198. String playerName = tmp.getName();
  199. if(tmp.isTurn() == false){ // means they haven't had a chance to go yet and that you've bet
  200. System.out.print(playerName + " has been delt: " + showHand(playerDatabase) + "\n");
  201. System.out.printf("1: STAY \n 2: HIT \n 3: DOUBLE \n 4: SPLIT");
  202. int response = 0;
  203. response = sc.nextInt();
  204. response = sc.nextInt(); // THIS LINE OF CODE MAY NEED TO BE REMOVED FOR TESTING PURPOSES
  205. while(response > 4 || response < 1){
  206. System.out.printf("\nPlease enter a number between 1 and 4, to indicate you choice.\n");
  207. response = sc.nextInt();
  208. response = sc.nextInt(); // THIS LINE OF CODE MAY NEED TO BE REMOVED FOR TESTING PURPOSES
  209. }
  210. if(response == 1){
  211. tmp.setTurn(true);
  212. }
  213. else if(response == 2){
  214. while(response != 1){
  215. cards tmpCard = draw(deck);
  216. tmp.getCardsInHand().add(tmpCard); // adds the cards to player's hand (ArrayList)
  217. int tmpCardValue = tmpCard.getValue();
  218. int newCardValue = tmp.getHandValue() + tmpCardValue; // calculates the sum of the player's hand
  219. tmp.setHandValue(newCardValue);
  220. if(tmp.getHandValue() <= 21){
  221. System.out.println("Hand Value: " + tmp.getHandValue()); //print the hand
  222. System.out.printf("1: STAY \n 2: HIT \n");
  223. while(response > 2 || response < 1){
  224. response = sc.nextInt();
  225. System.out.printf("Invalid number. \n 1: STAY \n 2: HIT \n");
  226. }
  227. }
  228. else{
  229. System.out.println("BUST!");
  230. double money = tmp.getMoney() - tmp.getBet();
  231. tmp.setMoney(money);
  232. }
  233. }
  234. }
  235. else if(response == 3){
  236. while(response != 1){
  237. cards tmpCard = draw(deck);
  238. tmp.getCardsInHand().add(tmpCard);
  239. int tmpCardValue = tmpCard.getValue();
  240. int newCardValue = tmp.getHandValue() + tmpCardValue;
  241. tmp.setHandValue(newCardValue);
  242.  
  243. }
  244. }
  245.  
  246. }// end of 1st if loop, which determines turn
  247. }
  248. sc.close();
  249. }
  250.  
  251.  
  252. }// end of PlayBJ
  253.  
  254.  
  255.  
  256. }// end of class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement