From 9d53d8857eaa1c9405894a88ca75bc4657e42f35 Mon Sep 17 00:00:00 2001 From: msglm Date: Sat, 14 Jan 2023 05:31:48 -0600 Subject: Inital Commit --- Java/BlackJack/Black Jack Part 1.pdf | Bin 0 -> 75427 bytes Java/BlackJack/Black Jack Part 2.pdf | Bin 0 -> 66221 bytes Java/BlackJack/Black Jack Part 3.pdf | Bin 0 -> 26847 bytes Java/BlackJack/Black Jack Part 4.pdf | Bin 0 -> 88284 bytes Java/BlackJack/Blackjack.java | 185 ++++++++++++++++++++++++++ Java/BlackJack/Card.java | 250 +++++++++++++++++++++++++++++++++++ Java/BlackJack/CardTester.java | 30 +++++ Java/BlackJack/Deck.java | 38 ++++++ Java/BlackJack/DeckDriver.java | 8 ++ Java/BlackJack/Hand.java | 51 +++++++ 10 files changed, 562 insertions(+) create mode 100644 Java/BlackJack/Black Jack Part 1.pdf create mode 100644 Java/BlackJack/Black Jack Part 2.pdf create mode 100644 Java/BlackJack/Black Jack Part 3.pdf create mode 100644 Java/BlackJack/Black Jack Part 4.pdf create mode 100644 Java/BlackJack/Blackjack.java create mode 100644 Java/BlackJack/Card.java create mode 100644 Java/BlackJack/CardTester.java create mode 100644 Java/BlackJack/Deck.java create mode 100644 Java/BlackJack/DeckDriver.java create mode 100644 Java/BlackJack/Hand.java (limited to 'Java/BlackJack') diff --git a/Java/BlackJack/Black Jack Part 1.pdf b/Java/BlackJack/Black Jack Part 1.pdf new file mode 100644 index 0000000..77f8e4f Binary files /dev/null and b/Java/BlackJack/Black Jack Part 1.pdf differ diff --git a/Java/BlackJack/Black Jack Part 2.pdf b/Java/BlackJack/Black Jack Part 2.pdf new file mode 100644 index 0000000..6421d17 Binary files /dev/null and b/Java/BlackJack/Black Jack Part 2.pdf differ diff --git a/Java/BlackJack/Black Jack Part 3.pdf b/Java/BlackJack/Black Jack Part 3.pdf new file mode 100644 index 0000000..fd4e56c Binary files /dev/null and b/Java/BlackJack/Black Jack Part 3.pdf differ diff --git a/Java/BlackJack/Black Jack Part 4.pdf b/Java/BlackJack/Black Jack Part 4.pdf new file mode 100644 index 0000000..25f4c2b Binary files /dev/null and b/Java/BlackJack/Black Jack Part 4.pdf differ 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("."); + 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; + }}} diff --git a/Java/BlackJack/Card.java b/Java/BlackJack/Card.java new file mode 100644 index 0000000..f4b2df4 --- /dev/null +++ b/Java/BlackJack/Card.java @@ -0,0 +1,250 @@ +public class Card { + + // data member + private int facePointValue; + private String faceValue; + private int points; + private String suit; + private int suitPointValue; + + + + // Constructors + //accepts suit and face + public Card (String inFace, String inSuit) { + + switch(inFace){ //Reasoning for the switch is that working with strings as values in a comparision game will only casue problems, it must be converted to a sensable value + case "King": //all face cards are 10. 10 is also 10 (for obvious reasons) + case "king": + case "Joker": + case "joker": + case "Queen": + case "queen": + case "Jack": + case "jack": + case "10": + case "ten": + case "Ten": + facePointValue = 10; + break; + //these comparisions are the worst, but its the best way I could figure out accounting for all reasonable inputs without using regex + case "9": + case "nine": + case "Nine": + facePointValue = 9; + break; + + case "8": + case "eight": + case "Eight": + facePointValue = 8; + break; + + case "7": + case "seven": + case "Seven": + facePointValue = 7; + break; + + case "6": + case "six": + case "Six": + facePointValue = 6; + break; + + case "5": + case "five": + case "Five": + facePointValue = 5; + break; + + case "4": + case "four": + case "Four": + facePointValue = 4; + break; + + case "3": + case "three": + case "Three": + facePointValue = 3; + break; + case "2": + case "two": + case "Two": + facePointValue = 2; + break; + + case "1": + case "one": + case "One": + case "Ace": + case "ace": + facePointValue = 1; + break; + default: + System.out.println("User Input was Invalid! " + inFace + " Is not a valad card!"); + System.exit(1); + + } + + faceValue = inFace; //since all of the above is just for value finding and will crash if breaks, might as well just get inFace as the value. + switch(inSuit){ + case "heart": + case "hearts": + case "Heart": + case "Hearts": + suitPointValue = 4; + break; + case "club": + case "clover": + case "Club": + case "Clover": + suitPointValue = 3; + break; + case "Diamond": + case "diamond": + suitPointValue = 2; + break; + case "spade": + case "spear": + case "shovel": + case "Spade": + case "Spear": + case "Shovel": + suitPointValue = 1; + break; + default: + System.out.println("User Input was Invalid! " + inSuit + " Is not a valad card!"); + + System.exit(1); + } + suit = inSuit; //ditto reasoning of last comment + + } +//no suit or face? assume 4 of clovers + public Card() { + suitPointValue = 3; + facePointValue = 4; + suit = "Clover"; + faceValue = "4"; + } + +// set & return +// set + +//return +public String getFaceValue(){ +return figureName(facePointValue); +} + +public String getFaceSuit(){ +return figureSuit(suitPointValue); +} + +public int getFaceNumericValue(){ +return facePointValue; +} + +public int getSuitNumericValue(){ +return suitPointValue; +} + + + public String figureName(int input) { + switch(input){ + + case 1: + return "One"; + + + case 2: + return "Two"; + + + case 3: + return "Three"; + + + case 4: + return "Four"; + + case 5: + return "Five"; + + case 6: + return "Six"; + + case 7: + return "Seven"; + + case 8: + return "Eight"; + + case 9: + return "Nine"; + + case 10: + return "Ten"; + + case 11: + return "Jack"; + + case 12: + return "Queen"; + + case 13: + return "King"; + + case 14: + return "Joker"; + + default: + System.out.println("figureName does not take " + input + "as a valid input!"); + return null; + } + + } + + public String figureSuit(int input) { + switch(input){ + case 1: + return "Hearts"; + + case 2: + return "Clovers"; + + case 3: + return "Diamonds"; + + case 4: + return "Spade"; + default: + System.out.println("figureSuit does not take" + input + "as a valid input!"); + return null; + + + } + } + + public String compareCardValue(String card1Name, int card1Face, int card1Suit, String card2Name, int card2Face, int card2Suit) { + + if(card1Face == card2Face){ + if(card1Suit > card2Suit){ + return card1Name; + } else { + return card2Name; + } + + } + + if (card1Face > card2Face) { + return card1Name; + } else { + return card2Name; + } + } + +} + + diff --git a/Java/BlackJack/CardTester.java b/Java/BlackJack/CardTester.java new file mode 100644 index 0000000..5655ea2 --- /dev/null +++ b/Java/BlackJack/CardTester.java @@ -0,0 +1,30 @@ +/* CardTester.java +Name: Nancy Reddig, Academy Instructor +Date: 5/15/03 +Modified by: +Carl Frank, 08/29/07 +Modifications were to not allow access to instance variables of Card. +The assumptions are that these will be private. +Use this class to test your Card Class +Program that tests the Card Class*/ +// Java packages +import java.awt.*; +import java.awt.event.*; +import javax.swing.*; // program uses JOptionPane +public class CardTester { +// main method begins execution of Java application +public static void main( String args[] ) { + +Card card1= new Card("King", "Hearts"); +// allow the user to think that they are dealing a random card + +JOptionPane.showMessageDialog(null, "Deal a Card", +"Card", JOptionPane.INFORMATION_MESSAGE ); +//display the card +JOptionPane.showMessageDialog( null, "The card is the " + card1.getFaceValue() + " of " //copying code from a pdf and having to debug it? Now this is a new low + + card1.getSuit(), + "Your Card", JOptionPane.PLAIN_MESSAGE ); + +System.exit(0); +} +} diff --git a/Java/BlackJack/Deck.java b/Java/BlackJack/Deck.java new file mode 100644 index 0000000..a828e52 --- /dev/null +++ b/Java/BlackJack/Deck.java @@ -0,0 +1,38 @@ +import java.util.Random; //for randomness +public class Deck { + + // data member + private int[] deckFace = new int[20]; + private int[] deckSuit = new int[20]; + + + + // Constructors + //accepts suit and face + public Deck() { + for(int i=0;i<20;i++){ + Random random = new Random(); + deckFace[i] = random.nextInt(14); //1-10 are as expected, 11 is Jack, 12 is Queen, 13 is King, 14 is Joker + deckSuit[i] = random.nextInt(4); //Card order is highest to lowest. 1 is hearts 4 is spades. + } + } + + void printDeck(){ + + for(int i=0;i<20;i++){ + System.out.println("Card " + i + " has a Face of " + deckFace[i] + " and a Suit of " + deckSuit[i]); + } + + } + + void shuffle(){ + + for(int i=0;i<20;i++){ + Random random = new Random(); + deckFace[i] = random.nextInt(14); //1-10 are as expected, 11 is Jack, 12 is Queen, 13 is King, 14 is Joker + deckSuit[i] = random.nextInt(4); //Card order is highest to lowest. 1 is hearts 4 is spades. + + } + + } +} diff --git a/Java/BlackJack/DeckDriver.java b/Java/BlackJack/DeckDriver.java new file mode 100644 index 0000000..28a8ff4 --- /dev/null +++ b/Java/BlackJack/DeckDriver.java @@ -0,0 +1,8 @@ +class DeckDriver { +public static void main(String [] args) { +Deck myDeck = new Deck(); +myDeck.printDeck(); +myDeck.shuffle(); +System.out.println("\n\nSorted Order"); +myDeck.printDeck(); +}} diff --git a/Java/BlackJack/Hand.java b/Java/BlackJack/Hand.java new file mode 100644 index 0000000..a9fb174 --- /dev/null +++ b/Java/BlackJack/Hand.java @@ -0,0 +1,51 @@ +public class Hand { + + // data member + private int[] cardsInHandFace= new int[22]; //the maximum possible hands size is 22, if you draw 22 aces you'll bust. Values as expected + private int[] cardsInHandSuit= new int[22]; //usual values + private int numCardsInHand; + + // Constructors + //accepts suit and face + public Hand (int firstFace, int firstSuit, int secondFace, int secondSuit) { + cardsInHandFace[1] = firstFace; + cardsInHandSuit[1] = firstSuit; + cardsInHandFace[2] = secondFace; + cardsInHandSuit[2] = secondSuit; + //for some reason it wanted this, so I gave it. This code is total spaghetti and garbage. These assignments don't feel like they were ment to work with one another. + } + +// set & return +// set + +//return +public int calculateHandValue(){ + +int value = 0; + +for(int i=0; i < cardsInHandFace.length;i++){ +value = value + cardsInHandFace[i]; +} +return value; +} + +public void showAllCards(){ //there is absolutely no reason this should return something. These constraints are bad and don't teach OOP well. None of this need be OOP at all as it just overcomplicates this to a maximal degree. A much better teaching for OOP would be making some small farming game in where you had animals to take care of. OOP would be useful there as you could easily generate personalities, needs, types, colors, etc for each animal as to make good use of OOP and its capabilities. Blackjack doesn't need nor have variation like that and thus does not need OOP. Due to the fact that each class has a private value to it, there is great redundancy in the amonut of values stored, therefore causing the application to use more RAM than it needs to. This is also difficult to keep track of because some of these methods (such as card name and value finding) are redundant and cannot easily work with one another due to the strange constraints in what methods we must use. + + for(int i=1; i < cardsInHandFace.length + 1;i++){ + System.out.print("Card Number " + i + "'s face is " + this.cardsInHandFace[i] + " and its suit is " + this.cardsInHandSuit[i]); + } + +} + + +public void showOneCard(int input) { //fix to return something +System.out.print("Card Number " + input + "'s face is " + this.cardsInHandFace[input] + " and its suit is " + this.cardsInHandSuit[input]); +} + +public void hit(Card c) { //fix to return something + for(int i=0; i < cardsInHandFace.length;i++){ + if(cardsInHandFace[i] == 0) { + cardsInHandFace[i] = c.getFaceNumericValue(); + cardsInHandSuit[i] = c.getSuitNumericValue(); + }}} +} -- cgit v1.2.3