Advertisement
Guest User

Untitled

a guest
May 4th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.92 KB | None | 0 0
  1. import java.util.*;
  2. class Blackjack{
  3. public static void main (String[] arg) {
  4. Deck d = new Deck(); d.shuffle();
  5. Hand h = new Hand();
  6. Score s = new Score(); //added for bonus
  7. System.out.println(d);
  8. System.out.println(d.hit());
  9. System.out.println( d.decode(d.hit()) );
  10. h.add(5);
  11. h.add(10);
  12. System.out.println(h.display());
  13.  
  14. while(true) {
  15. System.out.print("Enter (n)ew hand, (h)it, or (q)uit: ");
  16. Scanner scan = new Scanner (System.in);
  17. // these variables are for collecting the information for the Score
  18. int [] line = new int [5]; //added for Bonus
  19. int handScore = 0; //added for Bonus
  20. char c = scan.next().charAt(0);
  21. if (c == 'n' || c == 'N') {
  22. System.out.println(c);
  23.  
  24. h.reset();
  25. if (d.currentIndex < 51){
  26. int card= d.hit(); h.add(card);
  27. card= d.hit(); h.add(card);
  28. h.display();
  29. System.out.println();
  30. } else if (d.currentIndex > 51) {
  31. System.out.println("--- NO CARDS LEFT ON DECK ---");
  32. System.out.println();
  33. break;
  34. } else {
  35. System.out.println("-- NOT ENOUGH CARDS LEFT ON DECK--");
  36. System.out.println();
  37. break;
  38. }
  39. } else if (c == 'h' || c == 'H') {
  40. System.out.println(c);
  41. if (d.currentIndex < 52){ //must be 52
  42. int handValue = h.scoreInHand() ;
  43. if (handValue > 21) { //already busted
  44. System.out.println("--- GET A NEW HAND --- ");
  45. System.out.println();
  46. } else if (handValue == 21) {//already blackjack
  47. System.out.println("--- BlackJack --- ");
  48. System.out.println();
  49. } else if (h.firstIndex > 4 ) {
  50. System.out.println( "-- ALREADY 5 CARDS IN HAND ---");
  51. h.display();
  52. System.out.println( "--- GET A NEW HAND ---");
  53. System.out.println();
  54. } else if (h.firstIndex == 0) {
  55. System.out.println("--- GET A NEW HAND FIRST ---");
  56. System.out.println();
  57. } else {
  58. int card= d.hit(); h.add(card);
  59. h.display(); System.out.println();
  60. }
  61. } else { // > or = 52
  62. System.out.println("----- NO CARDS LEFT ON DECK -----");
  63. System.out.println();
  64. break;
  65. }//end if (d.firstIndex < 52
  66.  
  67. } else if (c == 'q' || c == 'Q') {
  68. System.out.println(c);
  69. //same as the if(c == n), so it can collect the last game.
  70.  
  71. break;
  72. }
  73.  
  74. } //while
  75. //prints the scores of the games the user played while the game was run.
  76. //System.out.print(s.display());System.out.println(); //added for Bonus
  77. System.out.println("BYE!");
  78. }//end main
  79. }
  80.  
  81. class Deck{
  82. private int[] cards;
  83. public int currentIndex;
  84. Deck(){
  85. currentIndex=0;
  86. cards=new int[52];
  87. for (int i=0; i<cards.length; i++)
  88. cards[i]=i;
  89. }
  90. public String toString(){
  91. String s="";
  92. for (int i=currentIndex;i<cards.length; i++)
  93. {
  94. /* s=s+" "+cards[i];*/
  95. String code=decode(cards[i]);
  96. s=s+" "+code;
  97. }
  98. return s;
  99. }
  100. public String decode(int value)
  101. {
  102. String code=""+"HCDS".charAt (value/13)
  103. +"23456789TJQKA".charAt(value%13);
  104. return code;
  105. }
  106. public void shuffle(){
  107. int j=0;
  108. for (int i=0;i<cards.length; i++)
  109. {
  110. j=(int)(52*Math.random());
  111. swap(i,j);
  112. }
  113. }
  114. public void swap(int i, int j){
  115. int temp=cards[i];
  116. cards[i]=cards[j];
  117. cards[j]=temp;
  118.  
  119.  
  120.  
  121.  
  122. }
  123. public int hit(){
  124. int i=cards[currentIndex];
  125. currentIndex++;
  126.  
  127. return i;
  128.  
  129.  
  130. }
  131. }
  132.  
  133.  
  134.  
  135. class Hand{
  136. private int []Hand;
  137. public int firstIndex;
  138. Hand(){
  139. Hand=new int[5];
  140. for (int i=0; i<Hand.length; i++)
  141. Hand[i]=-1;
  142. firstIndex=0;
  143. }
  144. public String display(){
  145. String s="";
  146. String empty="Hand is empty";
  147. for (int i=0; i<firstIndex;i++)
  148. s=s+" "+decode(Hand[i]);
  149.  
  150. if (s.length()==0)
  151. s= empty;
  152. String sih="";
  153. int sihand= scoreInHand();
  154. if(sihand>0&&sihand<21)
  155. sih=""+sihand;
  156. else if(sihand==21&&firstIndex==2)
  157. sih="blackjack";
  158. else if (sihand==21&&firstIndex>2)
  159. sih=""+sihand;
  160. else
  161. sih="BUST";
  162. System.out.println(s+"n"+sih);
  163. return s.trim();
  164. }
  165. public void reset(){
  166. for (int i=0; i<Hand.length;i++)
  167. Hand[i]=-1;
  168. firstIndex=0;
  169. }
  170. public void add(int card){
  171. Hand[firstIndex]=card;
  172. firstIndex++;
  173. }
  174. public String decode2(int value){
  175. String code=""+"HCDS".charAt(value/13)
  176. +"23456789TJQA".charAt(value%13);
  177. return code;
  178. }
  179.  
  180. public String decode(int value){
  181.  
  182. String[] pattern={"Heart","Clubs","Diamond","Spades"};
  183. String[] num={"two","three","four","five","six","seven","eight","nine","ten","jack","queen","king","ace"};
  184. return num[value%13]+ " of " + pattern[value/13];
  185. }
  186. public int scoreInHand(){
  187. int[] s={2,3,4,5,6,7,8,9,10,10,10,10};
  188. boolean firstAce=true;
  189. int value=0;
  190. int sum=0;
  191. for (int i=0; i<firstIndex; i++)
  192. {
  193. value=Hand[i]%13;
  194. if (value==12)
  195. {
  196. if (firstAce)
  197. {
  198. value=11;
  199. firstAce=false;
  200. }
  201. else
  202. value=1;
  203.  
  204. }
  205. else
  206. value=s[value];
  207.  
  208.  
  209.  
  210. sum=sum+value;
  211. }
  212. return sum;
  213.  
  214. }
  215. public String toString(){
  216. String s="";
  217. for (int i=0; i<5; i++)
  218. {
  219. if (i<firstIndex)
  220. s=s+" | " + decode2(Hand[i])+" ";
  221. else
  222. s=s+" ";
  223. }
  224. s=s+firstIndex;
  225. return s;
  226. }
  227.  
  228. }//(2,3,4,5,6,7,8,9,10,10,10,10,11)
  229.  
  230.  
  231. class Score extends Deck{
  232. private String [] cuts=new String [26];
  233. private int gfirstIndex;
  234. Score(){
  235. cuts=new String[26];
  236. gfirstIndex=0;
  237. }
  238. public void markScore(Hand h, int score)
  239. {
  240. String s="";
  241. char fi;
  242. String hand=h.toString();
  243. int firstIndex;
  244. fi=hand.charAt(hand.length()-1);
  245. firstIndex=fi-'0';
  246. hand=hand.substring(0,hand.length()-1);
  247. s=s+" | "+(gfirstIndex+1)+(((gfirstIndex+1)<10)?" ":" ");
  248. s=s+hand+" ";
  249. s=s+"(score="+score+")";
  250. if (score>21)
  251. s=s+"(BUSTS)";
  252. else if (score==21&&firstIndex==2)
  253. s=s+"(BLACKJACK)";
  254. cuts[gfirstIndex]=s;
  255. gfirstIndex++;
  256. }
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement