Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Blackjack {
- public static void main(String[] args) {
- split(5,5);
- run(startBet());
- }
- private static void run(int UserAmount){
- ArrayList<String> UserCard = new ArrayList<>();
- ArrayList<String> CpuCard = new ArrayList<>();
- // Getting random starting numbers \\
- int UserRand1 = randomNumber();
- int UserRand2 = randomNumber();
- int CpuRand1 = randomNumber();
- // Setting Values to the Cards \\
- int UserValue = (Rand2Value(UserRand1) + Rand2Value(UserRand2));
- int CpuValue = Rand2Value(CpuRand1);
- // Setting Previous Values to Cards \\
- UserCard.add(Rand2Card(UserRand1));
- UserCard.add(Rand2Card(UserRand2));
- CpuCard.add(Rand2Card(CpuRand1));
- // Asking the user how much he/she wants to bet \\
- int Bet = bet(UserAmount);
- // Seeing if the user has blackjack \\
- Blackjack(Bet,UserValue,UserAmount);
- // Seeing if user wants to double \\
- Double(CpuValue,UserValue,Bet, UserAmount,UserCard,CpuCard);
- // Loop for hit or stand \\
- UserValue = HitSequence(UserCard,CpuCard,UserValue);
- // Seeing if user busted \\
- UserValue = BustCheck(UserCard,CpuCard,UserValue);
- // Computer Hitting \\
- CpuValue = CpuHitting(CpuCard,CpuValue);
- // Calculations for Winner | Printing Results \\
- Calculations(UserValue,CpuValue,UserCard,CpuCard,UserAmount,Bet);
- }
- private static int randomNumber(){
- Random random = new Random();
- int rand = random.nextInt(13) + 1;
- return rand;
- }
- private static int startBet(){
- Scanner UserIn = new Scanner(System.in);
- System.out.println("How much money would you like to start with?");
- int UserAmount = (int) UserIn.nextDouble();
- return UserAmount;
- }
- private static String Rand2Card(int Rand) {
- switch (Rand){
- case 1:
- return "A";
- case 2:
- return "2";
- case 3:
- return "3";
- case 4:
- return "4";
- case 5:
- return "5";
- case 6:
- return "6";
- case 7:
- return "7";
- case 8:
- return "8";
- case 9:
- return "9";
- case 10:
- return "10";
- case 11:
- return "J";
- case 12:
- return "Q";
- case 13:
- return "K";
- default:
- return "0";
- }
- }
- private static int Rand2Value(int Rand) {
- switch (Rand){
- case 1:
- return 11;
- case 2:
- return 2;
- case 3:
- return 3;
- case 4:
- return 4;
- case 5:
- return 5;
- case 6:
- return 6;
- case 7:
- return 7;
- case 8:
- return 8;
- case 9:
- return 9;
- case 10:
- return 10;
- case 11:
- return 10;
- case 12:
- return 10;
- case 13:
- return 10;
- default:
- return 0;
- }
- }
- private static void Blackjack(int Bet, int Value, int Amt){
- if(Value == 21){
- System.out.println("You got blackjack! ");
- Amt += (int) (Bet * 1.5);
- again(Amt);
- }
- }
- private static int HitOrStand(ArrayList<String> Array, ArrayList<String> CpuArray, int size, int Value){
- boolean badIn = true;
- while (badIn) {
- badIn = false;
- System.out.print("Would you like to hit or stand! You have a ");
- PrintArray(Array);
- System.out.println("! A total of " + Value + "!");
- System.out.println("The opponent has a " + CpuArray.get(0) + " showing!");
- Scanner UserIn = new Scanner(System.in);
- String UserEnter = UserIn.next().substring(0,1).toUpperCase();
- switch (UserEnter) {
- case "H":
- return 1;
- case "S":
- return 2;
- default:
- System.out.print("Invalid! ");
- badIn = true;
- break;
- }
- }
- return 0; // Just for Safety Measures
- }
- private static int bust(ArrayList<String> Array, int Value){
- for(int i = 0; i < Array.size();i++) {
- if (Array.get(i).contains("A")) {
- return (Value - 10);
- }
- }
- return Value;
- }
- private static void Double(int CpuValue, int Value, int Bet, int Amt, ArrayList<String> Array, ArrayList<String> CpuArray) {
- boolean badIn = true;
- if (Amt != Bet) {
- while (badIn) {
- badIn = false;
- System.out.print("Would you like to double (Bet More)? You have a ");
- PrintArray(Array);
- System.out.println("! A total of " + Value + "!");
- System.out.println("The opponent has a " + CpuArray.get(0) + " showing!");
- System.out.println("You will only be able to hit once! And, can only increase up to double your bet");
- Scanner UserIn = new Scanner(System.in);
- String UserEnter = UserIn.next().substring(0, 1).toUpperCase();
- switch (UserEnter) {
- case "Y":
- int rand = randomNumber();
- Bet += bet(Bet);
- Array.add(Rand2Card(rand));
- Value += Rand2Value(rand);
- Value = DoubleBustCheck(Array,Value);
- CpuValue = CpuHitting(CpuArray,CpuValue);
- Calculations(Value,CpuValue,Array,CpuArray,Amt,Bet);
- break;
- case "N":
- break;
- default:
- System.out.print("Invalid! ");
- badIn = true;
- break;
- }
- }
- }
- }
- private static int HitSequence(ArrayList<String> Array, ArrayList<String> CpuArray, int Value){
- boolean hit = true;
- while (Value <= 21 && hit){
- // Asking User if Hit or Stand \\
- int UserChoice = HitOrStand(Array,CpuArray,Array.size(),Value);
- if (UserChoice == 1){
- // Performing Actions Based on Hit or Stand \\
- int UserRand = randomNumber();
- Array.add(Rand2Card(UserRand));
- Value += Rand2Value(UserRand);
- hit = true;
- }else{
- hit = false;
- }
- }
- return Value;
- }
- private static int AceAmount(ArrayList<String> Array) {
- int Amount = 0;
- for(int i = 0; i < Array.size(); i ++) {
- if(Array.get(i).contains("A")) {
- Amount ++;
- }
- }
- return Amount;
- }
- private static int BustCheck(ArrayList<String> Array, ArrayList<String> CpuArray, int UserValue){
- int RunAmt = 0;
- while (UserValue >= 22){
- int AmountAce = AceAmount(Array);
- int Returned = bust(Array,UserValue);
- if (Returned < 22 && RunAmt < AmountAce){
- UserValue = HitSequence(Array,CpuArray,Returned);
- RunAmt++;
- } else {
- System.out.println("You busted with a value of " + UserValue);
- return UserValue;
- }
- }
- return UserValue;
- }
- private static int DoubleBustCheck(ArrayList<String> Array, int UserValue){
- int RunAmt = 0;
- while (UserValue >= 22){
- int AmountAce = AceAmount(Array);
- int Returned = bust(Array,UserValue);
- if (Returned < 22 && RunAmt < AmountAce){
- UserValue = Returned;
- RunAmt++;
- } else {
- System.out.println("You busted with a value of " + UserValue);
- return UserValue;
- }
- }
- return UserValue;
- }
- private static int CpuHitting(ArrayList<String> Array, int Value){
- int RunAmt = 0;
- while(Value < 18){
- int AceAmt = AceAmount(Array);
- if(Value == 17 && RunAmt < AceAmt){
- int Rand = randomNumber();
- Value += Rand2Value(Rand);
- Array.add(Rand2Card(Rand));
- RunAmt ++;
- }else if(Value == 17){
- return Value;
- }else{
- int Rand = randomNumber();
- Value += Rand2Value(Rand);
- Array.add(Rand2Card(Rand));
- }
- }
- return Value;
- }
- private static void Calculations(int UserValue, int CpuValue, ArrayList<String> UserCard, ArrayList<String> CpuCard, int Amt, int Bet){
- if (UserValue > CpuValue && UserValue < 22 || CpuValue > 21 && UserValue < 22){
- Amt += Bet;
- if(CpuValue > 21){
- System.out.println("You won because the opponent busted with a ");
- PrintArray(CpuCard);
- System.out.println(".\nA total value of " + CpuValue);
- again(Amt);
- }else{
- System.out.print("You won with a ");
- PrintArray(UserCard);
- System.out.println(".\nA total value of " + UserValue);
- again(Amt);
- }
- }else if(CpuValue < 22 && CpuValue > UserValue || UserValue > 21) {
- Amt -= Bet;
- if (UserValue > 21){
- System.out.print("The opponent won because you busted with a");
- PrintArray(UserCard);
- System.out.println(".\nA total of " + UserValue + "!");
- again(Amt);
- }else{
- System.out.print("The opponent won with a ");
- PrintArray(CpuCard);
- System.out.println(".\nA total value of " + CpuValue);
- again(Amt);
- }
- }else{
- System.out.println("You both tied with " + CpuValue +". It is a push!");
- again(Amt);
- }
- }
- private static void PrintArray(ArrayList<String> Array){
- for (int i = 0; i < Array.size(); i++) {
- if (i + 2 == Array.size()) {
- System.out.print(Array.get(i) + " and ");
- } else if (i + 1 == Array.size()) {
- System.out.print(Array.get(i));
- } else {
- System.out.print(Array.get(i) + ", ");
- }
- }
- }
- private static int bet(int Amt){
- boolean badin = true;
- int UserEnter = 0;
- while (badin) {
- System.out.println("How much would you like to bet, you have " + Amt);
- Scanner UserIn = new Scanner(System.in);
- UserEnter = (int) UserIn.nextDouble();
- if (UserEnter > Amt){
- System.out.print("Invalid! ");
- badin = true;
- }else if(UserEnter < 0){
- System.out.print("Invalid! ");
- badin = true;
- }else{
- badin = false;
- }
- }
- return UserEnter;
- }
- private static void again(int Amt){
- Scanner UserIn = new Scanner(System.in);
- boolean repeat = true;
- while(repeat) {
- repeat = false;
- System.out.println("Would you like to continue, you have " + Amt);
- String UserEnter = UserIn.next().substring(0, 1).toUpperCase();
- if (UserEnter.equals("Y")) {
- run(Amt);
- } else if (UserEnter.equals("N")) {
- System.out.println("Thanks for playing. You ended with " + Amt);
- System.exit(0);
- } else {
- System.out.print("Invalid! ");
- repeat = true;
- }
- }
- }
- private static void split(int Card1,int Card2){
- if (Card1 == Card2){
- Scanner UserIn = new Scanner(System.in);
- boolean badIn = true;
- while (badIn) {
- System.out.println("Do you want to split? This would get you two hands to hit from, starting with " + Card1 + ".");
- String UserEnter = UserIn.next().substring(0,1).toUpperCase();
- switch (UserEnter){
- case "Y":
- splitTrue(Card1,Card2);
- badIn = false;
- break;
- case "N":
- badIn = false;
- break;
- default:
- System.out.print("Invalid! ");
- badIn = true;
- break;
- }
- }
- }
- }
- private static void splitTrue(int Card1, int Card2){
- System.out.println("Split True Initiated");
- System.exit(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement