Advertisement
Guest User

BJ Transfer

a guest
Nov 16th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.38 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Blackjack {
  4.  
  5. public static void main(String[] args) {
  6. split(5,5);
  7. run(startBet());
  8. }
  9.  
  10. private static void run(int UserAmount){
  11.  
  12. ArrayList<String> UserCard = new ArrayList<>();
  13. ArrayList<String> CpuCard = new ArrayList<>();
  14.  
  15. // Getting random starting numbers \\
  16. int UserRand1 = randomNumber();
  17. int UserRand2 = randomNumber();
  18. int CpuRand1 = randomNumber();
  19.  
  20. // Setting Values to the Cards \\
  21. int UserValue = (Rand2Value(UserRand1) + Rand2Value(UserRand2));
  22. int CpuValue = Rand2Value(CpuRand1);
  23.  
  24. // Setting Previous Values to Cards \\
  25. UserCard.add(Rand2Card(UserRand1));
  26. UserCard.add(Rand2Card(UserRand2));
  27. CpuCard.add(Rand2Card(CpuRand1));
  28.  
  29. // Asking the user how much he/she wants to bet \\
  30. int Bet = bet(UserAmount);
  31.  
  32. // Seeing if the user has blackjack \\
  33. Blackjack(Bet,UserValue,UserAmount);
  34.  
  35. // Seeing if user wants to double \\
  36. Double(CpuValue,UserValue,Bet, UserAmount,UserCard,CpuCard);
  37.  
  38. // Loop for hit or stand \\
  39. UserValue = HitSequence(UserCard,CpuCard,UserValue);
  40.  
  41. // Seeing if user busted \\
  42. UserValue = BustCheck(UserCard,CpuCard,UserValue);
  43.  
  44. // Computer Hitting \\
  45. CpuValue = CpuHitting(CpuCard,CpuValue);
  46.  
  47. // Calculations for Winner | Printing Results \\
  48. Calculations(UserValue,CpuValue,UserCard,CpuCard,UserAmount,Bet);
  49. }
  50.  
  51. private static int randomNumber(){
  52. Random random = new Random();
  53. int rand = random.nextInt(13) + 1;
  54. return rand;
  55. }
  56.  
  57. private static int startBet(){
  58. Scanner UserIn = new Scanner(System.in);
  59. System.out.println("How much money would you like to start with?");
  60. int UserAmount = (int) UserIn.nextDouble();
  61. return UserAmount;
  62. }
  63.  
  64. private static String Rand2Card(int Rand) {
  65.  
  66. switch (Rand){
  67. case 1:
  68. return "A";
  69. case 2:
  70. return "2";
  71. case 3:
  72. return "3";
  73. case 4:
  74. return "4";
  75. case 5:
  76. return "5";
  77. case 6:
  78. return "6";
  79. case 7:
  80. return "7";
  81. case 8:
  82. return "8";
  83. case 9:
  84. return "9";
  85. case 10:
  86. return "10";
  87. case 11:
  88. return "J";
  89. case 12:
  90. return "Q";
  91. case 13:
  92. return "K";
  93. default:
  94. return "0";
  95.  
  96. }
  97. }
  98.  
  99. private static int Rand2Value(int Rand) {
  100.  
  101. switch (Rand){
  102. case 1:
  103. return 11;
  104. case 2:
  105. return 2;
  106. case 3:
  107. return 3;
  108. case 4:
  109. return 4;
  110. case 5:
  111. return 5;
  112. case 6:
  113. return 6;
  114. case 7:
  115. return 7;
  116. case 8:
  117. return 8;
  118. case 9:
  119. return 9;
  120. case 10:
  121. return 10;
  122. case 11:
  123. return 10;
  124. case 12:
  125. return 10;
  126. case 13:
  127. return 10;
  128. default:
  129. return 0;
  130. }
  131.  
  132. }
  133.  
  134. private static void Blackjack(int Bet, int Value, int Amt){
  135. if(Value == 21){
  136. System.out.println("You got blackjack! ");
  137. Amt += (int) (Bet * 1.5);
  138. again(Amt);
  139. }
  140. }
  141.  
  142. private static int HitOrStand(ArrayList<String> Array, ArrayList<String> CpuArray, int size, int Value){
  143.  
  144. boolean badIn = true;
  145.  
  146. while (badIn) {
  147. badIn = false;
  148. System.out.print("Would you like to hit or stand! You have a ");
  149. PrintArray(Array);
  150. System.out.println("! A total of " + Value + "!");
  151. System.out.println("The opponent has a " + CpuArray.get(0) + " showing!");
  152. Scanner UserIn = new Scanner(System.in);
  153. String UserEnter = UserIn.next().substring(0,1).toUpperCase();
  154.  
  155. switch (UserEnter) {
  156. case "H":
  157. return 1;
  158. case "S":
  159. return 2;
  160. default:
  161. System.out.print("Invalid! ");
  162. badIn = true;
  163. break;
  164. }
  165. }
  166. return 0; // Just for Safety Measures
  167.  
  168. }
  169.  
  170. private static int bust(ArrayList<String> Array, int Value){
  171.  
  172. for(int i = 0; i < Array.size();i++) {
  173. if (Array.get(i).contains("A")) {
  174. return (Value - 10);
  175. }
  176. }
  177. return Value;
  178. }
  179.  
  180. private static void Double(int CpuValue, int Value, int Bet, int Amt, ArrayList<String> Array, ArrayList<String> CpuArray) {
  181. boolean badIn = true;
  182.  
  183. if (Amt != Bet) {
  184. while (badIn) {
  185. badIn = false;
  186. System.out.print("Would you like to double (Bet More)? You have a ");
  187. PrintArray(Array);
  188. System.out.println("! A total of " + Value + "!");
  189. System.out.println("The opponent has a " + CpuArray.get(0) + " showing!");
  190. System.out.println("You will only be able to hit once! And, can only increase up to double your bet");
  191. Scanner UserIn = new Scanner(System.in);
  192. String UserEnter = UserIn.next().substring(0, 1).toUpperCase();
  193.  
  194. switch (UserEnter) {
  195. case "Y":
  196. int rand = randomNumber();
  197. Bet += bet(Bet);
  198. Array.add(Rand2Card(rand));
  199. Value += Rand2Value(rand);
  200. Value = DoubleBustCheck(Array,Value);
  201. CpuValue = CpuHitting(CpuArray,CpuValue);
  202. Calculations(Value,CpuValue,Array,CpuArray,Amt,Bet);
  203. break;
  204. case "N":
  205. break;
  206. default:
  207. System.out.print("Invalid! ");
  208. badIn = true;
  209. break;
  210. }
  211. }
  212. }
  213. }
  214.  
  215. private static int HitSequence(ArrayList<String> Array, ArrayList<String> CpuArray, int Value){
  216. boolean hit = true;
  217. while (Value <= 21 && hit){
  218.  
  219. // Asking User if Hit or Stand \\
  220. int UserChoice = HitOrStand(Array,CpuArray,Array.size(),Value);
  221.  
  222. if (UserChoice == 1){
  223. // Performing Actions Based on Hit or Stand \\
  224. int UserRand = randomNumber();
  225. Array.add(Rand2Card(UserRand));
  226. Value += Rand2Value(UserRand);
  227. hit = true;
  228. }else{
  229. hit = false;
  230. }
  231. }
  232. return Value;
  233. }
  234.  
  235. private static int AceAmount(ArrayList<String> Array) {
  236. int Amount = 0;
  237. for(int i = 0; i < Array.size(); i ++) {
  238. if(Array.get(i).contains("A")) {
  239. Amount ++;
  240. }
  241. }
  242. return Amount;
  243. }
  244.  
  245. private static int BustCheck(ArrayList<String> Array, ArrayList<String> CpuArray, int UserValue){
  246. int RunAmt = 0;
  247. while (UserValue >= 22){
  248. int AmountAce = AceAmount(Array);
  249. int Returned = bust(Array,UserValue);
  250. if (Returned < 22 && RunAmt < AmountAce){
  251. UserValue = HitSequence(Array,CpuArray,Returned);
  252. RunAmt++;
  253. } else {
  254. System.out.println("You busted with a value of " + UserValue);
  255. return UserValue;
  256. }
  257. }
  258. return UserValue;
  259. }
  260.  
  261. private static int DoubleBustCheck(ArrayList<String> Array, int UserValue){
  262. int RunAmt = 0;
  263. while (UserValue >= 22){
  264. int AmountAce = AceAmount(Array);
  265. int Returned = bust(Array,UserValue);
  266. if (Returned < 22 && RunAmt < AmountAce){
  267. UserValue = Returned;
  268. RunAmt++;
  269. } else {
  270. System.out.println("You busted with a value of " + UserValue);
  271. return UserValue;
  272. }
  273. }
  274. return UserValue;
  275. }
  276.  
  277. private static int CpuHitting(ArrayList<String> Array, int Value){
  278. int RunAmt = 0;
  279. while(Value < 18){
  280. int AceAmt = AceAmount(Array);
  281. if(Value == 17 && RunAmt < AceAmt){
  282. int Rand = randomNumber();
  283. Value += Rand2Value(Rand);
  284. Array.add(Rand2Card(Rand));
  285. RunAmt ++;
  286. }else if(Value == 17){
  287. return Value;
  288. }else{
  289. int Rand = randomNumber();
  290. Value += Rand2Value(Rand);
  291. Array.add(Rand2Card(Rand));
  292. }
  293. }
  294. return Value;
  295. }
  296.  
  297. private static void Calculations(int UserValue, int CpuValue, ArrayList<String> UserCard, ArrayList<String> CpuCard, int Amt, int Bet){
  298. if (UserValue > CpuValue && UserValue < 22 || CpuValue > 21 && UserValue < 22){
  299. Amt += Bet;
  300. if(CpuValue > 21){
  301. System.out.println("You won because the opponent busted with a ");
  302. PrintArray(CpuCard);
  303. System.out.println(".\nA total value of " + CpuValue);
  304. again(Amt);
  305. }else{
  306. System.out.print("You won with a ");
  307. PrintArray(UserCard);
  308. System.out.println(".\nA total value of " + UserValue);
  309. again(Amt);
  310. }
  311. }else if(CpuValue < 22 && CpuValue > UserValue || UserValue > 21) {
  312. Amt -= Bet;
  313. if (UserValue > 21){
  314. System.out.print("The opponent won because you busted with a");
  315. PrintArray(UserCard);
  316. System.out.println(".\nA total of " + UserValue + "!");
  317. again(Amt);
  318. }else{
  319. System.out.print("The opponent won with a ");
  320. PrintArray(CpuCard);
  321. System.out.println(".\nA total value of " + CpuValue);
  322. again(Amt);
  323. }
  324. }else{
  325. System.out.println("You both tied with " + CpuValue +". It is a push!");
  326. again(Amt);
  327. }
  328. }
  329.  
  330. private static void PrintArray(ArrayList<String> Array){
  331.  
  332. for (int i = 0; i < Array.size(); i++) {
  333. if (i + 2 == Array.size()) {
  334. System.out.print(Array.get(i) + " and ");
  335. } else if (i + 1 == Array.size()) {
  336. System.out.print(Array.get(i));
  337. } else {
  338. System.out.print(Array.get(i) + ", ");
  339. }
  340. }
  341. }
  342.  
  343. private static int bet(int Amt){
  344. boolean badin = true;
  345. int UserEnter = 0;
  346. while (badin) {
  347. System.out.println("How much would you like to bet, you have " + Amt);
  348. Scanner UserIn = new Scanner(System.in);
  349. UserEnter = (int) UserIn.nextDouble();
  350.  
  351. if (UserEnter > Amt){
  352. System.out.print("Invalid! ");
  353. badin = true;
  354. }else if(UserEnter < 0){
  355. System.out.print("Invalid! ");
  356. badin = true;
  357. }else{
  358. badin = false;
  359. }
  360. }
  361. return UserEnter;
  362. }
  363.  
  364. private static void again(int Amt){
  365. Scanner UserIn = new Scanner(System.in);
  366. boolean repeat = true;
  367.  
  368. while(repeat) {
  369. repeat = false;
  370. System.out.println("Would you like to continue, you have " + Amt);
  371. String UserEnter = UserIn.next().substring(0, 1).toUpperCase();
  372.  
  373. if (UserEnter.equals("Y")) {
  374. run(Amt);
  375. } else if (UserEnter.equals("N")) {
  376. System.out.println("Thanks for playing. You ended with " + Amt);
  377. System.exit(0);
  378. } else {
  379. System.out.print("Invalid! ");
  380. repeat = true;
  381. }
  382. }
  383. }
  384.  
  385. private static void split(int Card1,int Card2){
  386. if (Card1 == Card2){
  387. Scanner UserIn = new Scanner(System.in);
  388.  
  389. boolean badIn = true;
  390. while (badIn) {
  391. System.out.println("Do you want to split? This would get you two hands to hit from, starting with " + Card1 + ".");
  392. String UserEnter = UserIn.next().substring(0,1).toUpperCase();
  393. switch (UserEnter){
  394. case "Y":
  395. splitTrue(Card1,Card2);
  396. badIn = false;
  397. break;
  398. case "N":
  399. badIn = false;
  400. break;
  401. default:
  402. System.out.print("Invalid! ");
  403. badIn = true;
  404. break;
  405. }
  406. }
  407.  
  408. }
  409. }
  410.  
  411. private static void splitTrue(int Card1, int Card2){
  412. System.out.println("Split True Initiated");
  413. System.exit(0);
  414. }
  415. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement