Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Collections;
- import java.util.Scanner;
- import java.util.ArrayList;
- public class Blackjack {
- public static ArrayList<cards> deck = new ArrayList<cards>();
- public static ArrayList<Player> playerDatabase = new ArrayList<Player>();
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~MAIN CLASS~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
- public static void main(String args []){
- playBJ();
- /*
- int i;
- int decks = 3; // where decks determines the amount of decks being played.
- for (int x = 0; x < decks; x++){
- for(i = deck.size() - 1; i > -1; i--){
- System.out.println(showHand(playerDatabase));
- //System.out.println(toString(deck.get(i)));
- deck.remove(i);
- }
- }
- */
- }//~~~~~~~~~~~~~~~~~~~~~~~~~~~~END OF MAIN CLASS~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
- public static String toString(cards blah){
- // toString. This is used to examine the deck, and at the points where the numbers are
- // 11, 12, 13, 1, it changes them to the appropriate variable.
- String s = "";
- switch( blah.getValue()){
- case 1:
- s = "A";
- break;
- case 11:
- s = "J";
- break;
- case 12:
- s = "Q";
- break;
- case 13:
- s = "K";
- break;
- default:
- s += blah.getValue(); // this is the same as s = s + blah.getValue();
- break;
- }// end of switch statement 1
- switch( blah.getSuit()){
- case 1:
- return s + "♥";
- case 2:
- return s + "♦";
- case 3:
- return s + "♣";
- default:
- return s + "♠";
- }// end of switch statement 2
- }// end of function
- // SHUFFEL FUNCTION
- public static void shuffle(){
- for(int i = 1; i < 5; i++){
- for (int j = 1; j < 14; j++){
- deck.add(new cards(i, j));
- }// end of loop 2
- }// end of loop 1
- Collections.shuffle(deck);
- }
- // DRAW FUNCTION
- public static cards draw(ArrayList<cards> deal){
- cards tmp = new cards(0,0);
- tmp = deal.get(0);
- return tmp;
- }
- // DEAL FUNCTION
- public static void deal(){
- shuffle();
- for(int i = 0; i < 2; i++){
- for(int j = 1; j < playerDatabase.size() -1; j++){
- Player tmp = playerDatabase.get(j); // getting cards from deck
- cards tmpCard = draw(deck);
- deck.remove(0);
- ArrayList<cards> tmpArray = new ArrayList<cards>();
- tmpArray.add(tmpCard); // adding card from deck to player's hand
- tmp.setCardsInHand(tmpArray);
- playerDatabase.set(j, tmp);
- }
- }
- }
- // CREATE PLAYER FUNCTION
- public static void createPlayer(){
- String response = "yes";
- Scanner sc = new Scanner(System.in);
- ArrayList<cards> dealer = new ArrayList<cards>();
- ArrayList<cards> cardsInHand = new ArrayList<cards>();
- playerDatabase.add(new Player("Dealer", 0.0, 0, 0, false, dealer )); // adds the dealer to spot 1
- while(response.equals("yes") || response.equals("Yes") || response.equals("Y")){
- System.out.println("What is your name?");
- String name = sc.nextLine();
- System.out.println("How much money do you have?");
- double money = sc.nextInt();
- playerDatabase.add(new Player(name, money, 0, 0, false, cardsInHand));
- System.out.println("Would you like to create another player?");
- response = sc.nextLine();
- response = sc.nextLine();
- // System.out.println(playerDatabase.size());
- }// end of while loop
- // this sets the dealer's money to 25x the amount of money the player's have
- double dealerMoney = 0;
- for(int x = 1; x < playerDatabase.size() - 1; x++){
- Player tmp = playerDatabase.get(x);
- double playerMoney = tmp.getMoney();
- dealerMoney += playerMoney * 25;
- }
- playerDatabase.get(1).setMoney(dealerMoney);
- sc.close();
- }// end of CREATE PLAYER FUNCTION
- // Get Value
- // gets the value of the player's hand by adding up the cards in it.
- // SHOW HAND FUNCTION
- public static int showHand(ArrayList<Player> Player){
- int tmpV;
- for(int x = 1; x <= playerDatabase.size() - 1; x++){
- Player tmp = playerDatabase.get(x);
- tmpV = tmp.getHandValue();
- ArrayList<cards> tmpArray = new ArrayList<cards>();
- tmpArray = tmp.getCardsInHand();
- for(int i = 0; i < tmpArray.size() - 1; i++){
- System.out.println(toString(tmpArray.get(i)));
- System.out.printf("\n Total Hand Value: ");
- }
- return tmpV;
- }
- return 0;
- }
- // PlayBJ
- public static void playBJ(){
- createPlayer();
- double bet;
- double totalBetSum = 0;
- while(totalBetSum == 0){
- shuffle(); // creates a shuffled deck
- Scanner sc = new Scanner(System.in);
- for(int j = 1; j <= playerDatabase.size() -1; j++){ // Handles the betting section
- System.out.println("I'm entering the betting loop");
- System.out.println("There are : " + playerDatabase.size() + " people in the database");
- Player tmp = playerDatabase.get(j);
- String tmpName = tmp.getName();
- System.out.println("How much does " + tmpName + " wish to bet?");
- String ii = sc.nextLine();
- bet = sc.nextDouble();
- tmp.setBet(bet);
- totalBetSum += tmp.getBet();
- if(tmp.getBet() == 0){
- tmp.setTurn(true);
- }
- playerDatabase.set(j, tmp);
- } // end of betting section
- System.out.print("Cards are being delt...");
- for(int x = 1; x <= playerDatabase.size() - 1; x++){ // starts at one because it doesn't include the dealer
- System.out.println("I'm entering the dealing loop"); // TESTING
- Player tmp = playerDatabase.get(x);
- String playerName = tmp.getName();
- if(tmp.isTurn() == false){ // means they haven't had a chance to go yet and that you've bet
- System.out.print(playerName + " has been delt: " + showHand(playerDatabase) + "\n");
- System.out.printf("1: STAY \n 2: HIT \n 3: DOUBLE \n 4: SPLIT");
- int response = 0;
- response = sc.nextInt();
- response = sc.nextInt(); // THIS LINE OF CODE MAY NEED TO BE REMOVED FOR TESTING PURPOSES
- while(response > 4 || response < 1){
- System.out.printf("\nPlease enter a number between 1 and 4, to indicate you choice.\n");
- response = sc.nextInt();
- response = sc.nextInt(); // THIS LINE OF CODE MAY NEED TO BE REMOVED FOR TESTING PURPOSES
- }
- if(response == 1){
- tmp.setTurn(true);
- }
- else if(response == 2){
- while(response != 1){
- cards tmpCard = draw(deck);
- tmp.getCardsInHand().add(tmpCard); // adds the cards to player's hand (ArrayList)
- int tmpCardValue = tmpCard.getValue();
- int newCardValue = tmp.getHandValue() + tmpCardValue; // calculates the sum of the player's hand
- tmp.setHandValue(newCardValue);
- if(tmp.getHandValue() <= 21){
- System.out.println("Hand Value: " + tmp.getHandValue()); //print the hand
- System.out.printf("1: STAY \n 2: HIT \n");
- while(response > 2 || response < 1){
- response = sc.nextInt();
- System.out.printf("Invalid number. \n 1: STAY \n 2: HIT \n");
- }
- }
- else{
- System.out.println("BUST!");
- double money = tmp.getMoney() - tmp.getBet();
- tmp.setMoney(money);
- }
- }
- }
- else if(response == 3){
- while(response != 1){
- cards tmpCard = draw(deck);
- tmp.getCardsInHand().add(tmpCard);
- int tmpCardValue = tmpCard.getValue();
- int newCardValue = tmp.getHandValue() + tmpCardValue;
- tmp.setHandValue(newCardValue);
- }
- }
- }// end of 1st if loop, which determines turn
- }
- sc.close();
- }
- }// end of PlayBJ
- }// end of class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement