diff options
author | msglm <msglm@techchud.xyz> | 2023-01-14 05:31:48 -0600 |
---|---|---|
committer | msglm <msglm@techchud.xyz> | 2023-01-14 05:31:48 -0600 |
commit | 9d53d8857eaa1c9405894a88ca75bc4657e42f35 (patch) | |
tree | eb1efc1d028b949dd83bb710c68be8eff58f26e7 /Java/BlackJack/Blackjack.java | |
download | school-code-master.tar.gz school-code-master.tar.bz2 school-code-master.zip |
Diffstat (limited to 'Java/BlackJack/Blackjack.java')
-rw-r--r-- | Java/BlackJack/Blackjack.java | 185 |
1 files changed, 185 insertions, 0 deletions
diff --git a/Java/BlackJack/Blackjack.java b/Java/BlackJack/Blackjack.java new file mode 100644 index 0000000..ff06b78 --- /dev/null +++ b/Java/BlackJack/Blackjack.java @@ -0,0 +1,185 @@ +//TODO + +import java.util.Random; //for randomness +//import java.io.BufferedReader; +//import java.io.InputStreamReader; +import java.io.*; + +class Blackjack { + public static void main(String [] args) throws java.io.IOException { + System.out.println("Welcome To Blackjack!"); + System.out.println("Copyright (C) 2021 msglm."); + System.out.println("License AGPLv3+: GNU Affero GPL version 3 Only"); + System.out.println("<http://www.gnu.org/licenses/agpl.html>."); + System.out.println("This is free software: you are free to change and redistribute it."); + System.out.println("There is NO WARRANTY, to the extent permitted by law. \n"); + + //first is for card value, second is for card type + int[] dealerFaceValues = new int[21]; + int[] dealerSuitValues = new int[21]; + int[] playerFaceValues = new int[21]; + int[] playerSuitValues = new int[21]; + + int dealerDeltCards = 0; + int playerDeltCards = 0; + + int dealerScore = 0; + int playerScore = 0; + int dealerScoreSuit = 0; + int playerScoreSuit = 0; + + int roundnum=0; + + boolean stood = false; + boolean gameEnd = false; + boolean playerHit = true; + boolean dealerHit = true; + Random random = new Random(); + + dealerFaceValues[0] = random.nextInt(9+1)+1; + dealerSuitValues[0] = random.nextInt(1+1)+1; + + playerFaceValues[0] = random.nextInt(9+1)+1; + playerSuitValues[0] = random.nextInt(1+1)+1; + + + //TODO: SWITCH STATEMENT THAT ASSIGNES A NAME TO EACH CARD + + System.out.println("Dealer's Hand:"); + for(int i=0;i<21;i++){ + int curcar = i + 1; + if(dealerFaceValues[i] != 0 || dealerSuitValues[i] != 0){ + System.out.println("Card " + curcar + " has a Face of " + dealerFaceValues[i] + " and a Suit of " + dealerSuitValues[i]); + } + } + + + System.out.println("Player's Hand:"); + for(int i=0;i<21;i++){ + int curcar = i + 1; + if(playerFaceValues[i] != 0 || playerSuitValues[i] != 0){ + System.out.println("Card " + curcar + " has a Face of " + playerFaceValues[i] + " and a Suit of " + playerSuitValues[i]); + } + } + while(!gameEnd){ + roundnum++; + System.out.println("Would you like a card? Y/n"); + + BufferedReader cardWantInput = new BufferedReader(new InputStreamReader(System.in)); + String tempString = cardWantInput.readLine(); + char cardWant = tempString.charAt(0); + System.out.println("===ROUND " + roundnum + " ==="); + if(cardWant == 'y' || cardWant == 'Y'){ + playerHit = true; + playerDeltCards++; + playerFaceValues[playerDeltCards] = random.nextInt(9+1)+1; + playerSuitValues[playerDeltCards] = random.nextInt(1+1)+1; + } else { + stood = true; + playerHit = false; + } + System.out.println(""); + System.out.println("Dealer's Hand:"); + for(int i=0;i<21;i++){ + int curcar = i + 1; + if(dealerFaceValues[i] != 0 || dealerSuitValues[i] != 0){ + if(curcar == 1){ + System.out.println("Card " + curcar + " has a Face of " + dealerFaceValues[i] + " and a Suit of " + dealerSuitValues[i]); + } else { + System.out.println ("The Dealer has another card..."); + } + } + } + System.out.println(""); + System.out.println("Player's Hand:"); + for(int i=0;i<21;i++){ + int curcar = i + 1; + if(playerFaceValues[i] != 0 || playerSuitValues[i] != 0){ + System.out.println("Card " + curcar + " has a Face of " + playerFaceValues[i] + " and a Suit of " + playerSuitValues[i]); + } + } + System.out.println(""); + + + //Find each one's score + for (int i=0;i<21;i++){ + playerScore = playerScore + playerFaceValues[i]; + } + + for (int i=0;i<21;i++){ + dealerScore = dealerScore + dealerFaceValues[i]; + } + + for (int i=0;i<21;i++){ + playerScoreSuit = playerScoreSuit + playerSuitValues[i]; + } + + for (int i=0;i<21;i++){ + dealerScoreSuit = dealerScoreSuit + dealerSuitValues[i]; + } + + if(dealerScore < 17){ + dealerHit = true; + dealerDeltCards++; + dealerFaceValues[dealerDeltCards] = random.nextInt(9+1)+1; + dealerSuitValues[dealerDeltCards] = random.nextInt(1+1)+1; + } else { + dealerHit = false; + } + + + System.out.println("Status:"); + if(playerHit){ + System.out.println("You took a Hit!"); + } else { + System.out.println("You decided to stand..."); + } + + + if(dealerHit){ + System.out.println("The dealer has taken a hit!"); + } else { + System.out.println("The dealer has decided to stand..."); + } + + if(dealerScore > 21) { + System.out.println("DEALER BUSTED!!! YOU WIN!"); + gameEnd = true; + } + if(playerScore > 21){ + System.out.println("YOU BUSTED!!! YOU LOSE"); + gameEnd = true; + + } + + //probably should clean this up with swich statments. This looks horrible. + if (stood == true && gameEnd == false) { + if(dealerScore > playerScore){ + System.out.println("DEALER HAS THE HIGHER CARDS!!! YOU LOSE!"); + gameEnd = true; + } else if (playerScore > dealerScore){ + System.out.println("YOU HAVE THE HIGHER CARDS!!! YOU WIN!"); + gameEnd = true; + } else { + System.out.println("YOU HAVE THE SAME VALUES. WE MUST LOOK AT THE SUITS TO DETERMINE WHO WON"); + if(dealerScore + dealerScoreSuit > playerScore + playerScoreSuit){ + System.out.println("DEALER HAS THE HIGHER CARDS!!! YOU LOSE!"); + gameEnd = true; + } else if (playerScore + playerScoreSuit > dealerScore + dealerScoreSuit){ + System.out.println("YOU HAVE THE HIGHER CARDS!!! YOU WIN!"); + gameEnd = true; + } else { + System.out.println("THE VALUES ARE STILL THE SAME! A COIN FLIP TO DECIDE WHO WON!"); + if(random.nextInt(1) == 1){ + System.out.println("YOU HAVE WON BY PURE LUCK!"); + gameEnd = true; + } else { + System.out.println("YOU HAVE LOST BY PURE LUCK!"); + gameEnd = true; + }}}} + //reset the score counted for next round + playerScoreSuit=0; + dealerScoreSuit=0; + dealerScore=0; + playerScore=0; + }}} |